Browse Source

libdpkg: Fix buffer size limit handling in path_quote_filename

Fix an off-by-one error on size limit NUL termination outside the loop,
and thus make sure then that size is always > 0 so that we don't write
outside the bounds. Check there's enough room when quoting '\\', and
terminate the string and return otherwise. Remove XXX comments now that
the code works fine.
Guillem Jover 16 years ago
parent
commit
1021129285
1 changed files with 11 additions and 2 deletions
  1. 11 2
      lib/dpkg/path.c

+ 11 - 2
lib/dpkg/path.c

@@ -110,12 +110,20 @@ path_quote_filename(char *dst, const char *src, size_t n)
 	char *r = dst;
 	ssize_t size = (ssize_t)n;
 
+	if (size == 0)
+		return r;
+
 	while (size > 0) {
 		switch (*src) {
 		case '\0':
 			*dst = '\0';
 			return r;
 		case '\\':
+			if (size <= 2) {
+				/* Buffer full. */
+				*dst = '\0';
+				return r;
+			}
 			*dst++ = '\\';
 			*dst++ = '\\';
 			src++;
@@ -134,13 +142,14 @@ path_quote_filename(char *dst, const char *src, size_t n)
 					src++;
 				} else {
 					/* Buffer full. */
-					*dst = '\0'; /* XXX */
+					*dst = '\0';
 					return r;
 				}
 			}
 		}
 	}
-	*dst = '\0'; /* XXX */
+	/* Buffer full. */
+	*(dst - 1) = '\0';
 
 	return r;
 }