diff options
author | Simon Ser <simon.ser@intel.com> | 2019-03-20 13:48:57 +0200 |
---|---|---|
committer | Petri Latvala <petri.latvala@intel.com> | 2019-03-25 08:49:50 +0200 |
commit | 97f1aae9d65e2357d61bdc05e331062f50e2e751 (patch) | |
tree | 11ed135751973f38e67d6adfe9a9e51001441908 /tests/testdisplay.c | |
parent | 8079fea60fba4f4136e0c45f1a26dc31d344b94b (diff) |
tests/testdisplay: fix heap overflow
v2: also simplify the code by using dirname(3).
v3: dirname may modify in-place its argument, duplicate the string
Signed-off-by: Simon Ser <simon.ser@intel.com>
Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>
Diffstat (limited to 'tests/testdisplay.c')
-rw-r--r-- | tests/testdisplay.c | 22 |
1 files changed, 8 insertions, 14 deletions
diff --git a/tests/testdisplay.c b/tests/testdisplay.c index b3657264..67d1b68a 100644 --- a/tests/testdisplay.c +++ b/tests/testdisplay.c @@ -51,6 +51,7 @@ #include <cairo.h> #include <errno.h> #include <getopt.h> +#include <libgen.h> #include <math.h> #include <stdint.h> #include <stdbool.h> @@ -563,24 +564,17 @@ static gboolean input_event(GIOChannel *source, GIOCondition condition, return TRUE; } -static void enter_exec_path( char **argv ) +static void enter_exec_path(const char **argv) { - char *exec_path = NULL; - char *pos = NULL; - short len_path = 0; + char *argv0, *exec_path; int ret; - len_path = strlen( argv[0] ); - exec_path = (char*) malloc(len_path); - - memcpy(exec_path, argv[0], len_path); - pos = strrchr(exec_path, '/'); - if (pos != NULL) - *(pos+1) = '\0'; - + argv0 = strdup(argv[0]); + igt_assert(argv0); + exec_path = dirname(argv0); ret = chdir(exec_path); igt_assert_eq(ret, 0); - free(exec_path); + free(argv0); } static void restore_termio_mode(int sig) @@ -626,7 +620,7 @@ int main(int argc, char **argv) igt_skip_on_simulation(); - enter_exec_path( argv ); + enter_exec_path((const char **) argv); while ((c = getopt_long(argc, argv, optstr, long_opts, NULL)) != -1) { switch (c) { |