Skip to contentSkip to conversation

memfd: Avoid Coverity warning about integer overflow

PATCH Applied Peter Maydell 4 participants 4 messages May 15, 2018 – May 16, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memfd: Avoid Coverity warning about integer overflow

Coverity complains about qemu_memfd_create() (CID 1385858) because we calculate a bit position htsize which could be up to 63, but then use it in "1 << htsize" which is a 32-bit integer calculation and could push the 1 off the top of the value.

Silence the complaint bu using "1ULL"; this isn't a bug in practice since a hugetlbsize of 4GB is not very plausible.

---
 util/memfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/memfd.c +1 -1
index b3ecbac19e..d248a53c3c 100644
@@ -66,7 +66,7 @@ int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
 {
     int htsize = hugetlbsize ? ctz64(hugetlbsize) : 0;
 
-    if (htsize && 1 << htsize != hugetlbsize) {
+    if (htsize && 1ULL << htsize != hugetlbsize) {
         error_setg(errp, "Hugepage size must be a power of 2");
         return -1;
     }
Signature · 1 line
2.17.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Peter Maydell

Coverity complains about qemu_memfd_create() (CID 1385858) because we calculate a bit position htsize which could be up to 63, but then use it in "1 << htsize" which is a 32-bit integer calculation and could push the 1 off the top of the value.

Silence the complaint bu using "1ULL"; this isn't a bug in practice since a hugetlbsize of 4GB is not very plausible.

Thanks Peter for the fix,

Quoted 21 lines
---
 util/memfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/memfd.c +1 -2
index b3ecbac19e..d248a53c3c 100644
@@ -66,7 +66,7 @@ int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
 {
     int htsize = hugetlbsize ? ctz64(hugetlbsize) : 0;
 
-    if (htsize && 1 << htsize != hugetlbsize) {
+    if (htsize && 1ULL << htsize != hugetlbsize) {
         error_setg(errp, "Hugepage size must be a power of 2");
         return -1;
     }
--

2.17.0

Signature · 1 line
Marc-André Lureau
Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

Coverity complains about qemu_memfd_create() (CID 1385858) because we calculate a bit position htsize which could be up to 63, but then use it in "1 << htsize" which is a 32-bit integer calculation and could push the 1 off the top of the value.

Silence the complaint bu using "1ULL"; this isn't a bug in practice since a hugetlbsize of 4GB is not very plausible.

Quoted 9 lines
---
 util/memfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/memfd.c +0 -0
index b3ecbac19e..d248a53c3c 100644

util/memfd.c int qemu_memfd_create(const char *name, size_t size, bool hugetlb, · line 72

Quoted 8 lines
util/memfd.c +1 -1
@@ -66,7 +66,7 @@ int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
 {
     int htsize = hugetlbsize ? ctz64(hugetlbsize) : 0;
 
-    if (htsize && 1 << htsize != hugetlbsize) {
+    if (htsize && 1ULL << htsize != hugetlbsize) {
         error_setg(errp, "Hugepage size must be a power of 2");
         return -1;
     }

--
Alex Bennée

#3 lore raw
In reply to Peter Maydell
Quoted 19 lines · Peter Maydell

Coverity complains about qemu_memfd_create() (CID 1385858) because we calculate a bit position htsize which could be up to 63, but then use it in "1 << htsize" which is a 32-bit integer calculation and could push the 1 off the top of the value.

Silence the complaint bu using "1ULL"; this isn't a bug in practice since a hugetlbsize of 4GB is not very plausible.

---
 util/memfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/memfd.c +0 -0
index b3ecbac19e..d248a53c3c 100644

util/memfd.c int qemu_memfd_create(const char *name, size_t size, bool hugetlb, · line 72

Quoted 8 lines
util/memfd.c +1 -1
@@ -66,7 +66,7 @@ int qemu_memfd_create(const char *name, size_t size, bool hugetlb,
 {
     int htsize = hugetlbsize ? ctz64(hugetlbsize) : 0;
 
-    if (htsize && 1 << htsize != hugetlbsize) {
+    if (htsize && 1ULL << htsize != hugetlbsize) {
         error_setg(errp, "Hugepage size must be a power of 2");
         return -1;
     }

Queued, thanks.

Paolo