[PATCH] chg: check snprintf result strictly

Jun Wu quark at fb.com
Wed Jan 11 15:42:57 UTC 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1484149164 -28800
#      Wed Jan 11 23:39:24 2017 +0800
# Node ID 5cbdc769201b22cbc4f698c7bc9235984c7cf427
# Parent  e882c7bb5a0ba2589a44108c9a87b300a13e08df
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 5cbdc769201b
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
@@ -367,7 +367,9 @@ static void readhello(hgclient_t *hgc)
 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");
 }


More information about the Mercurial-devel mailing list