From fdac9fd3f2c1c1d0fc25008a23e8739c95502342 Mon Sep 17 00:00:00 2001 From: Rick Masters Date: Fri, 7 Oct 2022 11:59:57 +0200 Subject: [PATCH] lib: abtol: Fix number digits in hex number conversion. After a letter based digit, number based digits are off. * lib/mes/abtol.c (abtol): Reset "m" for number based digit. * lib/tests/stdlib/70-strtoull.c (main): Test it. * lib/tests/stdlib/90-strtol.c (main): Likewise. --- lib/mes/abtol.c | 3 +++ lib/tests/stdlib/70-strtoull.c | 9 +++++++++ lib/tests/stdlib/90-strtol.c | 19 ++++++++++++------- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/mes/abtol.c b/lib/mes/abtol.c index 1f297b4a..0fafb5c1 100644 --- a/lib/mes/abtol.c +++ b/lib/mes/abtol.c @@ -1,6 +1,7 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software * Copyright © 2016,2017,2018,2019,2020 Jan (janneke) Nieuwenhuizen + * Copyright © 2022 Rick Masters * * This file is part of GNU Mes. * @@ -44,6 +45,8 @@ abtol (char const **p, int base) i = i * base; if (s[0] > '9') m = 'a' - 10; + else + m = '0'; i = i + s[0] - m; s = s + 1; } diff --git a/lib/tests/stdlib/70-strtoull.c b/lib/tests/stdlib/70-strtoull.c index 644a472e..2ef3b2ea 100644 --- a/lib/tests/stdlib/70-strtoull.c +++ b/lib/tests/stdlib/70-strtoull.c @@ -1,6 +1,7 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software * Copyright © 2017 Jan (janneke) Nieuwenhuizen + * Copyright © 2022 Rick Masters * * This file is part of GNU Mes. * @@ -43,5 +44,13 @@ main () if (strcmp (p, "zar\n")) return 4; + p = "a2zar\n"; + n = strtoull (p, (char **) &p, 16); + if (n != 162) + return 5; + eputs (p); + if (strcmp (p, "zar\n")) + return 6; + return 0; } diff --git a/lib/tests/stdlib/90-strtol.c b/lib/tests/stdlib/90-strtol.c index 318ce052..816c51e9 100644 --- a/lib/tests/stdlib/90-strtol.c +++ b/lib/tests/stdlib/90-strtol.c @@ -1,6 +1,7 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * Copyright © 2022 Rick Masters * * This file is part of GNU Mes. * @@ -28,25 +29,29 @@ main () if (strtol ("0x12", 0, 0) != 18) 1; - eputs ("012\n"); - if (strtol ("012", 0, 0) != 10) + eputs ("0xa2\n"); + if (strtol ("0xa2", 0, 0) != 162) 2; - eputs ("-1\n"); - if (strtol ("-1", 0, 0) != -1) + eputs ("012\n"); + if (strtol ("012", 0, 0) != 10) 3; eputs ("-1\n"); - if (strtoul ("-1", 0, 0) != -1) + if (strtol ("-1", 0, 0) != -1) 4; + eputs ("-1\n"); + if (strtoul ("-1", 0, 0) != -1) + 5; + char *p = "16"; int n = strtol (p, (char **) &p, 0); eputs ("p="); eputs (p); eputs ("\n"); if (*p != 0) - return 5; + return 6; p = "0x12"; n = strtol (p, (char **) &p, 0); @@ -54,7 +59,7 @@ main () eputs (p); eputs ("\n"); if (*p != 0) - return 5; + return 7; return 0;