From b360414fcae0282f7298999117f02a4884346dfc Mon Sep 17 00:00:00 2001 From: Steven Chan Date: Fri, 13 Feb 2015 12:18:39 -0800 Subject: [PATCH] Fix --tab error The error occurs when a tab number greater than one is given, which will cause obconf to crash with a segmentation violation. For example, typing 'obconf --tab 2' on the console will crash insteading of launching a GUI with tab 2 selected. The error is caused by using the MAX macro with a first argument that is not an idempotent expression (file main.c, line number 135): obc_tab = MAX(atoi(argv[++i]) - 1, 0); where MAX is defined in file /usr/include/x86_64-linux-gnu/sys/param.h: #define MAX(a,b) (((a)>(b))?(a):(b)) where the result will be the first argument a if it is more than the second argument b, otherwise the result will be b, ie, the result will be the maximum of a or b. Notice that a or b will be evaluated twice, depending on the > comparison, which means that a and b should always be idempotent expression, assuming that the compiler does not generate code to cache a and b values as intermediate results. In our case, if the argument counter i is zero or one, a will be evaluated once, but if it is two or more, a will be evaluated twice, which means that the argv array will be accessed past its natural end. --- src/main.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 2cbbbd6..d7e3446 100644 --- a/src/main.c +++ b/src/main.c @@ -131,8 +131,11 @@ static void parse_args(int argc, char **argv) else if (!strcmp(argv[i], "--tab")) { if (i == argc - 1) /* no args left */ g_printerr(_("--tab requires an argument\n")); - else - obc_tab = MAX(atoi(argv[++i]) - 1, 0); + else { + obc_tab = atoi(argv[++i]) - 1; + /* tab number should not be negative */ + obc_tab = MAX(obc_tab, 0); + } } else obc_theme_install = argv[i]; -- 2.39.2