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

tftpd: workaround a stringop-overread warning #11897

Closed
wants to merge 1 commit into from

Conversation

monnerat
Copy link
Contributor

When optimizing, GCC 12 detects and reports a stringop-overread warning:

tftpd.c: In function ‘write_behind.isra’:
tftpd.c:485:12: warning: ‘write’ reading between 1 and 2147483647 bytes from a region of size 0 [-Wstringop-overread]
485 | return write(test->ofile, writebuf, count);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from tftpd.c:71:
/usr/include/arpa/tftp.h:58:30: note: source object ‘tu_data’ of size 0
58 | char tu_data[0]; /* data or error string */
| ^~~~~~~

This occurs because writebuf points to this field and the latter cannot be considered as being of dynamic length because it is not the last field in the structure. Thus it is bounded to its declared size.

This commit explicitly computes the address to unbind it from the declaration.

@bagder
Copy link
Member

bagder commented Sep 20, 2023

Nice catch. But since we also need to have a working private header, can't we instead just use that always and then keep the existing approach since our struct has the field last?

IOW, can this maybe be an alternative fix?

diff --git a/tests/server/tftpd.c b/tests/server/tftpd.c
index 8bcd452d0..75de7f430 100644
--- a/tests/server/tftpd.c
+++ b/tests/server/tftpd.c
@@ -65,15 +65,12 @@
 #include <netinet/in.h>
 #endif
 #ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
 #endif
-#ifdef HAVE_ARPA_TFTP_H
-#include <arpa/tftp.h>
-#else
 #include "tftp.h"
-#endif
+
 #ifdef HAVE_NETDB_H
 #include <netdb.h>
 #endif
 #ifdef HAVE_SYS_FILIO_H
 /* FIONREAD on Solaris 7 */

@monnerat
Copy link
Contributor Author

monnerat commented Sep 20, 2023

IOW, can this maybe be an alternative fix?

Probably, I have to check.
In general, I prefer using system-provided headers when there are some because they are subject to definition changes, but in the tftp case there is almost no chance this can occur !!!

@monnerat
Copy link
Contributor Author

Probably, I have to check.

Checked: OK. No more warning, compiles and runs tftp tests successfully.
It can also be a candidate to drop HAVE_ARPA_TFTP_H.

I let you apply your preferred fix at your convenience.

@bagder
Copy link
Member

bagder commented Sep 20, 2023

Checked: OK. No more warning, compiles and runs tftp tests successfully. It can also be a candidate to drop HAVE_ARPA_TFTP_H.

Yeah, that's my preferred route to go with this.

Using the system's provided arpa/tftp.h and optimizing, GCC 12 detects
and reports a stringop-overread warning:

tftpd.c: In function ‘write_behind.isra’:
tftpd.c:485:12: warning: ‘write’ reading between 1 and 2147483647 bytes from a region of size 0 [-Wstringop-overread]
  485 |     return write(test->ofile, writebuf, count);
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from tftpd.c:71:
/usr/include/arpa/tftp.h:58:30: note: source object ‘tu_data’ of size 0
   58 |                         char tu_data[0];        /* data or error string */
      |                              ^~~~~~~

This occurs because writebuf points to this field and the latter
cannot be considered as being of dynamic length because it is not
the last field in the structure. Thus it is bound to its declared
size.

This commit always uses curl's own version of tftp.h where the
target field is last in its structure, effectively avoiding the
warning.

As HAVE_ARPA_TFTP_H is not used anymore, cmake/configure checks for
arpa/tftp.h are removed.
@monnerat
Copy link
Contributor Author

The commit in this PR has been completely rewritten to always use curl's own tftp.h.

@bagder bagder closed this in bbac7c1 Sep 21, 2023
@bagder
Copy link
Member

bagder commented Sep 21, 2023

Thanks!

@monnerat monnerat deleted the tftpd branch September 21, 2023 08:51
ptitSeb pushed a commit to wasix-org/curl that referenced this pull request Sep 25, 2023
Using the system's provided arpa/tftp.h and optimizing, GCC 12 detects
and reports a stringop-overread warning:

tftpd.c: In function ‘write_behind.isra’:
tftpd.c:485:12: warning: ‘write’ reading between 1 and 2147483647 bytes from a region of size 0 [-Wstringop-overread]
  485 |     return write(test->ofile, writebuf, count);
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from tftpd.c:71:
/usr/include/arpa/tftp.h:58:30: note: source object ‘tu_data’ of size 0
   58 |                         char tu_data[0];        /* data or error string */
      |                              ^~~~~~~

This occurs because writebuf points to this field and the latter
cannot be considered as being of dynamic length because it is not
the last field in the structure. Thus it is bound to its declared
size.

This commit always uses curl's own version of tftp.h where the
target field is last in its structure, effectively avoiding the
warning.

As HAVE_ARPA_TFTP_H is not used anymore, cmake/configure checks for
arpa/tftp.h are removed.

Closes curl#11897
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

2 participants