

This patch do several malloc() related things in the C library.

First of all, it adds missing malloc() return checks. Then it fixes a memory
leak in get_cmd where we could return without freeing some memory we
allocated, and then (and probably most important of all) fixes a segmentation
fault that could happen if the server crashed when the client was waiting for
a lock.



---

 cur-root/lib/libold.c |   15 ++++++++++++++-
 1 files changed, 14 insertions(+), 1 deletion(-)

diff -puN lib/libold.c~malloc_check lib/libold.c
--- cur/lib/libold.c~malloc_check	2004-04-27 10:19:26.000000000 -0300
+++ cur-root/lib/libold.c	2004-04-27 11:01:38.000000000 -0300
@@ -66,6 +66,9 @@ struct net_cmd *get_cmd(int fd) {
 		
 	/* parse it */
 	cmd = (struct net_cmd *) malloc(sizeof(struct net_cmd));
+	if (unlikely(cmd == NULL))
+		return NULL;
+
 	memset(cmd, 0, sizeof(struct net_cmd));
 	
 	cmd->ver = buf[0] >> 4;
@@ -74,11 +77,16 @@ struct net_cmd *get_cmd(int fd) {
 		((int) buf[2] << 8) + ((int) buf[3]);
 
 	if (unlikely(cmd->ver != 1 || cmd->len > MAX_PAYLOAD)) {
+		free(cmd);
 		return NULL;
 	}
 	
 	if (likely(cmd->len)) {
 		cmd->payload = (char *) malloc(cmd->len);
+		if (unlikely(cmd->payload == NULL)) {
+			free(cmd);
+			return NULL;
+		}
 		memset(cmd->payload, 0, cmd->len);
 	} else {
 		cmd->payload = NULL;
@@ -92,8 +100,10 @@ struct net_cmd *get_cmd(int fd) {
 	}
 	
 	s = read(fd, cmd->payload, cmd->len);
-	if (unlikely(s != cmd->len))
+	if (unlikely(s != cmd->len)) {
+		free(cmd);
 		return NULL;
+	}
 	
 	/* the command is complete! */
 	return cmd;
@@ -113,6 +123,9 @@ int old_lock(int fd, char *s) {
 		/* if we get an ACK, just wait for a definitive answer */
 		cmd = get_cmd(fd);
 
+	 if (unlikely(cmd == NULL))
+		 return -1;
+
 	if (cmd->op == REP_LOCK_ACQUIRED)
 		return 1;
 	

_
