color: add color_set helper for copying raw colors

To set up default colors, we sometimes strcpy() from the default string literals into our color buffers. This isn't a bug (assuming the destination is COLOR_MAXLEN bytes), but makes it harder to audit the code for problematic strcpy calls. Let's introduce a color_set which copies under the assumption that there are COLOR_MAXLEN bytes in the destination (of course you can call it on a smaller buffer, so this isn't providing a huge amount of safety, but it's more convenient than calling xsnprintf yourself). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 24, 2015 at 17:08 UTC 7ce4fb948c6d196a376a37840cb80ae95b5897ec
3 files changed +28 -16
color.c
+5
@@ -145,6 +145,11 @@ int color_parse(const char *value, char *dst)
145 return color_parse_mem(value, strlen(value), dst);
146 }
147
148 +void color_set(char *dst, const char *color_bytes)
149 +{
150 + xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
151 +}
152 +
153 /*
154 * Write the ANSI color codes for "c" to "out"; the string should
155 * already have the ANSI escape code in it. "out" should have enough
color.h
+7
@@ -75,6 +75,13 @@ extern int color_stdout_is_tty;
75 int git_color_config(const char *var, const char *value, void *cb);
76 int git_color_default_config(const char *var, const char *value, void *cb);
77
78 +/*
79 + * Set the color buffer (which must be COLOR_MAXLEN bytes)
80 + * to the raw color bytes; this is useful for initializing
81 + * default color variables.
82 + */
83 +void color_set(char *dst, const char *color_bytes);
84 +
85 int git_config_colorbool(const char *var, const char *value);
86 int want_color(int var);
87 int color_parse(const char *value, char *dst);
grep.c
+16 -16
@@ -31,14 +31,14 @@ void init_grep_defaults(void)
31 opt->max_depth = -1;
32 opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
33 opt->extended_regexp_option = 0;
34 - strcpy(opt->color_context, "");
35 - strcpy(opt->color_filename, "");
36 - strcpy(opt->color_function, "");
37 - strcpy(opt->color_lineno, "");
38 - strcpy(opt->color_match_context, GIT_COLOR_BOLD_RED);
39 - strcpy(opt->color_match_selected, GIT_COLOR_BOLD_RED);
40 - strcpy(opt->color_selected, "");
41 - strcpy(opt->color_sep, GIT_COLOR_CYAN);
34 + color_set(opt->color_context, "");
35 + color_set(opt->color_filename, "");
36 + color_set(opt->color_function, "");
37 + color_set(opt->color_lineno, "");
38 + color_set(opt->color_match_context, GIT_COLOR_BOLD_RED);
39 + color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED);
40 + color_set(opt->color_selected, "");
41 + color_set(opt->color_sep, GIT_COLOR_CYAN);
42 opt->color = -1;
43 }
44
@@ -151,14 +151,14 @@ void grep_init(struct grep_opt *opt, const char *prefix)
151 opt->regflags = def->regflags;
152 opt->relative = def->relative;
153
154 - strcpy(opt->color_context, def->color_context);
155 - strcpy(opt->color_filename, def->color_filename);
156 - strcpy(opt->color_function, def->color_function);
157 - strcpy(opt->color_lineno, def->color_lineno);
158 - strcpy(opt->color_match_context, def->color_match_context);
159 - strcpy(opt->color_match_selected, def->color_match_selected);
160 - strcpy(opt->color_selected, def->color_selected);
161 - strcpy(opt->color_sep, def->color_sep);
154 + color_set(opt->color_context, def->color_context);
155 + color_set(opt->color_filename, def->color_filename);
156 + color_set(opt->color_function, def->color_function);
157 + color_set(opt->color_lineno, def->color_lineno);
158 + color_set(opt->color_match_context, def->color_match_context);
159 + color_set(opt->color_match_selected, def->color_match_selected);
160 + color_set(opt->color_selected, def->color_selected);
161 + color_set(opt->color_sep, def->color_sep);
162 }
163
164 void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)