]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
base64: accept zero length argument to base64_encode
authorDaniel Stenberg <daniel@haxx.se>
Fri, 19 Sep 2025 11:47:16 +0000 (13:47 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Fri, 19 Sep 2025 20:57:20 +0000 (22:57 +0200)
We used to treat 0 as "call strlen() to get the length" for
curlx_base64_encode, but it turns out this is rather fragile as we
easily do the mistake of passing in zero when the data is actually not
there and then calling strlen() is wrong.

Force the caller to pass in the correct size. A zero length input string
now returns a zero length output and a NULL pointer.

Closes #18617

lib/curlx/base64.c
tests/unit/unit1302.c

index dc6e9c001ce034777aa0261c1a626e30175c3c02..5f6887bd5c4c516b771f6968af7a4e62771dfc85 100644 (file)
@@ -182,7 +182,7 @@ static CURLcode base64_encode(const char *table64,
   *outlen = 0;
 
   if(!insize)
-    insize = strlen(inputbuff);
+    return CURLE_OK;
 
 #if SIZEOF_SIZE_T == 4
   if(insize > UINT_MAX/4)
@@ -240,8 +240,6 @@ static CURLcode base64_encode(const char *table64,
  * encoded data. Size of encoded data is returned in variable pointed by
  * outlen.
  *
- * Input length of 0 indicates input buffer holds a null-terminated string.
- *
  * Returns CURLE_OK on success, otherwise specific error code. Function
  * output shall not be considered valid unless CURLE_OK is returned.
  *
index 54693631ff54f4c65b3f1034acf0fafb1319d63c..a1676699f27d9b5d0e98c1aee5b09fc2cd37d941 100644 (file)
@@ -172,7 +172,7 @@ static CURLcode test_unit1302(const char *arg)
       fprintf(stderr, "Test %u URL encoded output length %d instead of %d\n",
               i, (int)olen, (int)e->olen);
     }
-    if(memcmp(out, e->output, e->olen)) {
+    if(out && memcmp(out, e->output, e->olen)) {
       fprintf(stderr, "Test %u URL encoded badly. Got '%s', expected '%s'\n",
               i, out, e->output);
       unitfail++;