summary refs log tree commit diff
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2024-12-15 00:12:54 +0300
committerNakidai <nakidai@disroot.org>2024-12-15 00:16:01 +0300
commit93c754359d9a52a1e848f1c2ad9603a0c04bf1d7 (patch)
tree9ab8fec0bca016ec49f3f57199574fae70736ec1
parent69cc42f8317843b1f64308ff7a8586ebee58e681 (diff)
downloadlibhttpc-93c754359d9a52a1e848f1c2ad9603a0c04bf1d7.tar.gz
libhttpc-93c754359d9a52a1e848f1c2ad9603a0c04bf1d7.zip
Add bound checking for LibHTTPC_loadRequest
I realized that you don't need to pass buf_len as buf is a c-string.

Anyway, that is bound checking and it should work.
-rw-r--r--TODO.711
-rw-r--r--src/request.c7
2 files changed, 7 insertions, 11 deletions
diff --git a/TODO.7 b/TODO.7
index bfcb305..12a0adb 100644
--- a/TODO.7
+++ b/TODO.7
@@ -19,17 +19,6 @@ from
 .Xr socket 2
 directly.
 .It
-Add bound-checking for
-.Fn LibHTTPC_loadRequest .
-This just needs
-to add a
-.Fa buf_len
-parameter
-and check
-if function
-is righter than
-.Ql buf + buf_len .
-.It
 Make
 .Fn LibHTTPC_dumpResponse
 accept
diff --git a/src/request.c b/src/request.c
index 644cd4b..a0e1358 100644
--- a/src/request.c
+++ b/src/request.c
@@ -9,6 +9,7 @@ struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_b
 {
     char *next;
     int selfmalloc = 0;
+    char *buf_end = strchr(buf, '\0');
 
     if (!buf)
         return NULL;
@@ -50,6 +51,8 @@ struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_b
     if (!next)
         return NULL;
     *next++ = '\0';
+    if (next > buf_end)
+        return NULL;
 
     for (size_t i = 0; (next = strstr(next, "\r\n")); ++next, ++i)
     {
@@ -57,6 +60,8 @@ struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_b
             return NULL;
         *next = '\0';
         next += 2;
+        if (next > buf_end)
+            return NULL;
 
         if (strstr(next, "\r\n") == next)
             break;
@@ -78,6 +83,8 @@ struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_b
             if (!next)
                 return NULL;
             *next++ = '\0';
+            if (next > buf_end)
+                return NULL;
             request_buf->headers[i].value = next;
         }
     }