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

sprintf deprecation error failing build on macOS #4478

Closed
ghmanoj opened this issue Dec 20, 2022 · 3 comments · Fixed by #4494
Closed

sprintf deprecation error failing build on macOS #4478

ghmanoj opened this issue Dec 20, 2022 · 3 comments · Fixed by #4494

Comments

@ghmanoj
Copy link

ghmanoj commented Dec 20, 2022

Issue description

Build is failing in macOS 13.1

Environment

OS: macOS 13.1
Build Environment:
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

  • libzmq version (commit hash if unreleased): dfbfc59
  • OS: macOS 13.1

Minimal test code / Steps to reproduce the issue

Run the following command after git clone:

  1. ./autogen.sh
  2. ./configure
  3. make

What's the actual result? (include assertion message & call stack if applicable)

make[1]: Nothing to be done for `all'.
CXX src/libzmq_la-tcp_address.lo
src/tcp_address.cpp:132:12: error: '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. [-Werror,-Wdeprecated-declarations]
pos += sprintf (pos, "%d", ntohs (port_));
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:188:1: note: 'sprintf' has been explicitly marked deprecated here
__deprecated_msg("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.")
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro '__deprecated_msg'
#define __deprecated_msg(_msg) attribute((deprecated(_msg)))
^
1 error generated.
make[1]: *** [src/libzmq_la-tcp_address.lo] Error 1
make: *** [all-recursive] Error 1

What's the expected result?

Build should pass

@ghmanoj ghmanoj changed the title sprintf deprecation warning on macOS error while building sprintf deprecation error failing build on macOS Dec 20, 2022
@ghmanoj
Copy link
Author

ghmanoj commented Dec 20, 2022

The patch below fixes the build. It replaces the sprintf(...) with snprintf(...) but not sure this solution is optimal or not?

diff --git a/src/tcp_address.cpp b/src/tcp_address.cpp
index bdda66a2..fcce9c03 100644
--- a/src/tcp_address.cpp
+++ b/src/tcp_address.cpp
@@ -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, sizeof(pos), "%d", ntohs (port_));
+    
     return std::string (buf, pos - buf);
 }
diff --git a/src/udp_engine.cpp b/src/udp_engine.cpp
index d09bfe16..0b097f1f 100644
--- a/src/udp_engine.cpp
+++ b/src/udp_engine.cpp
@@ -368,7 +368,8 @@ void zmq::udp_engine_t::sockaddr_to_msg (zmq::msg_t *msg_,
 
     char port[6];
     const int port_len =
-      sprintf (port, "%d", static_cast<int> (ntohs (addr_->sin_port)));
+      snprintf (port, sizeof (port), "%d", static_cast<int> (ntohs (addr_->sin_port)));
+
     zmq_assert (port_len > 0);
 
     const size_t name_len = strlen (name);
diff --git a/tests/testutil.cpp b/tests/testutil.cpp
index 5306aff9..f9f1eb0c 100644
--- a/tests/testutil.cpp
+++ b/tests/testutil.cpp
@@ -518,7 +518,7 @@ fd_t bind_socket_resolve_port (const char *address_,
         addr_len = sizeof (struct sockaddr_storage);
         TEST_ASSERT_SUCCESS_RAW_ERRNO (
           getsockname (s_pre, (struct sockaddr *) &addr, &addr_len));
-        sprintf (my_endpoint_, "%s://%s:%u",
+        snprintf (my_endpoint_, sizeof(my_endpoint_), "%s://%s:%u",
                  protocol_ == IPPROTO_TCP   ? "tcp"
                  : protocol_ == IPPROTO_UDP ? "udp"
                  : protocol_ == IPPROTO_WSS ? "wss"

@nieder
Copy link

nieder commented Dec 25, 2022

@ghmanoj If you surround the code with lines with three backticks `,
then the formatting will be easier to read.

So like

```diff
first line
-Code diff
+Code diff
bar
```

Looks like

 first line
-Code diff
+Code diff
 bar

@sphaero
Copy link
Contributor

sphaero commented Jan 20, 2023

Seeing the same error on latest MacOS.

  CXX      src/libzmq_la-tcp_address.lo
src/tcp_address.cpp:132:12: error: '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. [-Werror,-Wdeprecated-declarations]
    pos += sprintf (pos, "%d", ntohs (port_));
           ^
/Applications/Xcode_14.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:188:1: note: 'sprintf' has been explicitly marked deprecated here
__deprecated_msg("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.")
^
/Applications/Xcode_14.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro '__deprecated_msg'
        #define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
                                                      ^
1 error generated.

@ghmanoj can you send a pull request?

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