Skip to contentSkip to conversation

usertools: check 0-division with hugepage size

PATCH Superseded Thomas Monjalon 3 participants 7 messages Feb 11, 2021 – Mar 25, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usertools: check 0-division with hugepage size

The default page size can be None, and the page size from user request can be 0 kB if lower than 1024. In these cases, a division will fail. In order to avoid a Python exception, the page size is checked and an error message "Invalid page size" is printed.

A similar error message is printed in set_hugepages()
if the size is not supported, except at this stage the message can be completed with "Valid page sizes".
Unfortunately the first check is too early to print such information.

A third error message can be printed in a different place (get_memsize) in case of a format issue, e.g. a negative size.
The function get_memsize() is also used for total requested size, so the error message "not a valid page size" was potentially wrong. This message is replaced with the more general "is not a valid size".

---
 usertools/dpdk-hugepages.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
usertools/dpdk-hugepages.py +3 -1
index fb368b6933..c1f2549ec0 100755
@@ -29,7 +29,7 @@ def get_memsize(arg):
     '''Convert memory size with suffix to kB'''
     match = re.match(r'(\d+)([' + BINARY_PREFIX + r']?)$', arg.upper())
     if match is None:
-        sys.exit('{} is not a valid page size'.format(arg))
+        sys.exit('{} is not a valid size'.format(arg))
     num = float(match.group(1))
     suffix = match.group(2)
     if suffix == "":
@@ -254,6 +254,8 @@ def main():
         pagesize_kb = get_memsize(args.pagesize)
     else:
         pagesize_kb = default_pagesize()
+    if pagesize_kb is None or pagesize_kb == 0:
+        sys.exit("Invalid page size: {}kB".format(pagesize_kb))
 
     if args.clear:
         clear_pages()
Signature · 1 line
2.30.0
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · On Thu, 11 Feb 2021 23:05:44 +0100 Thomas Monjalon <[email protected]> wrote:

The default page size can be None, and the page size from user request can be 0 kB if lower than 1024. In these cases, a division will fail. In order to avoid a Python exception, the page size is checked and an error message "Invalid page size" is printed.

A similar error message is printed in set_hugepages()
if the size is not supported, except at this stage the message can be completed with "Valid page sizes".
Unfortunately the first check is too early to print such information.

Quoted 7 lines

A third error message can be printed in a different place (get_memsize) in case of a format issue, e.g. a negative size.
The function get_memsize() is also used for total requested size, so the error message "not a valid page size" was potentially wrong. This message is replaced with the more general "is not a valid size".

#2 lore raw
In reply to Thomas Monjalon
Quoted 35 lines · Thomas Monjalon

The default page size can be None, and the page size from user request can be 0 kB if lower than 1024. In these cases, a division will fail. In order to avoid a Python exception, the page size is checked and an error message "Invalid page size" is printed.

A similar error message is printed in set_hugepages()
if the size is not supported, except at this stage the message can be completed with "Valid page sizes".
Unfortunately the first check is too early to print such information.

A third error message can be printed in a different place (get_memsize) in case of a format issue, e.g. a negative size.
The function get_memsize() is also used for total requested size, so the error message "not a valid page size" was potentially wrong. This message is replaced with the more general "is not a valid size".

---
  usertools/dpdk-hugepages.py | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
usertools/dpdk-hugepages.py +1 -1
index fb368b6933..c1f2549ec0 100755
@@ -29,7 +29,7 @@ def get_memsize(arg):
      '''Convert memory size with suffix to kB'''
      match = re.match(r'(\d+)([' + BINARY_PREFIX + r']?)$', arg.upper())
      if match is None:
-        sys.exit('{} is not a valid page size'.format(arg))
+        sys.exit('{} is not a valid size'.format(arg))
      num = float(match.group(1))
      suffix = match.group(2)
      if suffix == "":

usertools/dpdk-hugepages.py def main(): · line 258

Quoted 5 lines
usertools/dpdk-hugepages.py +2 -0
@@ -254,6 +254,8 @@ def main():
          pagesize_kb = get_memsize(args.pagesize)
      else:
          pagesize_kb = default_pagesize()
+    if pagesize_kb is None or pagesize_kb == 0:
+        sys.exit("Invalid page size: {}kB".format(pagesize_kb))

Both None and 0 evaluate to False for boolean comparisons, so you can replace it with:

if not pagesize_kb:
sys.exit(...)

Quoted 4 lines
usertools/dpdk-hugepages.py +0 -0
      if args.clear:
          clear_pages()
 
Signature · 2 lines
Thanks,
Anatoly
#3 lore raw

12/02/2021 12:27, Burakov, Anatoly:

Quoted 29 lines · Thomas Monjalon
Quoted 19 lines

The default page size can be None, and the page size from user request can be 0 kB if lower than 1024. In these cases, a division will fail. In order to avoid a Python exception, the page size is checked and an error message "Invalid page size" is printed.

A similar error message is printed in set_hugepages()
if the size is not supported, except at this stage the message can be completed with "Valid page sizes".
Unfortunately the first check is too early to print such information.

A third error message can be printed in a different place (get_memsize) in case of a format issue, e.g. a negative size.
The function get_memsize() is also used for total requested size, so the error message "not a valid page size" was potentially wrong. This message is replaced with the more general "is not a valid size".

---

usertools/dpdk-hugepages.py def main(): · line 258

Quoted 5 lines
usertools/dpdk-hugepages.py +2 -0
@@ -254,6 +254,8 @@ def main():
          pagesize_kb = get_memsize(args.pagesize)
      else:
          pagesize_kb = default_pagesize()
+    if pagesize_kb is None or pagesize_kb == 0:
+        sys.exit("Invalid page size: {}kB".format(pagesize_kb))

Both None and 0 evaluate to False for boolean comparisons, so you can replace it with:

Quoted 2 lines

if not pagesize_kb:

    sys.exit(...)

Oh yes better :)

#4 lore raw

PATCH v2 1/1 usertools: check 0-division with hugepage size

In reply to Thomas Monjalon

The default page size can be None, and the page size from user request can be 0 kB if lower than 1024. In these cases, a division will fail. In order to avoid a Python exception, the page size is checked and an error message "Invalid page size" is printed.

A similar error message is printed in set_hugepages()
if the size is not supported, except at this stage the message can be completed with "Valid page sizes".
Unfortunately the first check is too early to print such information.

A third error message can be printed in a different place (get_memsize) in case of a format issue, e.g. a negative size.
The function get_memsize() is also used for total requested size, so the error message "not a valid page size" was potentially wrong. This message is replaced with the more general "is not a valid size".

---
v2: use simple "if not" construct for both None and 0

---
 usertools/dpdk-hugepages.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
usertools/dpdk-hugepages.py +3 -1
index fb368b6933..db141b3efa 100755
@@ -29,7 +29,7 @@ def get_memsize(arg):
     '''Convert memory size with suffix to kB'''
     match = re.match(r'(\d+)([' + BINARY_PREFIX + r']?)$', arg.upper())
     if match is None:
-        sys.exit('{} is not a valid page size'.format(arg))
+        sys.exit('{} is not a valid size'.format(arg))
     num = float(match.group(1))
     suffix = match.group(2)
     if suffix == "":
@@ -254,6 +254,8 @@ def main():
         pagesize_kb = get_memsize(args.pagesize)
     else:
         pagesize_kb = default_pagesize()
+    if not pagesize_kb:
+        sys.exit("Invalid page size: {}kB".format(pagesize_kb))
 
     if args.clear:
         clear_pages()
Signature · 1 line
2.30.1
Acked-by on v2 1/1
#5 lore raw
Quoted 16 lines · Thomas Monjalon

The default page size can be None, and the page size from user request can be 0 kB if lower than 1024. In these cases, a division will fail. In order to avoid a Python exception, the page size is checked and an error message "Invalid page size" is printed.

A similar error message is printed in set_hugepages()
if the size is not supported, except at this stage the message can be completed with "Valid page sizes".
Unfortunately the first check is too early to print such information.

A third error message can be printed in a different place (get_memsize) in case of a format issue, e.g. a negative size.
The function get_memsize() is also used for total requested size, so the error message "not a valid page size" was potentially wrong. This message is replaced with the more general "is not a valid size".

Quoted 5 lines

---
v2: use simple "if not" construct for both None and 0
---

Signature · 2 lines
Thanks,
Anatoly
author Applied
#6 lore raw

23/03/2021 12:30, Burakov, Anatoly:

Quoted 17 lines · Thomas Monjalon
Quoted 16 lines

The default page size can be None, and the page size from user request can be 0 kB if lower than 1024. In these cases, a division will fail. In order to avoid a Python exception, the page size is checked and an error message "Invalid page size" is printed.

A similar error message is printed in set_hugepages()
if the size is not supported, except at this stage the message can be completed with "Valid page sizes".
Unfortunately the first check is too early to print such information.

A third error message can be printed in a different place (get_memsize) in case of a format issue, e.g. a negative size.
The function get_memsize() is also used for total requested size, so the error message "not a valid page size" was potentially wrong. This message is replaced with the more general "is not a valid size".

Quoted 6 lines
Quoted 5 lines

---
v2: use simple "if not" construct for both None and 0
---

Applied