chg: check snprintf result strictly
authorJun Wu <quark@fb.com>
Wed, 11 Jan 2017 23:39:24 +0800
changeset 30756 1f9684fe94cc
parent 30755 0fbb3a5c188e
child 30757 511a4bf52754
chg: check snprintf result strictly This makes the program more robust when somebody changes hgclient's maxdatasize in the future.
contrib/chg/hgclient.c
--- a/contrib/chg/hgclient.c	Tue Jan 10 09:32:27 2017 +0100
+++ b/contrib/chg/hgclient.c	Wed Jan 11 23:39:24 2017 +0800
@@ -366,9 +366,11 @@
 
 static void updateprocname(hgclient_t *hgc)
 {
-	size_t n = (size_t)snprintf(hgc->ctx.data, hgc->ctx.maxdatasize,
+	int r = snprintf(hgc->ctx.data, hgc->ctx.maxdatasize,
 			"chg[worker/%d]", (int)getpid());
-	hgc->ctx.datasize = n;
+	if (r < 0 || (size_t)r >= hgc->ctx.maxdatasize)
+		abortmsg("insufficient buffer to write procname (r = %d)", r);
+	hgc->ctx.datasize = (size_t)r;
 	writeblockrequest(hgc, "setprocname");
 }