From: Michael Stapelberg Date: Mon, 3 Aug 2015 17:49:15 +0000 (-0700) Subject: Merge pull request #1789 from shdown/next X-Git-Tag: 4.11~58 X-Git-Url: https://git.sur5r.net/?p=i3%2Fi3;a=commitdiff_plain;h=fdfe4081593268257cd65932bd5353eade45e190;hp=05fb9096362e788f032de6eccea2d7fea03317ab Merge pull request #1789 from shdown/next Use safe wrappers wherever possible --- diff --git a/.travis.yml b/.travis.yml index e3e45656..f90e0ebd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,3 +45,22 @@ script: - CFLAGS="-Wformat -Wformat-security -Wextra -Wno-unused-parameter -Werror" make -j - (cd testcases && xvfb-run ./complete-run.pl --parallel=1 || (cat latest/complete-run.log; false)) - clang-format-3.5 -i $(find . -name "*.[ch]" | tr '\n' ' ') && git diff --exit-code || (echo 'Code was not formatted using clang-format!'; false) + - | + funcs='malloc|calloc|realloc|strdup|strndup|asprintf|write' + cstring='"([^"\\]|\\.)*"' + cchar="'[^\\\\]'|'\\\\.[^']*'" + regex="^([^'\"]|${cstring}|${cchar})*\<(${funcs})\>" + detected=0 + while IFS= read -r file; do + if { cpp -w -fpreprocessed "$file" || exit "$?"; } | grep -E -- "$regex"; then + echo "^ $file calls a function that has a safe counterpart." + detected=1 + fi + done << EOF + $(find -name '*.c' -not -name safewrappers.c -not -name strndup.c) + EOF + if [ "$detected" -ne 0 ]; then + echo + echo "Calls of functions that have safe counterparts were detected." + exit 1 + fi diff --git a/i3-config-wizard/main.c b/i3-config-wizard/main.c index 4c1d9697..bd9aa28a 100644 --- a/i3-config-wizard/main.c +++ b/i3-config-wizard/main.c @@ -392,7 +392,7 @@ static char *rewrite_binding(const char *input) { } } if (walk != beginning) { - char *str = scalloc(walk - beginning + 1); + char *str = scalloc(walk - beginning + 1, 1); /* We copy manually to handle escaping of characters. */ int inpos, outpos; for (inpos = 0, outpos = 0; @@ -768,7 +768,7 @@ int main(int argc, char *argv[]) { switch (o) { case 's': FREE(socket_path); - socket_path = strdup(optarg); + socket_path = sstrdup(optarg); break; case 'v': printf("i3-config-wizard " I3_VERSION "\n"); diff --git a/i3-input/main.c b/i3-input/main.c index 6736aad3..cf3884e9 100644 --- a/i3-input/main.c +++ b/i3-input/main.c @@ -103,7 +103,7 @@ static void restore_input_focus(void) { * */ static uint8_t *concat_strings(char **glyphs, int max) { - uint8_t *output = calloc(max + 1, 4); + uint8_t *output = scalloc(max + 1, 4); uint8_t *walk = output; for (int c = 0; c < max; c++) { printf("at %c\n", glyphs[c][0]); @@ -187,10 +187,10 @@ static void finish_input() { /* allocate space for the output */ int inputlen = strlen(command); - char *full = calloc(1, - strlen(format) - (2 * cnt) /* format without all %s */ - + (inputlen * cnt) /* replaced %s */ - + 1); /* trailing NUL */ + char *full = scalloc(strlen(format) - (2 * cnt) /* format without all %s */ + + (inputlen * cnt) /* replaced %s */ + + 1, /* trailing NUL */ + 1); char *dest = full; for (c = 0; c < len; c++) { /* if this is not % or it is % but without a following 's', @@ -359,7 +359,7 @@ free_resources: } int main(int argc, char *argv[]) { - format = strdup("%s"); + format = sstrdup("%s"); socket_path = getenv("I3SOCK"); char *pattern = sstrdup("pango:monospace 8"); int o, option_index = 0; @@ -381,7 +381,7 @@ int main(int argc, char *argv[]) { switch (o) { case 's': FREE(socket_path); - socket_path = strdup(optarg); + socket_path = sstrdup(optarg); break; case 'v': printf("i3-input " I3_VERSION); @@ -401,11 +401,11 @@ int main(int argc, char *argv[]) { break; case 'f': FREE(pattern); - pattern = strdup(optarg); + pattern = sstrdup(optarg); break; case 'F': FREE(format); - format = strdup(optarg); + format = sstrdup(optarg); break; case 'h': printf("i3-input " I3_VERSION "\n"); diff --git a/i3-msg/main.c b/i3-msg/main.c index 6a6186d8..47e7ae91 100644 --- a/i3-msg/main.c +++ b/i3-msg/main.c @@ -77,7 +77,7 @@ static int reply_boolean_cb(void *params, int val) { } static int reply_string_cb(void *params, const unsigned char *val, size_t len) { - char *str = scalloc(len + 1); + char *str = scalloc(len + 1, 1); strncpy(str, (const char *)val, len); if (strcmp(last_key, "error") == 0) last_reply.error = str; @@ -105,7 +105,7 @@ static int reply_end_map_cb(void *params) { static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) { free(last_key); - last_key = scalloc(keyLen + 1); + last_key = scalloc(keyLen + 1, 1); strncpy(last_key, (const char *)keyVal, keyLen); return 1; } @@ -187,8 +187,7 @@ int main(int argc, char *argv[]) { payload = sstrdup(argv[optind]); } else { char *both; - if (asprintf(&both, "%s %s", payload, argv[optind]) == -1) - err(EXIT_FAILURE, "asprintf"); + sasprintf(&both, "%s %s", payload, argv[optind]); free(payload); payload = both; } diff --git a/i3-nagbar/main.c b/i3-nagbar/main.c index aca70ab1..d86cd69a 100644 --- a/i3-nagbar/main.c +++ b/i3-nagbar/main.c @@ -371,7 +371,7 @@ int main(int argc, char *argv[]) { if (argv0_len > strlen(".nagbar_cmd") && strcmp(argv[0] + argv0_len - strlen(".nagbar_cmd"), ".nagbar_cmd") == 0) { unlink(argv[0]); - cmd = strdup(argv[0]); + cmd = sstrdup(argv[0]); *(cmd + argv0_len - strlen(".nagbar_cmd")) = '\0'; execl("/bin/sh", "/bin/sh", cmd, NULL); err(EXIT_FAILURE, "execv(/bin/sh, /bin/sh, %s)", cmd); @@ -418,7 +418,7 @@ int main(int argc, char *argv[]) { printf("i3-nagbar [-m ] [-b