libkmod: fix result of comparison of unsigned enum in kmod_dump_index()
PATCH 1/1 libkmod: fix result of comparison of unsigned enum in kmod_dump_index()
From: Yunseong Kim <[email protected]>
This patch fix compiler warning in kmod_dump_index()
libkmod/libkmod.c:989:11: warning: result of comparison of unsigned enum expression < 0 is always false [-Wtautological-unsigned-enum-zero-compare]
989 | if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE)
| ~~~~ ^ ~
My compiler version.
$ clang -v clang version 18.1.6 (Fedora 18.1.6-3.fc40)
--- libkmod/libkmod.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libkmod/libkmod.c +1 -1
@@ -986,7 +986,7 @@ KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,if (ctx ==NULL )return -ENOSYS ; -if (type <0 || type >= _KMOD_INDEX_MODULES_SIZE) +if (type >=KMOD_INDEX_MODULES_DEP || type >= _KMOD_INDEX_MODULES_SIZE)return -ENOENT ;if (ctx->indexes[type] !=NULL ) {
Signature · 1 line
2.45.2
Hello,
Quoted 7 lines · On Sat, Jun 22, 2024 at 03:13:22PM +0900, [email protected] wrote:
From: Yunseong Kim <[email protected]>
This patch fix compiler warning in
kmod_dump_index()
libkmod/libkmod.c:989:11: warning: result of comparison of unsigned enum expression < 0 is always false [-Wtautological-unsigned-enum-zero-compare]
989 | if (type < 0 || type >=_KMOD_INDEX_MODULES_SIZE)
Quoted 6 lines
| ~~~~ ^ ~My compiler version.
$ clang -v clang version 18.1.6 (Fedora 18.1.6-3.fc40)
If you look eg. here
https://stackoverflow.com/questions/2579230/signedness-of-enum-in-c-c99-c-cx-gnu-c-gnu-c99
you can see that it is not *guaranteed* that the enum is unsigend, and consequently a value lower than zero can be passed in.
Consequently the bug is in your compiler issuing the warning. While in that specific C implementation the value cannot be negative in general it can.
Finally I see that _KMOD_INDEX_MODULES_SIZE is defined separately from the enumeration itself, and would be prone to getting slale if the enumeration is ever updated.
Making it part of the enumeration so it automatically gets the value of last used index type incremented by one would be probably less error-prone. In the kernel there is also a pattern of defining *_LAST as an alias to the last used value in the enumeration, and then _KMOD_INDEX_MODULES_SIZE coud be defined as this last value incremented by 1.
Thanks
Michal
Quoted 11 lines
Signed-off-by Yunseong Kim <[email protected]>--- libkmod/libkmod.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libkmod/libkmod.c +0
-0
libkmod/libkmod.c KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type, · line 989
Quoted 5 lines
libkmod/libkmod.c +1
-1@@ -986,7 +986,7 @@ KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,if (ctx ==NULL )return -ENOSYS ; -if (type <0 || type >= _KMOD_INDEX_MODULES_SIZE) +if (type >=KMOD_INDEX_MODULES_DEP || type >= _KMOD_INDEX_MODULES_SIZE)
Why are you adding a duplicate check here?
Quoted 6 lines
libkmod/libkmod.c +0
-0return -ENOENT ;if (ctx->indexes[type] !=NULL ) {Signature · 1 line
2.45.2
Hi Michal,
Quoted 38 lines · Michal Suchánek
Hello,
Quoted 7 lines · On Sat, Jun 22, 2024 at 03:13:22PM +0900, [email protected] wrote:
From: Yunseong Kim <[email protected]>
This patch fix compiler warning in
kmod_dump_index()
libkmod/libkmod.c:989:11: warning: result of comparison of unsigned enum expression < 0 is always false [-Wtautological-unsigned-enum-zero-compare]
989 | if (type < 0 || type >=_KMOD_INDEX_MODULES_SIZE)Quoted 6 lines
| ~~~~ ^ ~My compiler version.
$ clang -v clang version 18.1.6 (Fedora 18.1.6-3.fc40)If you look eg. here
https://stackoverflow.com/questions/2579230/signedness-of-enum-in-c-c99-c-cx-gnu-c-gnu-c99you can see that it is not *guaranteed* that the enum is unsigend, and consequently a value lower than zero can be passed in.
Consequently the bug is in your compiler issuing the warning. While in that specific C implementation the value cannot be negative in general it can.
Finally I see that
_KMOD_INDEX_MODULES_SIZEis defined separately from the enumeration itself, and would be prone to getting slale if the enumeration is ever updated.Making it part of the enumeration so it automatically gets the value of last used index type incremented by one would be probably less error-prone. In the kernel there is also a pattern of defining
*_LASTas an alias to the last used value in the enumeration, and then_KMOD_INDEX_MODULES_SIZEcoud be defined as this last value incremented by 1.
Quoted 3 lines
Thanks
Michal
Thank you for the code review Michal, I understand your point. Would it be acceptable if we don't need to make any further adjustments to the warning?
Quoted 13 lines
Quoted 13 lines
Signed-off-by Yunseong Kim <[email protected]>--- libkmod/libkmod.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libkmod/libkmod.c +0
-0@@ -986,7 +986,7 @@ KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,if (ctx ==NULL )return -ENOSYS ;
Quoted 4 lines
libkmod/libkmod.c KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type, · line 989
Quoted 2 lines
libkmod/libkmod.c +1
-1-if (type <0 || type >= _KMOD_INDEX_MODULES_SIZE) +if (type >=KMOD_INDEX_MODULES_DEP || type >= _KMOD_INDEX_MODULES_SIZE)Why are you adding a duplicate check here?
I also think my code is really wrong. I'm sorry.
Quoted 6 lines
Quoted 6 lines
libkmod/libkmod.c +0
-0return -ENOENT ;if (ctx->indexes[type] !=NULL ) {Signature · 1 line
2.45.2
Warm regards,
Yunseong Kim
Hello,
Quoted 41 lines · Yunseong Kim
Hi Michal,
Quoted 38 lines · Michal Suchánek
Hello,
Quoted 7 lines · On Sat, Jun 22, 2024 at 03:13:22PM +0900, [email protected] wrote:
From: Yunseong Kim <[email protected]>
This patch fix compiler warning in
kmod_dump_index()
libkmod/libkmod.c:989:11: warning: result of comparison of unsigned enum expression < 0 is always false [-Wtautological-unsigned-enum-zero-compare]
989 | if (type < 0 || type >=_KMOD_INDEX_MODULES_SIZE)Quoted 6 lines
| ~~~~ ^ ~My compiler version.
$ clang -v clang version 18.1.6 (Fedora 18.1.6-3.fc40)If you look eg. here
https://stackoverflow.com/questions/2579230/signedness-of-enum-in-c-c99-c-cx-gnu-c-gnu-c99you can see that it is not *guaranteed* that the enum is unsigend, and consequently a value lower than zero can be passed in.
Consequently the bug is in your compiler issuing the warning. While in that specific C implementation the value cannot be negative in general it can.
Finally I see that
_KMOD_INDEX_MODULES_SIZEis defined separately from the enumeration itself, and would be prone to getting slale if the enumeration is ever updated.Making it part of the enumeration so it automatically gets the value of last used index type incremented by one would be probably less error-prone. In the kernel there is also a pattern of defining
*_LASTas an alias to the last used value in the enumeration, and then_KMOD_INDEX_MODULES_SIZEcoud be defined as this last value incremented by 1.
Quoted 7 lines
Quoted 3 lines
Thanks
Michal
Thank you for the code review Michal, I understand your point. Would it be acceptable if we don't need to make any further adjustments to the warning?
As already said the warning should be adjusted on the compiler side. That is either report the problem to the compiler authors so they can fix it or disable the warning with a compiler option if it's bothering you.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
libkmod/libkmod.c +0
-0@@ -986,7 +986,7 @@ KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,if (ctx ==NULL )return -ENOSYS ;
Quoted 6 lines
Quoted 4 lines
libkmod/libkmod.c KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type, · line 989
Quoted 2 lines
libkmod/libkmod.c +1
-1-if (type <0 || type >= _KMOD_INDEX_MODULES_SIZE) +if (type >=KMOD_INDEX_MODULES_DEP || type >= _KMOD_INDEX_MODULES_SIZE)Why are you adding a duplicate check here?
I also think my code is really wrong. I'm sorry.
These two are defined in different files, so it's understandable that you might get confused about their relationship. Defining them in one place could make the code more robust and easier to understand. It would, however, not address the warning. It's a separate problem related to this enum definition.
Thanks
Michal
Quoted 70 lines · Michal Suchánek
Hello,
Quoted 41 lines · Yunseong Kim
Hi Michal,
Quoted 38 lines · Michal Suchánek
Hello,
Quoted 7 lines · On Sat, Jun 22, 2024 at 03:13:22PM +0900, [email protected] wrote:
From: Yunseong Kim <[email protected]>
This patch fix compiler warning in
kmod_dump_index()
libkmod/libkmod.c:989:11: warning: result of comparison of unsigned enum expression < 0 is always false [-Wtautological-unsigned-enum-zero-compare]
989 | if (type < 0 || type >=_KMOD_INDEX_MODULES_SIZE)Quoted 6 lines
| ~~~~ ^ ~My compiler version.
$ clang -v clang version 18.1.6 (Fedora 18.1.6-3.fc40)If you look eg. here
https://stackoverflow.com/questions/2579230/signedness-of-enum-in-c-c99-c-cx-gnu-c-gnu-c99you can see that it is not *guaranteed* that the enum is unsigend, and consequently a value lower than zero can be passed in.
Consequently the bug is in your compiler issuing the warning. While in that specific C implementation the value cannot be negative in general it can.
Finally I see that
_KMOD_INDEX_MODULES_SIZEis defined separately from the enumeration itself, and would be prone to getting slale if the enumeration is ever updated.Making it part of the enumeration so it automatically gets the value of last used index type incremented by one would be probably less error-prone. In the kernel there is also a pattern of defining
*_LASTas an alias to the last used value in the enumeration, and then_KMOD_INDEX_MODULES_SIZEcoud be defined as this last value incremented by 1.Quoted 7 lines
Quoted 3 lines
Thanks
Michal
Thank you for the code review Michal, I understand your point. Would it be acceptable if we don't need to make any further adjustments to the warning?
As already said the warning should be adjusted on the compiler side. That is either report the problem to the compiler authors so they can fix it or disable the warning with a compiler option if it's bothering you.
Quoted 13 lines
Quoted 12 lines
libkmod/libkmod.c KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type, · line 989
Quoted 10 lines
libkmod/libkmod.c +1
-1@@ -986,7 +986,7 @@ KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,if (ctx ==NULL )return -ENOSYS ; -if (type <0 || type >= _KMOD_INDEX_MODULES_SIZE) +if (type >=KMOD_INDEX_MODULES_DEP || type >= _KMOD_INDEX_MODULES_SIZE)Why are you adding a duplicate check here?
Quoted 7 lines
Quoted 1 line
I also think my code is really wrong. I'm sorry.
These two are defined in different files, so it's understandable that you might get confused about their relationship. Defining them in one place could make the code more robust and easier to understand. It would, however, not address the warning. It's a separate problem related to this enum definition.
yes, but the main issue is that libkmod/libkmod.h is the header exposed to the users, making it harder to maintain API compatibility since the value exposed would be changing.
Lucas De Marchi
Quoted 4 lines
Thanks
Michal