cURL / Mailing Lists / curl-library / Single Mail

curl-library

[PATCH 1/4] lib: remove some uses of malloc in favor of stack alloc

From: Dave Reisner <d_at_falconindy.com>
Date: Mon, 6 May 2013 14:19:48 -0400

These are all cases where we're alloc'ing a fixed size buffer and not
returning it to the caller, or explicitly avoiding large allocations on
the stack.

The overall effect of this is actually a small net shrinkage in library
size:

$ bloat-o-meter libcurl.so.before libcurl.so.after
add/remove: 1/2 grow/shrink: 0/3 up/down: 4038/-4077 (-39)
function old new delta
static.get_cert_chain - 4038 +4038
Curl_cookie_init 429 404 -25
Curl_cookie_add 3180 3112 -68
static.X509_signature 178 - -178
X509V3_ext 557 - -557
servercert 5476 2227 -3249

---
This sort of change can be applied elsewhere in the lib, but it means
getting involved with the tedium of changing internal struct definitions
and probably some attention with bloat-o-meter to make sure the changes
are worthwhile.
 lib/cookie.c | 40 ++++++++++++++++------------------------
 lib/ssluse.c | 30 +++++++++++-------------------
 2 files changed, 27 insertions(+), 43 deletions(-)
diff --git a/lib/cookie.c b/lib/cookie.c
index a67204e..fad8364 100644
--- a/lib/cookie.c
+++ b/lib/cookie.c
@@ -221,13 +221,7 @@ Curl_cookie_add(struct SessionHandle *data,
     /* This line was read off a HTTP-header */
     const char *ptr;
     const char *semiptr;
-    char *what;
-
-    what = malloc(MAX_COOKIE_LINE);
-    if(!what) {
-      free(co);
-      return NULL;
-    }
+    char what[MAX_COOKIE_LINE];
 
     semiptr=strchr(lineptr, ';'); /* first, find a semicolon */
 
@@ -757,26 +751,24 @@ struct CookieInfo *Curl_cookie_init(struct SessionHandle *data,
   if(fp) {
     char *lineptr;
     bool headerline;
+    char line[MAX_COOKIE_LINE];
 
-    char *line = malloc(MAX_COOKIE_LINE);
-    if(line) {
-      while(fgets(line, MAX_COOKIE_LINE, fp)) {
-        if(checkprefix("Set-Cookie:", line)) {
-          /* This is a cookie line, get it! */
-          lineptr=&line[11];
-          headerline=TRUE;
-        }
-        else {
-          lineptr=line;
-          headerline=FALSE;
-        }
-        while(*lineptr && ISBLANK(*lineptr))
-          lineptr++;
-
-        Curl_cookie_add(data, c, headerline, lineptr, NULL, NULL);
+    while(fgets(line, MAX_COOKIE_LINE, fp)) {
+      if(checkprefix("Set-Cookie:", line)) {
+        /* This is a cookie line, get it! */
+        lineptr=&line[11];
+        headerline=TRUE;
+      }
+      else {
+        lineptr=line;
+        headerline=FALSE;
       }
-      free(line); /* free the line buffer */
+      while(*lineptr && ISBLANK(*lineptr))
+        lineptr++;
+
+      Curl_cookie_add(data, c, headerline, lineptr, NULL, NULL);
     }
+
     if(fromfile)
       fclose(fp);
   }
diff --git a/lib/ssluse.c b/lib/ssluse.c
index a6709d9..14e3f4b 100644
--- a/lib/ssluse.c
+++ b/lib/ssluse.c
@@ -2010,23 +2010,17 @@ static CURLcode get_cert_chain(struct connectdata *conn,
 {
   STACK_OF(X509) *sk;
   int i;
-  char *bufp;
+  char bufp[CERTBUFFERSIZE];
   struct SessionHandle *data = conn->data;
   int numcerts;
 
-  bufp = malloc(CERTBUFFERSIZE);
-  if(!bufp)
-    return CURLE_OUT_OF_MEMORY;
-
   sk = SSL_get_peer_cert_chain(connssl->handle);
   if(!sk) {
-    free(bufp);
     return CURLE_OUT_OF_MEMORY;
   }
 
   numcerts = sk_X509_num(sk);
   if(init_certinfo(data, numcerts)) {
-    free(bufp);
     return CURLE_OUT_OF_MEMORY;
   }
 
@@ -2048,27 +2042,27 @@ static CURLcode get_cert_chain(struct connectdata *conn,
     int j;
     char *ptr;
 
-    (void)x509_name_oneline(X509_get_subject_name(x), bufp, CERTBUFFERSIZE);
+    (void)x509_name_oneline(X509_get_subject_name(x), bufp, sizeof(bufp));
     infof(data, "%2d Subject: %s\n", i, bufp);
     push_certinfo(data, i, "Subject", bufp);
 
-    (void)x509_name_oneline(X509_get_issuer_name(x), bufp, CERTBUFFERSIZE);
+    (void)x509_name_oneline(X509_get_issuer_name(x), bufp, sizeof(bufp));
     infof(data, "   Issuer: %s\n", bufp);
     push_certinfo(data, i, "Issuer", bufp);
 
     value = X509_get_version(x);
     infof(data, "   Version: %lu (0x%lx)\n", value+1, value);
-    snprintf(bufp, CERTBUFFERSIZE, "%lx", value);
+    snprintf(bufp, sizeof(bufp), "%lx", value);
     push_certinfo(data, i, "Version", bufp); /* hex */
 
     num=X509_get_serialNumber(x);
     if(num->length <= 4) {
       value = ASN1_INTEGER_get(num);
       infof(data,"   Serial Number: %ld (0x%lx)\n", value, value);
-      snprintf(bufp, CERTBUFFERSIZE, "%lx", value);
+      snprintf(bufp, sizeof(bufp), "%lx", value);
     }
     else {
-      int left = CERTBUFFERSIZE;
+      int left = sizeof(bufp);
 
       ptr = bufp;
       *ptr++ = 0;
@@ -2092,23 +2086,23 @@ static CURLcode get_cert_chain(struct connectdata *conn,
 
     cinf = x->cert_info;
 
-    j = asn1_object_dump(cinf->signature->algorithm, bufp, CERTBUFFERSIZE);
+    j = asn1_object_dump(cinf->signature->algorithm, bufp, sizeof(bufp));
     if(!j) {
       infof(data, "   Signature Algorithm: %s\n", bufp);
       push_certinfo(data, i, "Signature Algorithm", bufp);
     }
 
     certdate = X509_get_notBefore(x);
-    asn1_output(certdate, bufp, CERTBUFFERSIZE);
+    asn1_output(certdate, bufp, sizeof(bufp));
     infof(data, "   Start date: %s\n", bufp);
     push_certinfo(data, i, "Start date", bufp);
 
     certdate = X509_get_notAfter(x);
-    asn1_output(certdate, bufp, CERTBUFFERSIZE);
+    asn1_output(certdate, bufp, sizeof(bufp));
     infof(data, "   Expire date: %s\n", bufp);
     push_certinfo(data, i, "Expire date", bufp);
 
-    j = asn1_object_dump(cinf->key->algor->algorithm, bufp, CERTBUFFERSIZE);
+    j = asn1_object_dump(cinf->key->algor->algorithm, bufp, sizeof(bufp));
     if(!j) {
       infof(data, "   Public Key Algorithm: %s\n", bufp);
       push_certinfo(data, i, "Public Key Algorithm", bufp);
@@ -2122,7 +2116,7 @@ static CURLcode get_cert_chain(struct connectdata *conn,
       case EVP_PKEY_RSA:
         infof(data,  "   RSA Public Key (%d bits)\n",
               BN_num_bits(pubkey->pkey.rsa->n));
-        snprintf(bufp, CERTBUFFERSIZE, "%d", BN_num_bits(pubkey->pkey.rsa->n));
+        snprintf(bufp, sizeof(bufp), "%d", BN_num_bits(pubkey->pkey.rsa->n));
         push_certinfo(data, i, "RSA Public Key", bufp);
 
         print_pubkey_BN(rsa, n, i);
@@ -2163,8 +2157,6 @@ static CURLcode get_cert_chain(struct connectdata *conn,
     dumpcert(data, x, i);
   }
 
-  free(bufp);
-
   return CURLE_OK;
 }
 
-- 
1.8.2.2
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Received on 2013-05-06