color: Fix another ip segfault when using --color switch

Commit 959f1428 ("color: add new COLOR_NONE and disable_color function")
introducing color enum COLOR_NONE, which is not only duplicite of
COLOR_CLEAR, but also caused segfault, when running ip with --color
switch, as 'attr + 8' in color_fprintf() access array item out of
bounds. Thus removing it and restoring "magic" offset + 7.

Reproduce with:
$ ip -c a

Signed-off-by: Petr Vorel <petr.vorel@gmail.com>
This commit is contained in:
Petr Vorel 2017-10-13 15:57:17 +02:00 committed by Stephen Hemminger
parent e6849a5722
commit 24b058a2a4
2 changed files with 2 additions and 3 deletions

View File

@ -2,7 +2,6 @@
#define __COLOR_H__ 1 #define __COLOR_H__ 1
enum color_attr { enum color_attr {
COLOR_NONE,
COLOR_IFNAME, COLOR_IFNAME,
COLOR_MAC, COLOR_MAC,
COLOR_INET, COLOR_INET,

View File

@ -104,13 +104,13 @@ int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...)
va_start(args, fmt); va_start(args, fmt);
if (!color_is_enabled || attr == COLOR_NONE) { if (!color_is_enabled || attr == COLOR_CLEAR) {
ret = vfprintf(fp, fmt, args); ret = vfprintf(fp, fmt, args);
goto end; goto end;
} }
ret += fprintf(fp, "%s", ret += fprintf(fp, "%s",
color_codes[attr_colors[is_dark_bg ? attr + 8 : attr]]); color_codes[attr_colors[is_dark_bg ? attr + 7 : attr]]);
ret += vfprintf(fp, fmt, args); ret += vfprintf(fp, fmt, args);
ret += fprintf(fp, "%s", color_codes[C_CLEAR]); ret += fprintf(fp, "%s", color_codes[C_CLEAR]);