浏览代码

ctl: proper handling of realloc failure to avoid leak

Daniel-Constantin Mierla 8 年之前
父节点
当前提交
72e0ebcdcb
共有 1 个文件被更改,包括 4 次插入2 次删除
  1. 4 2
      src/modules/ctl/fifo_server.c

+ 4 - 2
src/modules/ctl/fifo_server.c

@@ -1277,6 +1277,7 @@ static int rpc_rpl_printf(rpc_ctx_t* ctx, char* fmt, ...)
 {
 	int n, buf_size;
 	char* buf;
+	char* buf0;
 	va_list ap;
 	str s;
 	struct text_chunk* l;
@@ -1287,7 +1288,7 @@ static int rpc_rpl_printf(rpc_ctx_t* ctx, char* fmt, ...)
 		ERR("No memory left\n");
 		return -1;
 	}
-	
+
 	buf_size = RPC_BUF_SIZE;
 	while (1) {
 		     /* Try to print in the allocated space. */
@@ -1314,11 +1315,12 @@ static int rpc_rpl_printf(rpc_ctx_t* ctx, char* fmt, ...)
 		} else {          /* glibc 2.0 */
 			buf_size *= 2;  /* twice the old size */
 		}
-		if ((buf = ctl_realloc(buf, buf_size)) == 0) {
+		if ((buf0 = ctl_realloc(buf, buf_size)) == 0) {
 			rpc_fault(ctx, 500, "Internal Server Error (No memory left)");
 			ERR("No memory left\n");
 			goto err;
 		}
+		buf = buf0;
 	}
 	return 0;
  err: