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

Why you say snprintf_s is unsafe std C11 function? #35

Closed
chenlilong84 opened this issue Jan 3, 2018 · 4 comments
Closed

Why you say snprintf_s is unsafe std C11 function? #35

chenlilong84 opened this issue Jan 3, 2018 · 4 comments

Comments

@chenlilong84
Copy link

Hi rurban,

  From configure file, '--enable-unsafe'  flag will include unsafe std C11 functions, could you tell me why these functions are unsafe? based on what? thanks!

Regards
chenlilong84

@rurban
Copy link
Owner

rurban commented Jan 3, 2018

See the documentation of these functions. E.g.
https://rurban.github.io/safeclib/doc/safec-3.1/d8/d3c/snprintf__s_8c.html
Instead of being printed, the content is stored in dest. More than dmax - 1 characters might be written, so this variant is unsafe! Always use sprintf_s instead.

With truncation the final \0 might get lost.

@chenlilong84
Copy link
Author

sprintf_s(char * restrict dest, rsize_t dmax, const char * restrict fmt, ...)
snprintf_s(char * restrict dest, rsize_t dmax, const char * restrict fmt, ...)
So you mean sprintf_s is more safer than snprintf_s, because sprintf_s checks argument length should less than dmax, but snprintf_s has no check for it, if argument length is greater than dmax, null-terminated will be lost, right?

@rurban
Copy link
Owner

rurban commented Jan 5, 2018

exactly

@rurban rurban closed this as completed Jan 5, 2018
@rurban
Copy link
Owner

rurban commented Mar 2, 2018

I made the 4 truncating versions now safe, putting a \0 at the end when truncating. See #52

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

No branches or pull requests

2 participants