HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Verify that a path doesn't contain . or .. or // and doesn't end with /

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pathcontainwithverifydoesnthatendand

Problem

This is actually a function from the first commit of the git source code, for the purpose stated in the title:

static int verify_path(char *path)
{
  char c;

  goto inside;
  for (;;) {
      if (!c)
          return 1;
      if (c == '/') {
inside:
          c = *path++;
          if (c != '/' && c != '.' && c != '\0')
              continue;
          return 0;
      }
      c = *path++;
  }
}


I was checking if there was an alternate way to implement this without the goto statement and ended up with this function:

static int verify_path(char *path)
{
    char c = '/';

    do {
        if (c == '/') {
            c = *path++;
            if (c == '/' || c == '.' || c == '\0')
                return 0;
        }
    } while ((c = *path++));

    return 1;
}


Do you see any problems with the new function? Is it missing anything from the original?

Let us please avoid discussing about goto.

Sample list of invalid path names:

  • /path/to/file



  • ../path/to/file



  • path//to/file



  • path/../to/file



  • path/./to/file



  • path/to/.file



  • path/to/file/

Solution

Much better

I like your version much better than the original. I don't see any cases you missed - it appears to work the same as the original function. The only comment I have is that the path argument should be marked const. But maybe you aren't allowed to change that.

Context

StackExchange Code Review Q#99067, answer score: 4

Revisions (0)

No revisions yet.