View Issue Details

IDProjectCategoryView StatusLast Update
0001688libmicrohttpdotherpublic2011-06-23 09:47
Reporterbplant Assigned ToChristian Grothoff Due Date
PrioritynormalSeverityfeatureReproducibilityalways
Status closedResolutionfixed 
Product Version0.9.5 
Summary0001688: Use strtoul instead of sscanf to reduce code size
DescriptionMHD appears to only use sscanf in 3 locations, each being to parse a number. sscanf requires ~4KB of code space on my embedded cortex M3 platform. 4KB is quite significant when you might only have 128 or 256 KB of code space available. Because sscanf is only used to parse numbers, we can use strtoul or strtoull instead which use significantly less code space. The attached patch (against 0.9.5) implements this change.
TagsNo tags attached.
Attached Files
MHD.patch (2,430 bytes)   
Index: lib/libmicrohttpd/src/daemon/connection.c
===================================================================
--- lib/libmicrohttpd/src/daemon/connection.c	(revision 1099)
+++ lib/libmicrohttpd/src/daemon/connection.c	(working copy)
@@ -1256,6 +1256,7 @@
   int instant_retry;
   int malformed;
   char *buffer_head;
+  char *endptr;
 
   if (connection->response != NULL)
     return;                     /* already queued a response */
@@ -1330,11 +1331,11 @@
               if (!malformed)
                 {
                   buffer_head[i] = '\0';
-                  malformed =
-                    (1 != SSCANF (buffer_head, "%X",
-                                  &connection->current_chunk_size)) &&
-                    (1 != SSCANF (buffer_head, "%x",
-                                  &connection->current_chunk_size));
+                  connection->current_chunk_size = strtoul(buffer_head, &endptr, 16);
+                  if(buffer_head == endptr)
+				    {
+				      malformed = 1;
+				    }
                 }
               if (malformed)
                 {
@@ -1643,6 +1644,7 @@
 parse_connection_headers (struct MHD_Connection *connection)
 {
   const char *clen;
+  char *endptr;
   unsigned MHD_LONG_LONG cval;
   struct MHD_Response *response;
   const char *enc;
@@ -1678,7 +1680,8 @@
                                       MHD_HTTP_HEADER_CONTENT_LENGTH);
   if (clen != NULL)
     {
-      if (1 != SSCANF (clen, "%" MHD_LONG_LONG_PRINTF "u", &cval))
+      cval = strtoull (clen, &endptr, 10);
+	  if (endptr != clen)
         {
 #if HAVE_MESSAGES
           MHD_DLOG (connection->daemon,
Index: lib/libmicrohttpd/src/daemon/internal.c
===================================================================
--- lib/libmicrohttpd/src/daemon/internal.c	(revision 1099)
+++ lib/libmicrohttpd/src/daemon/internal.c	(working copy)
@@ -122,6 +122,8 @@
   char *rpos = val;
   char *wpos = val;
   unsigned int num;
+  char buf[3];
+  char *endptr;
 
   while ('\0' != *rpos)
     {
@@ -133,10 +135,10 @@
 	  rpos++;
 	  break;
 	case '%':
-	  if ( (1 == SSCANF (&rpos[1],
-			     "%2x", &num)) ||
-	       (1 == SSCANF (&rpos[1],
-			     "%2X", &num)) )
+	  strncpy (buf, &rpos[1], 2);
+	  buf[2] = '\0';
+	  strtoul (buf, &endptr, 16);
+	  if (buf != endptr)
 	    {
 	      *wpos = (unsigned char) num;
 	      wpos++;
MHD.patch (2,430 bytes)   

Activities

Christian Grothoff

2011-06-11 11:15

manager   ~0004358

Fixed in SVN 15551.

Issue History

Date ModifiedUsernameFieldChange
2011-06-10 14:08bplantNew Issue
2011-06-10 14:08bplantFile Added: MHD.patch
2011-06-11 05:48Christian GrothoffStatusnew => assigned
2011-06-11 05:48Christian GrothoffAssigned To => Christian Grothoff
2011-06-11 11:15Christian GrothoffNote Added: 0004358
2011-06-11 11:15Christian GrothoffStatusassigned => resolved
2011-06-11 11:15Christian GrothoffResolutionopen => fixed
2011-06-23 09:47Christian GrothoffStatusresolved => closed