diff options
author | Amit Kucheria <amit.kucheria@verdurent.com> | 2014-08-29 04:22:23 +0530 |
---|---|---|
committer | Amit Kucheria <amit.kucheria@verdurent.com> | 2014-08-29 10:42:29 +0530 |
commit | 9ecbc807bef4c27fa74765a54e95bd8cfdcf4261 (patch) | |
tree | 6d894ed9cd7b516ac817986dfc3d863cea8c828a | |
parent | 1fca0d9234b5980138686f280620263568327cfb (diff) |
Add filename-checking to ignore bad characters
Checking filename so that "--" doesn't become a filename as in the example
below:
sudo ./idlestat --trace -t 10 -f -- rt-app /tmp/browser.json
Signed-off-by: Amit Kucheria <amit.kucheria@linaro.org>
-rw-r--r-- | idlestat.c | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -52,6 +52,30 @@ static char buffer[BUFSIZE]; +/* I happen to agree with David Wheeler's assertion that Unix filenames + * are too flexible. Eliminate some of the madness. + * http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html + */ +static inline int bad_filename(const char *filename) +{ + const char *c; + + c = filename; + /* Check for first char being '-' */ + if (*c == '-') { + fprintf(stderr, "Bad character '%c' found in filename\n", *c); + return EINVAL; + } + for (; *c; c++) { + /* Check for control chars and other bad characters */ + if (*c < 32 || *c == '<' || *c == '>' || *c == '|') { + fprintf(stderr, "Bad character '%c' found in filename\n", *c); + return EINVAL; + } + } + return 0; +} + static inline int error(const char *str) { perror(str); @@ -1220,6 +1244,10 @@ int getoptions(int argc, char *argv[], struct program_options *options) return -1; } + if (bad_filename(options->filename)) { + return -1; + } + if (options->mode == TRACE) { if (options->duration <= 0) { fprintf(stderr, "expected -t <seconds>\n"); |