property: convert integers to strings properly.
authorPauli <pauli@openssl.org>
Fri, 21 May 2021 01:19:30 +0000 (11:19 +1000)
committerPauli <pauli@openssl.org>
Sat, 22 May 2021 05:30:26 +0000 (15:30 +1000)
The int64_t type was converted to int (truncation).
Negative values were not handled at all.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15396)

crypto/property/property_parse.c
test/property_test.c

index aab8cbe8a408e11b1e38013be23fdd0628a60a5f..6352def8609c1555567e585774f1e075ddc7374b 100644 (file)
@@ -658,11 +658,15 @@ static void put_str(const char *str, char **buf, size_t *remain, size_t *needed)
     }
 }
 
-static void put_num(int val, char **buf, size_t *remain, size_t *needed)
+static void put_num(int64_t val, char **buf, size_t *remain, size_t *needed)
 {
-    int tmpval = val;
+    int64_t tmpval = val;
     size_t len = 1;
 
+    if (tmpval < 0) {
+        len++;
+        tmpval = -tmpval;
+    }
     for (; tmpval > 9; len++, tmpval /= 10);
 
     *needed += len;
@@ -670,7 +674,7 @@ static void put_num(int val, char **buf, size_t *remain, size_t *needed)
     if (*remain == 0)
         return;
 
-    BIO_snprintf(*buf, *remain, "%d", val);
+    BIO_snprintf(*buf, *remain, "%lld", (long long int)val);
     if (*remain < len) {
         *buf += *remain;
         *remain = 0;
index 94540bc7769d1da8ed8af6bdaed02c130a3bafe6..6cc8eec138ab492aef7f5a34e95d81b0252fa221 100644 (file)
@@ -435,54 +435,51 @@ err:
     return ret;
 }
 
-static int test_property_list_to_string(void)
+static struct {
+    const char *in;
+    const char *out;
+} to_string_tests[] = {
+    { "fips=yes", "fips=yes" },
+    { "fips!=yes", "fips!=yes" },
+    { "fips = yes", "fips=yes" },
+    { "fips", "fips=yes" },
+    { "fips=no", "fips=no" },
+    { "-fips", "-fips" },
+    { "?fips=yes", "?fips=yes" },
+    { "fips=yes,provider=fips", "fips=yes,provider=fips" },
+    { "fips = yes , provider = fips", "fips=yes,provider=fips" },
+    { "fips=yes,provider!=fips", "fips=yes,provider!=fips" },
+    { "fips=yes,?provider=fips", "fips=yes,?provider=fips" },
+    { "fips=yes,-provider", "fips=yes,-provider" },
+      /* foo is an unknown internal name */
+    { "foo=yes,fips=yes", "fips=yes"},
+    { "", "" },
+    { "fips=3", "fips=3" },
+    { "fips=-3", "fips=-3" },
+    { NULL, "" }
+};
+
+static int test_property_list_to_string(int i)
 {
     OSSL_PROPERTY_LIST *pl = NULL;
     int ret = 0;
-    struct props_list_str {
-        const char *in;
-        const char *out;
-    } props[] = {
-        { "fips=yes", "fips=yes" },
-        { "fips!=yes", "fips!=yes" },
-        { "fips = yes", "fips=yes" },
-        { "fips", "fips=yes" },
-        { "fips=no", "fips=no" },
-        { "-fips", "-fips" },
-        { "?fips=yes", "?fips=yes" },
-        { "fips=yes,provider=fips", "fips=yes,provider=fips" },
-        { "fips = yes , provider = fips", "fips=yes,provider=fips" },
-        { "fips=yes,provider!=fips", "fips=yes,provider!=fips" },
-        { "fips=yes,?provider=fips", "fips=yes,?provider=fips" },
-        { "fips=yes,-provider", "fips=yes,-provider" },
-          /* foo is an unknown internal name */
-        { "foo=yes,fips=yes", "fips=yes"},
-        { "", "" },
-        { NULL, "" }
-    };
-    size_t i, bufsize;
+    size_t bufsize;
     char *buf = NULL;
 
-    for (i = 0; i < OSSL_NELEM(props); i++) {
-        if (props[i].in != NULL
-                && !TEST_ptr(pl = ossl_parse_query(NULL, props[i].in, 1)))
-            goto err;
-        bufsize = ossl_property_list_to_string(NULL, pl, NULL, 0);
-        if (!TEST_size_t_gt(bufsize, 0))
-            goto err;
-        buf = OPENSSL_malloc(bufsize);
-        if (!TEST_ptr(buf)
-                || !TEST_size_t_eq(ossl_property_list_to_string(NULL, pl, buf,
-                                                                bufsize),
-                                   bufsize)
-                || !TEST_str_eq(props[i].out, buf)
-                || !TEST_size_t_eq(bufsize, strlen(props[i].out) + 1))
-            goto err;
-        OPENSSL_free(buf);
-        buf = NULL;
-        ossl_property_free(pl);
-        pl = NULL;
-    }
+    if (to_string_tests[i].in != NULL
+            && !TEST_ptr(pl = ossl_parse_query(NULL, to_string_tests[i].in, 1)))
+        goto err;
+    bufsize = ossl_property_list_to_string(NULL, pl, NULL, 0);
+    if (!TEST_size_t_gt(bufsize, 0))
+        goto err;
+    buf = OPENSSL_malloc(bufsize);
+    if (!TEST_ptr(buf)
+            || !TEST_size_t_eq(ossl_property_list_to_string(NULL, pl, buf,
+                                                            bufsize),
+                               bufsize)
+            || !TEST_str_eq(to_string_tests[i].out, buf)
+            || !TEST_size_t_eq(bufsize, strlen(to_string_tests[i].out) + 1))
+        goto err;
 
     ret = 1;
  err:
@@ -503,6 +500,6 @@ int setup_tests(void)
     ADD_TEST(test_property);
     ADD_TEST(test_query_cache_stochastic);
     ADD_TEST(test_fips_mode);
-    ADD_TEST(test_property_list_to_string);
+    ADD_ALL_TESTS(test_property_list_to_string, OSSL_NELEM(to_string_tests));
     return 1;
 }