Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
memory: fix the potential overflow in a generic manner
Baptiste Daroussin committed 4 years ago
commit 89b90c34b6a570865ec0e59294e9c0ce4b81c1a8
parent 8dd9e28
2 files changed +5 -4
modified libpkg/utils.c
@@ -928,8 +928,9 @@ append_random_suffix(char *buf, int buflen, int suflen)
	char *pos;
	int r;

-
	if (nchars + suflen > buflen - 1) {
-
		suflen = buflen - nchars - 1;
+
	/* 2 being the "." and the \0 */
+
	if (nchars + suflen > buflen - 2) {
+
		suflen = buflen - nchars - 2;
		if (suflen <= 0)
			return;
	}
@@ -978,7 +979,7 @@ hidden_tempfile(char *buf, int buflen, const char *path)
		if (strlen(path) > NAME_MAX - 15)
			prefix = ".";
		snprintf(buf, buflen, "%s%s", prefix, path);
-
		nbuflen = NAME_MAX -1;
+
		nbuflen = NAME_MAX;
	}


modified tests/lib/utils.c
@@ -61,7 +61,7 @@ ATF_TC_BODY(random_suffix, tc) {
	ATF_REQUIRE_EQ_MSG(strlen(buf), 13, "suffix not long enough %lu", strlen(buf));
	snprintf(buf, sizeof(buf), "filename");
	append_random_suffix(buf, sizeof(buf), 12);
-
	ATF_REQUIRE_EQ_MSG(strlen(buf), 14, "suffix not long enough %lu", strlen(buf));
+
	ATF_REQUIRE_EQ_MSG(strlen(buf), 13, "suffix not long enough %lu", strlen(buf));
}

ATF_TP_ADD_TCS(tp)