The attached patch fixes this problem.

In this patch I removed the strerror() from the malloc() failure messages.  It 
seems very unlikely that strerror_r() will add any useful information to a 
malloc() failure.  However anything which uses memory has the potential to 
fail after malloc() failure.

The ideal thing to do would be to have a special function to handle malloc() 
failures in which you try to avoid allocating any memory.  Then you can wrap 
malloc(), calloc(), etc with functions such as xmalloc(), xcalloc(), etc and 
the calling code won't even need to display an error message.

I would be happy to implement the ideal solution to this and some other issues 
if you will take over upstream maintenance and make the Debian patches be 
part of an official release.  The Debian patches are becoming difficult to 
manage and I don't want to increase their scope.
diff -ru libcsoap-1.1.0-old/libcsoap/soap-ctx.c libcsoap-1.1.0/libcsoap/soap-ctx.c
--- libcsoap-1.1.0-old/libcsoap/soap-ctx.c	2009-06-15 15:54:02.000000000 +1000
+++ libcsoap-1.1.0/libcsoap/soap-ctx.c	2009-06-15 16:09:06.000000000 +1000
@@ -48,7 +48,7 @@
  
   if (!(ctx = (SoapCtx *) malloc(sizeof(SoapCtx))))
   {
-    log_error2("malloc failed (%s)", strerror(errno));
+    log_error1("malloc failed");
     return NULL;
   }
 
diff -ru libcsoap-1.1.0-old/libcsoap/soap-env.c libcsoap-1.1.0/libcsoap/soap-env.c
--- libcsoap-1.1.0-old/libcsoap/soap-env.c	2006-07-10 02:24:19.000000000 +1000
+++ libcsoap-1.1.0/libcsoap/soap-env.c	2009-06-15 16:09:30.000000000 +1000
@@ -181,7 +181,7 @@
 
   if (!(env = (SoapEnv *) malloc(sizeof(SoapEnv))))
   {
-    log_error2("malloc failed (%s)", strerror(errno));
+    log_error1("malloc failed");
     return herror_new("soap_env_from_doc", GENERAL_INVALID_PARAM, "malloc failed");
   }
 
diff -ru libcsoap-1.1.0-old/libcsoap/soap-router.c libcsoap-1.1.0/libcsoap/soap-router.c
--- libcsoap-1.1.0-old/libcsoap/soap-router.c	2006-07-10 02:24:19.000000000 +1000
+++ libcsoap-1.1.0/libcsoap/soap-router.c	2009-06-15 16:09:17.000000000 +1000
@@ -44,7 +44,7 @@
 
   if (!(router = (SoapRouter *) malloc(sizeof(SoapRouter))))
   {
-    log_error2("malloc failed (%s)", strerror(errno));
+    log_error1("malloc failed");
     return NULL;
   }
   memset(router, 0, sizeof(SoapRouter));
diff -ru libcsoap-1.1.0-old/libcsoap/soap-server.c libcsoap-1.1.0/libcsoap/soap-server.c
--- libcsoap-1.1.0-old/libcsoap/soap-server.c	2006-07-10 02:24:19.000000000 +1000
+++ libcsoap-1.1.0/libcsoap/soap-server.c	2009-06-15 16:09:25.000000000 +1000
@@ -211,7 +211,7 @@
 
   if (!(node = (SoapRouterNode *) malloc(sizeof(SoapRouterNode)))) {
 
-    log_error2("malloc failed (%s)", strerror(errno));
+    log_error1("malloc failed");
     return NULL;
   }
 
diff -ru libcsoap-1.1.0-old/nanohttp/nanohttp-request.c libcsoap-1.1.0/nanohttp/nanohttp-request.c
--- libcsoap-1.1.0-old/nanohttp/nanohttp-request.c	2009-06-15 15:54:02.000000000 +1000
+++ libcsoap-1.1.0/nanohttp/nanohttp-request.c	2009-06-15 15:56:14.000000000 +1000
@@ -52,7 +52,7 @@
  
   if (!(req = (hrequest_t *) malloc(sizeof(hrequest_t)))) {
 
-	  log_error2("malloc failed (%s)", strerror(errno));
+	  log_error1("malloc failed");
 	  return NULL;
   }
 
@@ -175,7 +175,7 @@
           {
             if (!(tmppair = (hpair_t *) malloc(sizeof(hpair_t))))
             {
-              log_error2("malloc failed (%s)", strerror(errno));
+              log_error1("malloc failed");
               return NULL;
             }
 
diff -ru libcsoap-1.1.0-old/nanohttp/nanohttp-response.c libcsoap-1.1.0/nanohttp/nanohttp-response.c
--- libcsoap-1.1.0-old/nanohttp/nanohttp-response.c	2006-07-10 02:24:19.000000000 +1000
+++ libcsoap-1.1.0/nanohttp/nanohttp-response.c	2009-06-15 16:00:43.000000000 +1000
@@ -53,7 +53,7 @@
   /* create response object */
   if (!(res = (hresponse_t *) malloc(sizeof(hresponse_t)))) {
 
-	  log_error2("malloc failed (%s)", strerror(errno));
+	  log_error1("malloc failed");
 	  return NULL;
   }
 
diff -ru libcsoap-1.1.0-old/nanohttp/nanohttp-server.c libcsoap-1.1.0/nanohttp/nanohttp-server.c
--- libcsoap-1.1.0-old/nanohttp/nanohttp-server.c	2006-07-10 02:24:19.000000000 +1000
+++ libcsoap-1.1.0/nanohttp/nanohttp-server.c	2009-06-15 16:00:31.000000000 +1000
@@ -249,7 +249,7 @@
 
   if (!(service = (hservice_t *) malloc(sizeof(hservice_t))))
   {
-    log_error2("malloc failed (%s)", strerror(errno));
+    log_error1("malloc failed");
     return -1;
   }
 
@@ -516,8 +516,7 @@
 
   if (!(conn = (httpd_conn_t *) malloc(sizeof(httpd_conn_t))))
   {
-
-    log_error2("malloc failed (%s)", strerror(errno));
+    log_error1("malloc failed");
     return NULL;
   }
   conn->sock = sock;
@@ -576,7 +575,7 @@
   if (!(tmp = (char *) calloc(1, len)))
   {
 
-    log_error2("calloc failed (%s)", strerror(errno));
+    log_error1("calloc failed");
     return -1;
   }
 
@@ -968,7 +967,10 @@
   pthread_sigmask(SIG_BLOCK, &thrsigset, NULL);
   if ((err =
        pthread_create(&(conn->tid), &(conn->attr), httpd_session_main, conn)))
-    log_error2("pthread_create failed (%s)", strerror(err));
+  {
+    char err_buf[256];
+    log_error2("pthread_create failed (%s)", strerror_r(errno, err_buf, sizeof(err_buf)));
+  }
 #endif
 
   return;
@@ -1158,7 +1160,7 @@
     if (!(postdata = (char *) malloc(1)))
     {
 
-      log_error2("malloc failed (%s)", strerror(errno));
+      log_error1("malloc failed");
       return NULL;
     }
     postdata[0] = '\0';
@@ -1166,7 +1168,7 @@
   }
   if (!(postdata = (unsigned char *) malloc(content_length + 1)))
   {
-    log_error2("malloc failed (%)", strerror(errno));
+    log_error1("malloc failed");
     return NULL;
   }
   if (http_input_stream_read(req->in, postdata, (int) content_length) > 0)
diff -ru libcsoap-1.1.0-old/nanohttp/nanohttp-socket.c libcsoap-1.1.0/nanohttp/nanohttp-socket.c
--- libcsoap-1.1.0-old/nanohttp/nanohttp-socket.c	2009-06-15 15:54:02.000000000 +1000
+++ libcsoap-1.1.0/nanohttp/nanohttp-socket.c	2009-06-15 16:05:13.000000000 +1000
@@ -186,10 +186,11 @@
   int h_errnop;
 #endif
   struct in_addr **h_addr_list;
+  char err_buf[256];
 
   if ((dsock->sock = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
     return herror_new("hsocket_open", HSOCKET_ERROR_CREATE,
-                      "Socket error (%s)", strerror(errno));
+                      "Socket error (%s)", strerror_r(errno, err_buf, sizeof(err_buf)));
 
   /* Get host data */
 #ifdef HAVE_GETHOSTBYNAME_R
@@ -199,7 +200,7 @@
 #endif
   if(!host)
     return herror_new("hsocket_open", HSOCKET_ERROR_GET_HOSTNAME,
-                      "Socket error (%s)", strerror(errno));
+                      "Socket error (%s)", strerror_r(errno, err_buf, sizeof(err_buf)));
 
   h_addr_list = (struct in_addr **)host->h_addr_list;
   address.sin_addr.s_addr = h_addr_list[0]->s_addr;
@@ -216,7 +217,7 @@
   {
     if(errno != EINPROGRESS)
       return herror_new("hsocket_open", HSOCKET_ERROR_CONNECT,
-                      "Socket error (%s)", strerror(errno));
+                      "Socket error (%s)", strerror_r(errno, err_buf, sizeof(err_buf)));
   }
 
   FD_ZERO(&fds);
@@ -252,13 +253,15 @@
   hsocket_t sock;
   struct sockaddr_in addr;
   int opt = 1;
+  char err_buf[256];
 
   /* create socket */
   if ((sock.sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
   {
-    log_error2("Cannot create socket (%s)", strerror(errno));
+    char *tmp = strerror_r(errno, err_buf, sizeof(err_buf));
+    log_error2("Cannot create socket (%s)", tmp);
     return herror_new("hsocket_bind", HSOCKET_ERROR_CREATE,
-                      "Socket error (%s)", strerror(errno));
+                      "Socket error (%s)", tmp);
   }
 
   setsockopt(sock.sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
@@ -271,9 +274,10 @@
   if (bind(sock.sock, (struct sockaddr *) &addr, sizeof(struct sockaddr)) ==
       -1)
   {
-    log_error2("Cannot bind socket (%s)", strerror(errno));
+    char *tmp = strerror_r(errno, err_buf, sizeof(err_buf));
+    log_error2("Cannot bind socket (%s)", tmp);
     return herror_new("hsocket_bind", HSOCKET_ERROR_BIND, "Socket error (%s)",
-                      strerror(errno));
+                      tmp);
   }
   dsock->sock = sock.sock;
   return H_OK;
@@ -294,8 +298,11 @@
     if (sockfd.sock == INVALID_SOCKET)
     {
       if (WSAGetLastError() != WSAEWOULDBLOCK)
+      {
+        char err_buf[256];
         return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT,
-                          "Socket error (%s)", strerror(errno));
+                          "Socket error (%s)", strerror_r(errno, err_buf, sizeof(err_buf)));
+      }
     }
     else
     {
@@ -318,10 +325,11 @@
   if ((dest->sock =
        accept(sock->sock, (struct sockaddr *) &(dest->addr), &len)) == -1)
   {
-    log_warn2("accept failed (%s)", strerror(errno));
+    char err_buf[256];
+    char *tmp = strerror_r(errno, err_buf, sizeof(err_buf));
+    log_warn2("accept failed (%s)", tmp);
     return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT,
-                      "Cannot accept network connection (%s)",
-                      strerror(errno));
+                      "Cannot accept network connection (%s)", tmp);
   }
 
   return H_OK;
@@ -369,9 +377,11 @@
 
   if (listen(sock->sock, 15) == -1)
   {
-    log_error2("listen failed (%s)", strerror(errno));
+    char err_buf[256];
+    char *tmp = strerror_r(errno, err_buf, sizeof(err_buf));
+    log_error2("listen failed (%s)", tmp);
     return herror_new("hsocket_listen", HSOCKET_ERROR_LISTEN,
-                      "Cannot listen on this socket (%s)", strerror(errno));
+                      "Cannot listen on this socket (%s)", tmp);
   }
 
   return H_OK;
diff -ru libcsoap-1.1.0-old/nanohttp/nanohttp-ssl.c libcsoap-1.1.0/nanohttp/nanohttp-ssl.c
--- libcsoap-1.1.0-old/nanohttp/nanohttp-ssl.c	2009-06-15 15:54:02.000000000 +1000
+++ libcsoap-1.1.0/nanohttp/nanohttp-ssl.c	2009-06-15 16:08:54.000000000 +1000
@@ -124,7 +124,11 @@
   case SSL_ERROR_SYSCALL:
     if (ERR_get_error() == 0 && ret == -1)
     {
-      return strerror(errno);
+      /* This code is wrong, it's not clear who owns the memory */
+      /* Things are slightly better now we use strerror_t() but it's still */
+      /* not very good. */
+      char err_buf[256];
+      return strerror_r(errno, err_buf, sizeof(err_buf));
     }
     return "Syscall failed";
   case SSL_ERROR_SSL:
@@ -534,8 +538,11 @@
   else
   {
     if ((count = hsocket_select_read(sock->sock, buf, len)) == -1)
+    {
+      char err_buf[256];
       return herror_new("hssl_read", HSOCKET_ERROR_RECEIVE,
-                        "recv failed (%s)", strerror(errno));
+                        "recv failed (%s)", strerror_r(errno, err_buf, sizeof(err_buf)));
+    }
   }
   *received = count;
 
@@ -573,8 +580,11 @@
   else
   {
     if ((count = send(sock->sock, buf, len, 0)) == -1)
+    {
+      char err_buf[256];
       return herror_new("hssl_write", HSOCKET_ERROR_SEND, "send failed (%s)",
-                        strerror(errno));
+                        strerror_r(errno, err_buf, sizeof(err_buf)));
+    }
   }
   *sent = count;
 
@@ -589,8 +599,11 @@
   int count;
 
   if ((count = hsocket_select_read(sock->sock, buf, len)) == -1)
+  {
+    char err_buf[256];
     return herror_new("hssl_read", HSOCKET_ERROR_RECEIVE, "recv failed (%s)",
-                      strerror(errno));
+                      strerror_r(errno, err_buf, sizeof(err_buf)));
+  }
   *received = count;
   return H_OK;
 }
@@ -615,8 +628,11 @@
     return herror_new("hssl_write", HSOCKET_ERROR_SEND, "select error");
 
   if ((count = send(sock->sock, buf, len, 0)) == -1)
+  {
+    char err_buf[256];
     return herror_new("hssl_write", HSOCKET_ERROR_SEND, "send failed (%s)",
-                      strerror(errno));
+                      strerror_r(errno, err_buf, sizeof(err_buf)));
+  }
   *sent = count;
   return H_OK;
 }
diff -ru libcsoap-1.1.0-old/nanohttp/nanohttp-stream.c libcsoap-1.1.0/nanohttp/nanohttp-stream.c
--- libcsoap-1.1.0-old/nanohttp/nanohttp-stream.c	2006-07-10 02:24:19.000000000 +1000
+++ libcsoap-1.1.0/nanohttp/nanohttp-stream.c	2009-06-15 15:59:02.000000000 +1000
@@ -88,7 +88,7 @@
   /* Create object */
   if (!(result = (http_input_stream_t *) malloc(sizeof(http_input_stream_t))))
   {
-    log_error2("malloc failed (%s)", strerror(errno));
+    log_error1("malloc failed");
     return NULL;
   }
 
@@ -136,16 +136,16 @@
   http_input_stream_t *result;
   FILE *fd;
  
+  char err_buf[256];
   if (!(fd = fopen(filename, "rb"))) {
-
-    log_error2("fopen failed (%s)", strerror(errno));
+    log_error2("fopen failed (%s)", strerror_r(errno, err_buf, sizeof(err_buf)));
     return NULL;
   }
 
   /* Create object */
   if (!(result = (http_input_stream_t *) malloc(sizeof(http_input_stream_t)))) 
   {
-    log_error2("malloc failed (%s)", strerror(errno));
+    log_error1("malloc failed");
     fclose(fd);
     return NULL;
   }
@@ -519,7 +519,8 @@
   /* Create object */
   if (!(result = (http_output_stream_t *) malloc(sizeof(http_output_stream_t))))
   {
-    log_error2("malloc failed (%s)", strerror(errno));
+
+    log_error1("malloc failed");
     return NULL;
   }
 

Reply via email to