Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stb_image_write: warning C4996: ... Consider using sprintf_s ... #1446

Open
etavardt opened this issue Feb 3, 2023 · 7 comments
Open

stb_image_write: warning C4996: ... Consider using sprintf_s ... #1446

etavardt opened this issue Feb 3, 2023 · 7 comments

Comments

@etavardt
Copy link

etavardt commented Feb 3, 2023

During a MS VS C++ build, I get the following warning.

stb_image_write.h(776): warning C4996: 'sprintf': This function or variable may be unsafe. 
Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
See online help for details.

Expected behavior

I expect to see warnings that are relevant to my code without adding _CRT_SECURE_NO_WARNINGS to my build. 
If I did add it I would not see Warnings for my own code as well and I do wish to see them.
@etavardt
Copy link
Author

etavardt commented Feb 5, 2023

My work around is to place:
#define __STDC_LIB_EXT1__
along with:
#define STB_IMAGE_WRITE_IMPLEMENTATION
just before the:
#include <stb/stb_image_write.h>

I don't know if this is a good idea or not but it got rid of the warning. I'll know better once I start implementing my save function.

FYI: keep up the good work.

@N-R-K
Copy link

N-R-K commented Feb 5, 2023

I expect to see warnings that are relevant to my code without adding _CRT_SECURE_NO_WARNINGS to my build.
If I did add it I would not see Warnings for my own code as well and I do wish to see them.

stb libraries try to compile without warnings - so this is not a "solution" but as a temporary workaround - it's typically possible to silence warnings for a section of code via some sort of pragma. GCC and Clang accept the following:

#pragma GCC diagnostic push /* also works on clang */
#pragma GCC diagnostic ignored "-Wunused-function"
#define HEADERLIB_IMPLEMENTATION
#include "headerlib.h"
#pragma GCC diagnostic pop

I'd assume msvc would have similar construct - but I don't use msvc so cannot help you further.


#define __STDC_LIB_EXT1__

AFAIU that macro should be defined by the implementation (i.e libc) if they support Annex K. I don't believe it's something the user is meant to define themselves (?)

@etavardt
Copy link
Author

etavardt commented Feb 14, 2023

You make a good point about not using #define __STDC_LIB_EXT1__

So I went with this:

#define STB_IMAGE_WRITE_IMPLEMENTATION
#pragma warning( push )
#pragma warning(disable:4996)
#include <stb/stb_image_write.h>
#pragma warning( pop )

Maybe you may consider using this wrapper in stb_image_write. as you already have the test condition to use sprintf_s if the requirements are met. Just a suggestion. You may close this issue otherwise if you want or is this something only I can do?

@nothings
Copy link
Owner

We should just use sprintf_s instead as needed. It just requires conditional compilation to support all the compilers, so it requires search and testing to get right.

@dsieger
Copy link

dsieger commented Apr 2, 2023

Getting a similar warning on latest macOS / clang 14.0.3:

stb_image_write.h:776:13: warning: 'sprintf' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]

Would just using snprintf an option or are there concerns regarding compiler / platform support?

Thanks!

@ismagilli
Copy link

ismagilli commented Mar 4, 2024

For macOS / clang I create PR #1619. I hope it will be merged.

@julcst
Copy link

julcst commented May 22, 2024

I have the same issue currently I have to use this very ugly workaround:

#ifndef __STDC_LIB_EXT1__
#define __STDC_LIB_EXT1__
#define sprintf_s snprintf
#endif
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>

@ismagilli I also hope your fix gets merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants