# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1484149164 -28800
# Node ID 1f9684fe94cc6cef4d4510a67e167e5586cbb06e
# Parent  0fbb3a5c188e4240e44358429e64dafe23216e7d
chg: check snprintf result strictly

This makes the program more robust when somebody changes hgclient's
maxdatasize in the future.

diff --git a/contrib/chg/hgclient.c b/contrib/chg/hgclient.c
--- a/contrib/chg/hgclient.c
+++ b/contrib/chg/hgclient.c
@@ -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");
 }