diff mbox series

[v2] usertools: check 0-division with hugepage size

Message ID 20210321090903.612998-1-thomas@monjalon.net (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers show
Series [v2] usertools: check 0-division with hugepage size | expand

Checks

Context Check Description
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/github-robot success github build: passed
ci/travis-robot success travis build: passed
ci/intel-Testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/Intel-compilation success Compilation OK
ci/checkpatch success coding style OK

Commit Message

Thomas Monjalon March 21, 2021, 9:09 a.m. UTC
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".

Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
Acked-by: Stephen Hemminger <stephen@networkplumber.org>
---
v2: use simple "if not" construct for both None and 0
---
 usertools/dpdk-hugepages.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Burakov, Anatoly March 23, 2021, 11:30 a.m. UTC | #1
On 21-Mar-21 9:09 AM, Thomas Monjalon 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.
> 
> 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".
> 
> Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> Acked-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
> v2: use simple "if not" construct for both None and 0
> ---
Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
Thomas Monjalon March 25, 2021, 5:04 p.m. UTC | #2
23/03/2021 12:30, Burakov, Anatoly:
> On 21-Mar-21 9:09 AM, Thomas Monjalon 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.
> > 
> > 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".
> > 
> > Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
> > Acked-by: Stephen Hemminger <stephen@networkplumber.org>
> > ---
> > v2: use simple "if not" construct for both None and 0
> > ---
> Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>

Applied
diff mbox series

Patch

diff --git a/usertools/dpdk-hugepages.py b/usertools/dpdk-hugepages.py
index fb368b6933..db141b3efa 100755
--- a/usertools/dpdk-hugepages.py
+++ b/usertools/dpdk-hugepages.py
@@ -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()