Skip to content

Commit 7ce4fb9

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 34fa79a commit 7ce4fb9

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

color.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ int color_parse(const char *value, char *dst)
145145
return color_parse_mem(value, strlen(value), dst);
146146
}
147147

148+
void color_set(char *dst, const char *color_bytes)
149+
{
150+
xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
151+
}
152+
148153
/*
149154
* Write the ANSI color codes for "c" to "out"; the string should
150155
* already have the ANSI escape code in it. "out" should have enough

color.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ extern int color_stdout_is_tty;
7575
int git_color_config(const char *var, const char *value, void *cb);
7676
int git_color_default_config(const char *var, const char *value, void *cb);
7777

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+
7885
int git_config_colorbool(const char *var, const char *value);
7986
int want_color(int var);
8087
int color_parse(const char *value, char *dst);

grep.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ void init_grep_defaults(void)
3131
opt->max_depth = -1;
3232
opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
3333
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);
4242
opt->color = -1;
4343
}
4444

@@ -151,14 +151,14 @@ void grep_init(struct grep_opt *opt, const char *prefix)
151151
opt->regflags = def->regflags;
152152
opt->relative = def->relative;
153153

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);
162162
}
163163

164164
void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)

0 commit comments

Comments
 (0)