getenv() may be unsafe?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
YoMomIsANiceLady
Posts: 33
Joined: February 2nd, 2017, 8:20 am

getenv() may be unsafe?

Post by YoMomIsANiceLady » February 26th, 2017, 3:51 am

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?
"Life is like death, but completely different"
- Ivan Gašparovič

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: getenv() may be unsafe?

Post by chili » February 26th, 2017, 5:15 am

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.
Chili

User avatar
YoMomIsANiceLady
Posts: 33
Joined: February 2nd, 2017, 8:20 am

Re: getenv() may be unsafe?

Post by YoMomIsANiceLady » February 26th, 2017, 6:16 am

Yay, okay! I got it working. Thank you!
"Life is like death, but completely different"
- Ivan Gašparovič

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: getenv() may be unsafe?

Post by chili » February 26th, 2017, 8:21 am

Good job man :)
Chili

Post Reply