-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
deprecate sprint and replace with snprintf #4494
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
I'll fix if I can get succesful tests. I'm also still missing one sprintf. (Disclaimer; I don't have any OSX machines so I'm really dependent on CI) Not sure what's happening with CI tests now as I'm hardly getting any output. I might still be having the max size wrong though |
Not sure why it says it's not successful. All tests seem fine? |
there are some failures, but it's just the usual flaky tests |
Ok, but where do you see those failures. I only see green checkmarks |
Aargh, my browser is hiding the scrollbar so I never noticed I can scroll down. |
@@ -129,7 +129,8 @@ static std::string make_address_string (const char *hbuf_, | |||
pos += hbuf_len; | |||
memcpy (pos, ipv6_suffix_, sizeof ipv6_suffix_ - 1); | |||
pos += sizeof ipv6_suffix_ - 1; | |||
pos += sprintf (pos, "%d", ntohs (port_)); | |||
pos += snprintf (pos, max_port_str_length + 1 * sizeof (char), "%d", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of snprintf
is the "number of characters (not including the terminating null character) which would have been written to buffer if bufsz
was ignored, or a negative value if an encoding error (for string and character conversion specifiers) occurred". (Arguably this was a misdesign of snprintf
.) So if there were a buffer overflow, pos
would be calculated incorrectly and the call to the std::string
constructor below would still be undefined behaviour.
A better way is to either use snprintf_s
(requires __STDC_WANT_LIB_EXT1__
to be defined before #include <cstdio>
, and C++11), or add an explicit zmq_assert
. I will file a PR shortly, covering all the snprintf
calls in this PR.
Less importantly, sizeof(char) == 1
by definition, so it's pointless (and makes the code no clearer) to multiply by it.
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Replace such calls with calls to a zmq_snprintf helper that asserts in the case of error or truncation. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Replace such calls with calls to a zmq_snprintf helper that asserts in the case of error or truncation. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Replace such calls with calls to a zmq_snprintf helper that asserts in the case of error or truncation. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Replace such calls with calls to a zmq_snprintf helper that asserts in the case of error or truncation. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Replace such calls with calls to a zmq_snprintf helper that asserts in the case of error or truncation. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Replace such calls with snprintf_s. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Replace such calls with snprintf_s. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Replace such calls with snprintf_s. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Signed-off-by: Daira Hopwood <[email protected]>
…at snprintf can truncate, and then return the number of characters that would have been written without truncation. Signed-off-by: Daira Hopwood <[email protected]>
@daira identified an error in zeromq/libzmq#4494 that we reproduced in zcash#6393. This adds @daira's patch from zeromq/libzmq#4507 to ensure we handle this potential case of undefined behavior. Co-authored-by: Daira Hopwood <[email protected]>
#4494 added calls to snprintf, but did not take into account that snprintf can truncate
@daira identified an error in zeromq/libzmq#4494 that we reproduced in zcash/zcash#6393. This adds @daira's patch from zeromq/libzmq#4507 to ensure we handle this potential case of undefined behavior. Co-authored-by: Daira Hopwood <[email protected]>
Problem: Latest OSX deprecates using sprint resulting is failure to build
Fix by switching to snprint.
As tried by #4481 and fixing #4478