nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nano-devel] [patch] matching the extension regex against the full path


From: Benno Schulenberg
Subject: [Nano-devel] [patch] matching the extension regex against the full path
Date: Sun, 26 Apr 2015 14:47:57 +0200

Bug #44288 (https://savannah.gnu.org/bugs/index.php?44288)
reports that the file regex given after the syntax command
operates on the path as given by the user instead of on the
real, absolute path.  If that file regex contains a directory
name, the regex will not match if you're inside that directory,
or have reached it through a symlink.  Below patch canonicalizes
the given path before comparing it with the file regex.  Please
verify that it is doing anything fishy.

Benno


Index: src/color.c
===================================================================
--- src/color.c (revision 5217)
+++ src/color.c (working copy)
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <unistd.h>
 
 #ifdef HAVE_MAGIC_H
 #include <magic.h>
@@ -187,6 +188,18 @@
      * there was no syntax by that name, get the syntax based on the
      * file extension, then try the headerline, and then try magic. */
     if (openfile->colorstrings == NULL) {
+       char *joinednames = charalloc(PATH_MAX + 1);
+       char *fullname = charalloc(PATH_MAX + 1);
+
+       /* Concatenate the current working directory with the specified
+        * filename, and canonicalize the result. */
+       sprintf(joinednames, "%s/%s", getcwd(NULL, PATH_MAX + 1), 
openfile->filename);
+       fullname = realpath(joinednames, NULL);
+       free(joinednames);
+
+       if (!fullname)
+           fullname = mallocstrcpy(fullname, openfile->filename);
+
        for (tmpsyntax = syntaxes; tmpsyntax != NULL;
                tmpsyntax = tmpsyntax->next) {
 
@@ -211,7 +224,7 @@
                }
 
                /* Set colorstrings if we match the extension regex. */
-               if (regexec(e->ext, openfile->filename, 0, NULL, 0) == 0) {
+               if (regexec(e->ext, fullname, 0, NULL, 0) == 0) {
                    openfile->syntax = tmpsyntax;
                    openfile->colorstrings = tmpsyntax->color;
                    break;
@@ -224,6 +237,8 @@
            }
        }
 
+       free(fullname);
+
        /* Check the headerline if the extension didn't match anything. */
        if (openfile->colorstrings == NULL) {
 #ifdef DEBUG

-- 
http://www.fastmail.com - The way an email service should be




reply via email to

[Prev in Thread] Current Thread [Next in Thread]