Page 1 of 1

getenv() may be unsafe?

Posted: February 26th, 2017, 3:51 am
by YoMomIsANiceLady
Hello,

I'm trying to get file i/o working so I can store my game's top score in appdata. So I use getenv("appdata") in order to do that. But visual studio gives me a warning saying that getenv may be unsafe and that I should use _dupenv_s instead.

Now I tried looking around for how to use the function but I don't think I understand it.
https://msdn.microsoft.com/en-us/library/ms175774.aspx
I guess I need to give it a buffer? Then free the buffer?
It also takes in size? What is this sorcery?

http://stackoverflow.com/questions/1591 ... f-dupenv-s

Can someone explain / give an example how I can do this properly?

Re: getenv() may be unsafe?

Posted: February 26th, 2017, 5:15 am
by chili
The first answer on stackoverflow looks like a good solution to me. Create a pointer to hold the buffer and an integral type to hold the size of the buffer. You pass in a pointer to both, and if the buffer pointer is null, that is a signal that the function should allocate a buffer for you. It sets the size variable to the size of the allocated buffer. Then you can use it and all free() on the bufffer pointer at the end to free it.

Re: getenv() may be unsafe?

Posted: February 26th, 2017, 6:16 am
by YoMomIsANiceLady
Yay, okay! I got it working. Thank you!

Re: getenv() may be unsafe?

Posted: February 26th, 2017, 8:21 am
by chili
Good job man :)