Copy one byte less in strncpynt()
PATCH 1/1 Copy one byte less in strncpynt()
While the existing code is not wrong and will never cause an overflow, it will copy (on a too-long source string) "maxlen" bytes to dest, and then overwrite the last byte just copied with "0" - which causes a warning in gcc 9 about filling the target buffer "up to the end, with no room for a trailing 0 anymore".
Reducing the maximum bytes-to-be-copied to "maxlen -1", because the last byte will be stamped with 0 anyway.
--- src/openvpn/buffer.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
src/openvpn/buffer.h +2 -1
@@ -347,7 +347,8 @@ buf_set_read(struct buffer *buf, const uint8_t *data, int size)static inline void strncpynt (char *dest,const char *src,size_t maxlen) { -strncpy (dest, src, maxlen); +ASSERT (maxlen>0 ); +strncpy (dest, src, maxlen-1 );if (maxlen >0 ) { dest[maxlen -1 ] =0 ;
Signature · 1 line
2.18.0
Hi,
Quoted 19 lines · Gert Doering
While the existing code is not wrong and will never cause an overflow, it will copy (on a too-long source string) "maxlen" bytes to dest, and then overwrite the last byte just copied with "0" - which causes a warning in gcc 9 about filling the target buffer "up to the end, with no room for a trailing 0 anymore".
Reducing the maximum bytes-to-be-copied to "maxlen -1", because the last byte will be stamped with 0 anyway.
Signed-off-by Gert Doering <[email protected]...>--- src/openvpn/buffer.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)src/openvpn/buffer.h +0
-0
src/openvpn/buffer.h buf_set_read(struct buffer *buf, const uint8_t *data, int size) · line 352
Quoted 7 lines
src/openvpn/buffer.h +2
-1@@ -347,7 +347,8 @@ buf_set_read(struct buffer *buf, const uint8_t *data, int size)static inline void strncpynt (char *dest,const char *src,size_t maxlen) { -strncpy (dest, src, maxlen); +ASSERT (maxlen>0 ); +strncpy (dest, src, maxlen-1 );if (maxlen >0 )
This if condition makes me think that this function is allowed to be invoked with maxlen == 0. However you are now introducing an ASSERT() which would stop the execution in that case.
Either the ASSERT() is right, and then the if condition should be removed, or the ASSERT() is wrong and should not be introduced.
src/openvpn/buffer.h buf_set_read(struct buffer *buf, const uint8_t *data, int size) · line 354
Quoted 3 lines
src/openvpn/buffer.h +0
-0{ dest[maxlen -1 ] =0 ;
Other than that the change makes sense.
Regards,
Signature · 1 line
Antonio Quartulli
PATCH v2 1/1 Copy one byte less in strncpynt()
In reply to Gert DoeringWhile the existing code is not wrong and will never cause an overflow, it will copy (on a too-long source string) "maxlen" bytes to dest, and then overwrite the last byte just copied with "0" - which causes a warning in gcc 9 about filling the target buffer "up to the end, with no room for a trailing 0 anymore".
Reducing the maximum bytes-to-be-copied to "maxlen -1", because the last byte will be stamped with 0 anyway.
v2: do not ASSERT() on "maxlen == 0", but move the strncpy() call inside the if() clause - so "just do nothing" on maxlen == 0, as before.
--- src/openvpn/buffer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/buffer.h +1 -1
@@ -347,9 +347,9 @@ buf_set_read(struct buffer *buf, const uint8_t *data, int size)static inline void strncpynt (char *dest,const char *src,size_t maxlen) { -strncpy (dest, src, maxlen);if (maxlen >0 ) { +strncpy (dest, src, maxlen-1 ); dest[maxlen -1 ] =0 ; } }
Signature · 1 line
2.21.0
Hi,
Quoted 22 lines · Gert Doering
While the existing code is not wrong and will never cause an overflow, it will copy (on a too-long source string) "maxlen" bytes to dest, and then overwrite the last byte just copied with "0" - which causes a warning in gcc 9 about filling the target buffer "up to the end, with no room for a trailing 0 anymore".
Reducing the maximum bytes-to-be-copied to "maxlen -1", because the last byte will be stamped with 0 anyway.
v2: do not
ASSERT()on "maxlen == 0", but move thestrncpy()call inside theif()clause - so "just do nothing" on maxlen == 0, as before.Signed-off-by Gert Doering <[email protected]...>--- src/openvpn/buffer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)src/openvpn/buffer.h +0
-0
src/openvpn/buffer.h buf_set_read(struct buffer *buf, const uint8_t *data, int size) · line 352
Quoted 7 lines
src/openvpn/buffer.h +1
-1@@ -347,9 +347,9 @@ buf_set_read(struct buffer *buf, const uint8_t *data, int size)static inline void strncpynt (char *dest,const char *src,size_t maxlen) { -strncpy (dest, src, maxlen);if (maxlen >0 ) { +strncpy (dest, src, maxlen-1 );
can you add spaces around the '-' when you commit this change? (basically like the line below)
src/openvpn/buffer.h buf_set_read(struct buffer *buf, const uint8_t *data, int size) · line 355
Quoted 4 lines
src/openvpn/buffer.h +0
-0dest[maxlen -1 ] =0 ; } }
Signature · 1 line
Antonio Quartulli
Patch has been applied to the master branch.
(I have left the "maxlen-1" as it was, as uncrustify says this is fine and I personally find "maxlen - 1" less readable here. I did fix the tab-vs-spaces violation)
commit bebd25a0e3a2aba0b1f98463a87b24db6c419a66
Author: Gert Doering Date: Sat Jun 8 09:56:22 2019 +0200
Copy one byte less in strncpynt()
Signed-off-by: Gert Doering <[email protected]...>
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg18502.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering