libstd: refine dynamic memory allocations in os::getcwd() and os::change_dir()

Although the call to `as_slice()` below allows pointing to uninitialized memory because you could have allocated more than `getcwd` set and the `as_slice()` will encompass all the data.