changeset 1027:6d68a65b5ec6

[BP-3225a1ba050d] Fix a potential libconnwrap issue
author Mikael Berthe <mikael@lilotux.net>
date Thu, 16 Nov 2006 19:30:12 +0100
parents fc6060707022
children 8569e3535305
files mcabber/connwrap/connwrap.c
diffstat 1 files changed, 22 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/connwrap/connwrap.c	Thu Nov 16 19:30:12 2006 +0100
+++ b/mcabber/connwrap/connwrap.c	Thu Nov 16 19:30:12 2006 +0100
@@ -150,7 +150,11 @@
 
 static sslsock *addsock(int fd) {
     sslsock *p;
-    socks = (sslsock *) realloc(socks, sizeof(sslsock)*++sockcount);
+
+    if (socks)
+	socks = (sslsock *) realloc(socks, sizeof(sslsock)*++sockcount);
+    else
+	socks = (sslsock *) malloc(sizeof(sslsock)*++sockcount);
 
     p = &socks[sockcount-1];
 
@@ -168,21 +172,27 @@
     sslsock *nsocks;
 
     nsockcount = 0;
-    nsocks = (sslsock *) malloc(sizeof(sslsock)*(sockcount-1));
+
+    if (sockcount > 1) {
+	nsocks = (sslsock *) malloc(sizeof(sslsock)*(sockcount-1));
 
-    for(i = 0; i < sockcount; i++) {
-	if(socks[i].fd != fd) {
-	    nsocks[nsockcount++] = socks[i];
-	} else {
-	    SSL_free(socks[i].ssl);
+	for(i = 0; i < sockcount; i++) {
+	    if(socks[i].fd != fd) {
+		nsocks[nsockcount++] = socks[i];
+	    } else {
+		SSL_free(socks[i].ssl);
+	    }
 	}
+
+    } else {
+	if (ctx)
+	    SSL_CTX_free(ctx);
+	ctx = 0;
+	nsocks = 0;
     }
 
-    free(socks);
-
-    SSL_CTX_free(ctx);
-    ctx = 0;
-
+    if (socks)
+	free(socks);
     socks = nsocks;
     sockcount = nsockcount;
 }