]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
strto*: fix crash if the pointer to the error pointer is NULL
authorBaptiste Daroussin <bapt@FreeBSD.org>
Sun, 12 Feb 2023 08:48:43 +0000 (09:48 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Sun, 12 Feb 2023 08:51:20 +0000 (09:51 +0100)
src/utils.c
tests/mlmmj.c

index 1f4c245ddce0492b879d4dfbd73bd2c755c8d0b7..ff6a73fe6cdfc120c1ed949a02c6864529510cd7 100644 (file)
@@ -44,27 +44,32 @@ strtoim(const char *np, intmax_t minval, intmax_t maxval, const char **errpp)
        char *endp;
        intmax_t ret;
 
-       *errpp = NULL;
+       if (errpp != NULL)
+               *errpp = NULL;
        if (minval > maxval || np == NULL) {
                errno = EINVAL;
-               *errpp = "invalid";
+               if (errpp != NULL)
+                       *errpp = "invalid";
                return (0);
        }
        errno = 0;
        ret = strtoimax(np, &endp, 10);
        if (endp == np || *endp != '\0') {
                errno = EINVAL;
-               *errpp = "invalid";
+               if (errpp != NULL)
+                       *errpp = "invalid";
                return (0);
        }
        if (ret < minval) {
                errno = ERANGE;
-               *errpp = "too small";
+               if (errpp != NULL)
+                       *errpp = "too small";
                return (0);
        }
        if (errno == ERANGE || ret > maxval) {
                errno = ERANGE;
-               *errpp = "too large";
+               if (errpp != NULL)
+                       *errpp = "too large";
                return (0);
        }
        return (ret);
@@ -84,27 +89,32 @@ strtouim(const char *np, uintmax_t minval, uintmax_t maxval, const char **errpp)
        char *endp;
        uintmax_t ret;
 
-       *errpp = NULL;
+       if (errpp != NULL)
+               *errpp = NULL;
        if (minval > maxval || np == NULL) {
                errno = EINVAL;
-               *errpp = "invalid";
+               if (errpp != NULL)
+                       *errpp = "invalid";
                return (0);
        }
        errno = 0;
        ret = strtoumax(np, &endp, 10);
        if (endp == np || *endp != '\0') {
                errno = EINVAL;
-               *errpp = "invalid";
+               if (errpp != NULL)
+                       *errpp = "invalid";
                return (0);
        }
        if (ret < minval) {
                errno = ERANGE;
-               *errpp = "too small";
+               if (errpp != NULL)
+                       *errpp = "too small";
                return (0);
        }
        if (errno == ERANGE || ret > maxval) {
                errno = ERANGE;
-               *errpp = "too large";
+               if (errpp != NULL)
+                       *errpp = "too large";
                return (0);
        }
        return (ret);
index 062bf34d3eb1828078bdb1a9b60f960e2f946028..a120adab7e580965f67703822fd6f3487b154aed 100644 (file)
@@ -501,7 +501,7 @@ ATF_TC_BODY(strtoim, tc)
        ATF_REQUIRE_EQ(errno, EINVAL);
        ATF_REQUIRE(errp != NULL);
        ATF_REQUIRE_STREQ(errp, "invalid");
-
+       ATF_REQUIRE_EQ(strtoim("ba12", 0, 9, NULL), 0);
 
        ATF_REQUIRE_EQ(strtouim(NULL, 0, 10, &errp), 0);
        ATF_REQUIRE(errp != NULL);
@@ -524,6 +524,7 @@ ATF_TC_BODY(strtoim, tc)
        ATF_REQUIRE_EQ(errno, EINVAL);
        ATF_REQUIRE(errp != NULL);
        ATF_REQUIRE_STREQ(errp, "invalid");
+       ATF_REQUIRE_EQ(strtouim("ba12", 0, 9, NULL), 0);
 }
 
 ATF_TC_BODY(write_ehlo, tc)