From 1e04deb6677538e39ee3aaf95d92101e6372bb2b Mon Sep 17 00:00:00 2001 From: Simon Goldschmidt Date: Thu, 15 May 2014 20:50:22 +0200 Subject: [PATCH] makefsdata.c: minimal check if given path is OK --- apps/httpserver_raw/makefsdata/makefsdata.c | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/apps/httpserver_raw/makefsdata/makefsdata.c b/apps/httpserver_raw/makefsdata/makefsdata.c index 44e1a7d..8e5c551 100644 --- a/apps/httpserver_raw/makefsdata/makefsdata.c +++ b/apps/httpserver_raw/makefsdata/makefsdata.c @@ -100,6 +100,7 @@ int file_write_http_header(FILE *data_file, const char *filename, int file_size, int file_put_ascii(FILE *file, const char *ascii_string, int len, int *i); int s_put_ascii(char *buf, const char *ascii_string, int len, int *i); void concat_files(const char *file1, const char *file2, const char *targetfile); +int check_path(char* path, size_t size); static unsigned char file_buffer_raw[COPY_BUFSIZE]; /* 5 bytes per char + 3 bytes per line */ @@ -160,6 +161,11 @@ int main(int argc, char *argv[]) } } + if(!check_path(path, sizeof(path))) { + printf("Invalid path: \"%s\"." NEWLINE, path); + exit(-1); + } + GETCWD(appPath, MAX_PATH_LEN); /* if command line param or subdir named 'fs' not found spout usage verbiage */ if (!CHDIR_SUCCEEDED(CHDIR(path))) { @@ -248,6 +254,29 @@ int main(int argc, char *argv[]) return 0; } +int check_path(char* path, size_t size) +{ + size_t slen; + if (path[0] == 0) { + /* empty */ + return 0; + } + slen = strlen(path); + if (slen >= size) { + /* not NULL-terminated */ + return 0; + } + while ((slen > 0) && ((path[slen] == '\\') || (path[slen] == '/'))) { + /* path should not end with trailing backslash */ + path[slen] = 0; + slen--; + } + if (slen == 0) { + return 0; + } + return 1; +} + static void copy_file(const char *filename_in, FILE *fout) { FILE *fin;