]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
imap: remove the only sscanf() call in the IMAP code
authorDaniel Stenberg <daniel@haxx.se>
Mon, 14 Aug 2023 11:38:09 +0000 (13:38 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 14 Aug 2023 16:07:12 +0000 (18:07 +0200)
Avoids the use of a stack buffer.

Closes #11673

lib/imap.c

index 27b7ac94c314e00aac8bab63511b0f3d7b192716..b8c220569ca5ad4e3bfb48c99c0c385c4c1f8cae 100644 (file)
@@ -1091,10 +1091,19 @@ static CURLcode imap_state_select_resp(struct Curl_easy *data, int imapcode,
 
   if(imapcode == '*') {
     /* See if this is an UIDVALIDITY response */
-    char tmp[20];
-    if(sscanf(line + 2, "OK [UIDVALIDITY %19[0123456789]]", tmp) == 1) {
-      Curl_safefree(imapc->mailbox_uidvalidity);
-      imapc->mailbox_uidvalidity = strdup(tmp);
+    if(checkprefix("OK [UIDVALIDITY ", line + 2)) {
+      size_t len = 0;
+      const char *p = &line[2] + strlen("OK [UIDVALIDITY ");
+      while((len < 20) && p[len] && ISDIGIT(p[len]))
+        len++;
+      if(len && (p[len] == ']')) {
+        struct dynbuf uid;
+        Curl_dyn_init(&uid, 20);
+        if(Curl_dyn_addn(&uid, p, len))
+          return CURLE_OUT_OF_MEMORY;
+        Curl_safefree(imapc->mailbox_uidvalidity);
+        imapc->mailbox_uidvalidity = Curl_dyn_ptr(&uid);
+      }
     }
   }
   else if(imapcode == IMAP_RESP_OK) {