Skip to content

Commit

Permalink
Merge pull request #1176 from tdesveauxPKFX/host/fix_normalize
Browse files Browse the repository at this point in the history
path.normalize: Fix when call with path surrounded with quotes
  • Loading branch information
samsinsane authored Oct 12, 2018
2 parents 0601ec6 + f68c386 commit ea1df50
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/host/path_normalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
#include <string.h>

#define IS_SEP(__c) ((__c) == '/' || (__c) == '\\')
#define IS_QUOTE(__c) ((__c) == '\"' || (__c) == '\'')

#define IS_UPPER_ALPHA(__c) ((__c) >= 'A' && (__c) <= 'Z')
#define IS_LOWER_ALPHA(__c) ((__c) >= 'a' && (__c) <= 'z')
#define IS_ALPHA(__c) (IS_UPPER_ALPHA(__c) || IS_LOWER_ALPHA(__c))

#define IS_SPACE(__c) ((__c >= '\t' && __c <= '\r') || __c == ' ')

static void* normalize_substring(const char* srcPtr, const char* srcEnd, char* dstPtr) {

static void* normalize_substring(const char* srcPtr, const char* srcEnd, char* dstPtr)
{
#define IS_END(__p) (__p >= srcEnd || *__p == '\0')
#define IS_SEP_OR_END(__p) (IS_END(__p) || IS_SEP(*__p))

Expand Down Expand Up @@ -114,6 +115,14 @@ int path_normalize(lua_State* L)
while (*endPtr && !IS_SPACE(*endPtr))
++endPtr;

// path is surrounded with quotes
if (readPtr != endPtr &&
IS_QUOTE(*readPtr) && IS_QUOTE(endPtr[-1]) &&
*readPtr == endPtr[-1])
{
*(writePtr++) = *(readPtr++);
}

writePtr = normalize_substring(readPtr, endPtr, writePtr);

// skip any white spaces between sub paths
Expand Down
5 changes: 5 additions & 0 deletions tests/base/test_path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,8 @@
test.isequal("//myawesomeserver/test", path.normalize("//myawesomeserver/test/"))
test.isequal("//myawesomeserver/test", path.normalize("///myawesomeserver/test/"))
end

function suite.normalize_quotedpath()
test.isequal("\"../../test/test/\"", path.normalize("\"../../test/test/\""))
test.isequal("\"../../test/\"", path.normalize("\"../../test/../test/\""))
end

0 comments on commit ea1df50

Please sign in to comment.