dhcp-util: Fix warning on signed pointer systems
PATCH 1/1 dhcp-util: Fix warning on signed pointer systems
Some systems treat pointers as signed values, causing the following error:
ell/dhcp-util.c: In function ‘_dhcp_message_builder_append’:ell/dhcp-util.c:55:49: error: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Werror=sign-compare]
55 | if ((builder)->pos - (builder)->start + (next) > (builder)->max) \
| ^ell/dhcp-util.c:65:3: note: in expansion of macro ‘LEN_CHECK’
65 | LEN_CHECK(builder, 1);
| ^~~~~~~~~
---
ell/dhcp-util.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)ell/dhcp-util.c +2 -1
@@ -52,7 +52,8 @@ static void dhcp_init_header(struct dhcp_message *message, uint8_t type) }#define LEN_CHECK (builder, next) \ - if ((builder)->pos - (builder)->start + (next) > (builder)->max) \ + if ((unsigned)((builder)->pos - (builder)->start + (next)) > \ + (builder)->max) \ return false; bool _dhcp_message_builder_append(struct dhcp_message_builder *builder,
Signature · 1 line
2.25.4
Hi Brian,
Quoted 32 lines · Brian Gix
Some systems treat pointers as signed values, causing the following error:
ell/dhcp-util.c: In function ‘_dhcp_message_builder_append’:ell/dhcp-util.c:55:49: error: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Werror=sign-compare]
55 | if ((builder)->pos - (builder)->start + (next) > (builder)-Quoted 1 line
max) \
| ^
ell/dhcp-util.c:65:3: note: in expansion of macro ‘LEN_CHECK’65 | LEN_CHECK(builder, 1); | ^~~~~~~~~ --- ell/dhcp-util.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)ell/dhcp-util.c +2
-1@@ -52,7 +52,8 @@ static void dhcp_init_header(struct dhcp_message *message, uint8_t type) }#define LEN_CHECK (builder, next) \ - if ((builder)->pos - (builder)->start + (next) > (builder)- >max) \ + if ((unsigned)((builder)->pos - (builder)->start + (next)) > \ + (builder)->max) \return false;
Quoted 2 lines
bool _dhcp_message_builder_append(struct
dhcp_message_builder
*builder,
I actually just fixed this (for me at least) just by passing '1U' to LEN_CHECK so the compiler treats it as unsigned. Looks like its at the HEAD of ELL, 3d096702. See if this works for you.
Thanks,
James
Hi Brian,
Quoted 2 lines · Brian Gix
Some systems treat pointers as signed values, causing the following error:
That doesn't seem right. The issue is that pointer arithmetic is a signed type and the macro assumes that next is unsigned, so the temporary resulting in the operation left of '>' gets promoted to unsigned type. This breaks if a signed type is passed in as 'next' argument of the macro. In theory this should already be fixed by 3d096702811d ("dhcp-util: fix GCC 10 warning").
There may be systems that have ptrdiff_t be a non-standard type, so a really paranoid fix would be an explicit cast, as you have it here...
Quoted 12 lines
ell/dhcp-util.c: In function ‘_dhcp_message_builder_append’:ell/dhcp-util.c:55:49: error: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Werror=sign-compare]55 | if ((builder)->pos - (builder)->start + (next) > (builder)->max) \ | ^ ell/dhcp-util.c:65:3: note: in expansion of macro ‘LEN_CHECK’ 65 | LEN_CHECK(builder, 1); | ^~~~~~~~~ --- ell/dhcp-util.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Regards,
-Denis
Gix
Quoted 11 lines · Denis Kenzior
Hi Brian,
Quoted 2 lines · Brian Gix
Some systems treat pointers as signed values, causing the following error:
That doesn't seem right. The issue is that pointer arithmetic is a signed type and the macro assumes that next is unsigned, so the temporary resulting in the operation left of '>' gets promoted to unsigned type. This breaks if a signed type is passed in as 'next' argument of the macro. In theory this should already be fixed by
3d096702811d("dhcp-util: fix GCC 10 warning").
Yes, I can confirm that James' commit 3d096702811d fixes this problem on 32 bit Fedora-30 runnint GCC version 9.3.1
Sorry for the redundent fix.
Quoted 20 lines
There may be systems that have
ptrdiff_tbe a non-standard type, so a really paranoid fix would be an explicit cast, as you have it here...Quoted 12 lines
ell/dhcp-util.c: In function ‘_dhcp_message_builder_append’:ell/dhcp-util.c:55:49: error: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Werror=sign-compare]55 | if ((builder)->pos - (builder)->start + (next) > (builder)->max) \ | ^ ell/dhcp-util.c:65:3: note: in expansion of macro ‘LEN_CHECK’ 65 | LEN_CHECK(builder, 1); | ^~~~~~~~~ --- ell/dhcp-util.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)Regards,
-Denis