pathencode: don't use alloca() for safety/portability
authorMatt Mackall <mpm@selenic.com>
Sat, 19 Jan 2013 17:20:39 -0600
changeset 18452 8bd338c7c4c9
parent 18451 d6b3b36f1db2
child 18453 f5fbe15ca744
child 18535 7068089c95a2
pathencode: don't use alloca() for safety/portability
mercurial/pathencode.c
--- a/mercurial/pathencode.c	Sat Jan 19 02:29:56 2013 +0100
+++ b/mercurial/pathencode.c	Sat Jan 19 17:20:39 2013 -0600
@@ -696,22 +696,22 @@
 	return 0;
 }
 
+#define MAXENCODE 4096 * 3
+
 static PyObject *hashencode(const char *src, Py_ssize_t len)
 {
-	const Py_ssize_t baselen = (len - 5) * 3;
-#ifndef _MSC_VER
-	/* alloca is surprisingly slow, so avoid when possible */
-	char dired[baselen];
-	char lowered[baselen];
-	char auxed[baselen];
-#else
-	char *dired = alloca(baselen);
-	char *lowered = alloca(baselen);
-	char *auxed = alloca(baselen);
-#endif
-	Py_ssize_t dirlen, lowerlen, auxlen;
+	char dired[MAXENCODE];
+	char lowered[MAXENCODE];
+	char auxed[MAXENCODE];
+	Py_ssize_t dirlen, lowerlen, auxlen, baselen;
 	char sha[20];
 
+	baselen = (len - 5) * 3;
+	if (baselen >= MAXENCODE) {
+		PyErr_SetString(PyExc_ValueError, "string too long");
+		return NULL;
+	}
+
 	dirlen = _encodedir(dired, baselen, src, len);
 	if (sha1hash(sha, dired, dirlen - 1) == -1)
 		return NULL;