Skip to content

Silence compiler warnings#1361

Closed
thyssentishman wants to merge 1 commit intosolvespace:masterfrom
thyssentishman:master
Closed

Silence compiler warnings#1361
thyssentishman wants to merge 1 commit intosolvespace:masterfrom
thyssentishman:master

Conversation

@thyssentishman
Copy link
Copy Markdown

@thyssentishman thyssentishman commented Apr 20, 2023

When compiling with clang 13.0.0 on OpenBSD the following warning is shown:

warning: sprintf() is often misused, please use snprintf()

This commit suggests replacing all instances of sprintf() with snprintf() to silence the above mentioned compiler warning.

@thyssentishman
Copy link
Copy Markdown
Author

Just noticed in the diff file that my editor removed some trailing spaces. Harmless, but let me know if you want them back haha :)

@thyssentishman thyssentishman changed the title Replace sprintf with snprintf to silence warnings Silence compiler warnings Apr 21, 2023
@phkahler
Copy link
Copy Markdown
Member

If rand() is actually deprecated I think it would be better to replace it in the source rather than do something special just for BSD. I don't think we care too much about the quality of the random numbers, but speed and thread safety are important.

@thyssentishman
Copy link
Copy Markdown
Author

thyssentishman commented Apr 24, 2023

If rand() is actually deprecated I think it would be better to replace it in the source rather than do something special just for BSD. I don't think we care too much about the quality of the random numbers, but speed and thread safety are important.

I don't believe its deprecated, just preferred. It seems that OpenBSD modified these functions to provide more secure non-deterministic values. Since I don't know comparable and portable alternatives I think we'd be better sticking to rand (which is apparently a wrapper to arc4random in OpenBSD anyways) and we can just ignore the compiler warning.

@phkahler
Copy link
Copy Markdown
Member

I don't want the target specific # stuff in the cpp files like that. @ruevs or @rpavlik what's the best place for that? Or should we just replace use of rand() on all platforms? I'm not sure that's right either - we want reproducible results, so rand actually preferable for us.

@ruevs
Copy link
Copy Markdown
Member

ruevs commented May 22, 2023

@phkahler I wouldn't merge either of these.

@rpavlik
Copy link
Copy Markdown
Contributor

rpavlik commented May 22, 2023

snprintf is better than sprintf, but it's also commonly misused, see https://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/ Ideally we do what is suggested there and have a wrapper auto-deduce buffer size and do it right in one place.

Agree that we want determinism so the rand change is not useful here: we are not relying on the random for anything security-related afaik.

@neonkore
Copy link
Copy Markdown

I'd be curious about real use of determinism here if you're calling a random function then swapping results. sprintf() misuse is even worse than snprintf() misuse also. So code is awesome from a OS portability view if your primary target is windows.

@ruevs
Copy link
Copy Markdown
Member

ruevs commented May 31, 2023

I'd be curious about real use of determinism here if you're calling a random function then swapping results.

The rand function does not generate a random number. It generates a pseudo random number. If the seed is the same the sequence of pseudo random numbers will be the same. This allows bugs that may depend on the random sequence to be reproduced.

a28bfac#r1174520181

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

Successfully merging this pull request may close these issues.

5 participants