Skip to content

Commit d59f765

Browse files
peffgitster
authored andcommitted
use sha1_to_hex_r() instead of strcpy
Before sha1_to_hex_r() existed, a simple way to get hex sha1 into a buffer was with: strcpy(buf, sha1_to_hex(sha1)); This isn't wrong (assuming the buf is 41 characters), but it makes auditing the code base for bad strcpy() calls harder, as these become false positives. Let's convert them to sha1_to_hex_r(), and likewise for some calls to find_unique_abbrev(). While we're here, we'll double-check that all of the buffers are correctly sized, and use the more obvious GIT_SHA1_HEXSZ constant. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f063d38 commit d59f765

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

builtin/blame.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,9 +1867,9 @@ static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent,
18671867
int cnt;
18681868
const char *cp;
18691869
struct origin *suspect = ent->suspect;
1870-
char hex[41];
1870+
char hex[GIT_SHA1_HEXSZ + 1];
18711871

1872-
strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1872+
sha1_to_hex_r(hex, suspect->commit->object.sha1);
18731873
printf("%s %d %d %d\n",
18741874
hex,
18751875
ent->s_lno + 1,
@@ -1905,11 +1905,11 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
19051905
const char *cp;
19061906
struct origin *suspect = ent->suspect;
19071907
struct commit_info ci;
1908-
char hex[41];
1908+
char hex[GIT_SHA1_HEXSZ + 1];
19091909
int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
19101910

19111911
get_commit_info(suspect->commit, &ci, 1);
1912-
strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1912+
sha1_to_hex_r(hex, suspect->commit->object.sha1);
19131913

19141914
cp = nth_line(sb, ent->lno);
19151915
for (cnt = 0; cnt < ent->num_lines; cnt++) {

builtin/merge-index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ static int merge_entry(int pos, const char *path)
99
{
1010
int found;
1111
const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
12-
char hexbuf[4][60];
12+
char hexbuf[4][GIT_SHA1_HEXSZ + 1];
1313
char ownbuf[4][60];
1414

1515
if (pos >= active_nr)
@@ -22,7 +22,7 @@ static int merge_entry(int pos, const char *path)
2222
if (strcmp(ce->name, path))
2323
break;
2424
found++;
25-
strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
25+
sha1_to_hex_r(hexbuf[stage], ce->sha1);
2626
xsnprintf(ownbuf[stage], sizeof(ownbuf[stage]), "%o", ce->ce_mode);
2727
arguments[stage] = hexbuf[stage];
2828
arguments[stage + 4] = ownbuf[stage];

builtin/merge.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,13 +1319,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13191319
if (verify_signatures) {
13201320
for (p = remoteheads; p; p = p->next) {
13211321
struct commit *commit = p->item;
1322-
char hex[41];
1322+
char hex[GIT_SHA1_HEXSZ + 1];
13231323
struct signature_check signature_check;
13241324
memset(&signature_check, 0, sizeof(signature_check));
13251325

13261326
check_commit_signature(commit, &signature_check);
13271327

1328-
strcpy(hex, find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
1328+
find_unique_abbrev_r(hex, commit->object.sha1, DEFAULT_ABBREV);
13291329
switch (signature_check.result) {
13301330
case 'G':
13311331
break;
@@ -1415,15 +1415,15 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
14151415
/* Again the most common case of merging one remote. */
14161416
struct strbuf msg = STRBUF_INIT;
14171417
struct commit *commit;
1418-
char hex[41];
14191418

1420-
strcpy(hex, find_unique_abbrev(head_commit->object.sha1, DEFAULT_ABBREV));
1421-
1422-
if (verbosity >= 0)
1423-
printf(_("Updating %s..%s\n"),
1424-
hex,
1425-
find_unique_abbrev(remoteheads->item->object.sha1,
1426-
DEFAULT_ABBREV));
1419+
if (verbosity >= 0) {
1420+
char from[GIT_SHA1_HEXSZ + 1], to[GIT_SHA1_HEXSZ + 1];
1421+
find_unique_abbrev_r(from, head_commit->object.sha1,
1422+
DEFAULT_ABBREV);
1423+
find_unique_abbrev_r(to, remoteheads->item->object.sha1,
1424+
DEFAULT_ABBREV);
1425+
printf(_("Updating %s..%s\n"), from, to);
1426+
}
14271427
strbuf_addstr(&msg, "Fast-forward");
14281428
if (have_message)
14291429
strbuf_addstr(&msg,

builtin/receive-pack.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,11 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
10711071
const char *dst_name;
10721072
struct string_list_item *item;
10731073
struct command *dst_cmd;
1074-
unsigned char sha1[20];
1075-
char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
1074+
unsigned char sha1[GIT_SHA1_RAWSZ];
1075+
char cmd_oldh[GIT_SHA1_HEXSZ + 1],
1076+
cmd_newh[GIT_SHA1_HEXSZ + 1],
1077+
dst_oldh[GIT_SHA1_HEXSZ + 1],
1078+
dst_newh[GIT_SHA1_HEXSZ + 1];
10761079
int flag;
10771080

10781081
strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
@@ -1103,10 +1106,10 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
11031106

11041107
dst_cmd->skip_update = 1;
11051108

1106-
strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
1107-
strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
1108-
strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
1109-
strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
1109+
find_unique_abbrev_r(cmd_oldh, cmd->old_sha1, DEFAULT_ABBREV);
1110+
find_unique_abbrev_r(cmd_newh, cmd->new_sha1, DEFAULT_ABBREV);
1111+
find_unique_abbrev_r(dst_oldh, dst_cmd->old_sha1, DEFAULT_ABBREV);
1112+
find_unique_abbrev_r(dst_newh, dst_cmd->new_sha1, DEFAULT_ABBREV);
11101113
rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
11111114
" its target '%s' (%s..%s)",
11121115
cmd->ref_name, cmd_oldh, cmd_newh,

builtin/rev-list.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static void print_var_int(const char *var, int val)
217217
static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
218218
{
219219
int cnt, flags = info->flags;
220-
char hex[41] = "";
220+
char hex[GIT_SHA1_HEXSZ + 1] = "";
221221
struct commit_list *tried;
222222
struct rev_info *revs = info->revs;
223223

@@ -242,7 +242,7 @@ static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
242242
cnt = reaches;
243243

244244
if (revs->commits)
245-
strcpy(hex, sha1_to_hex(revs->commits->item->object.sha1));
245+
sha1_to_hex_r(hex, revs->commits->item->object.sha1);
246246

247247
if (flags & BISECT_SHOW_ALL) {
248248
traverse_commit_list(revs, show_commit, show_object, info);

diff.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ static struct diff_tempfile {
322322
*/
323323
const char *name;
324324

325-
char hex[41];
325+
char hex[GIT_SHA1_HEXSZ + 1];
326326
char mode[10];
327327

328328
/*
@@ -2878,8 +2878,7 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
28782878
die_errno("unable to write temp-file");
28792879
close_tempfile(&temp->tempfile);
28802880
temp->name = get_tempfile_path(&temp->tempfile);
2881-
strcpy(temp->hex, sha1_to_hex(sha1));
2882-
temp->hex[40] = 0;
2881+
sha1_to_hex_r(temp->hex, sha1);
28832882
xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
28842883
strbuf_release(&buf);
28852884
strbuf_release(&template);
@@ -2926,9 +2925,9 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
29262925
/* we can borrow from the file in the work tree */
29272926
temp->name = name;
29282927
if (!one->sha1_valid)
2929-
strcpy(temp->hex, sha1_to_hex(null_sha1));
2928+
sha1_to_hex_r(temp->hex, null_sha1);
29302929
else
2931-
strcpy(temp->hex, sha1_to_hex(one->sha1));
2930+
sha1_to_hex_r(temp->hex, one->sha1);
29322931
/* Even though we may sometimes borrow the
29332932
* contents from the work tree, we always want
29342933
* one->mode. mode is trustworthy even when

0 commit comments

Comments
 (0)