diff --git a/src/color.c b/src/color.c
index 75e924326df9cc044a8e022da74246c74ff728c2..39355ca7fe0c7647717468b0c0714e40a3cff88a 100644
--- a/src/color.c
+++ b/src/color.c
@@ -189,11 +189,14 @@ void color_update(void)
     /* If no syntax-override string was specified, or it didn't match,
      * try finding a syntax based on the filename (extension). */
     if (sint == NULL) {
-	char *currentdir = getcwd(NULL, PATH_MAX + 1);
+	char *reserved = charalloc(PATH_MAX + 1);
+	char *currentdir = getcwd(reserved, PATH_MAX + 1);
 	char *joinednames = charalloc(PATH_MAX + 1);
 	char *fullname = NULL;
 
-	if (currentdir != NULL) {
+	if (currentdir == NULL)
+	    free(reserved);
+	else {
 	    /* Concatenate the current working directory with the
 	     * specified filename, and canonicalize the result. */
 	    sprintf(joinednames, "%s/%s", currentdir, openfile->filename);