Skip to content

Commit 1a168e5

Browse files
peffgitster
authored andcommitted
convert unchecked snprintf into xsnprintf
These calls to snprintf should always succeed, because their input is small and fixed. Let's use xsnprintf to make sure this is the case (and to make auditing for actual truncation easier). These could be candidates for turning into heap buffers, but they fall into a few broad categories that make it not worth doing: - formatting single numbers is simple enough that we can see the result should fit - the size of a sha1 is likewise well-known, and I didn't want to cause unnecessary conflicts with the ongoing process to convert these constants to GIT_MAX_HEXSZ - the interface for curl_errorstr is dictated by curl Signed-off-by: Jeff King <[email protected]>
1 parent 0dc3b03 commit 1a168e5

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

grep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
11711171
}
11721172
if (opt->linenum) {
11731173
char buf[32];
1174-
snprintf(buf, sizeof(buf), "%d", lno);
1174+
xsnprintf(buf, sizeof(buf), "%d", lno);
11751175
output_color(opt, buf, strlen(buf), opt->color_lineno);
11761176
output_sep(opt, sign);
11771177
}
@@ -1653,7 +1653,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
16531653
opt->color_filename);
16541654
output_sep(opt, ':');
16551655
}
1656-
snprintf(buf, sizeof(buf), "%u\n", count);
1656+
xsnprintf(buf, sizeof(buf), "%u\n", count);
16571657
opt->output(opt, buf, strlen(buf));
16581658
return 1;
16591659
}

http.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,9 +1366,9 @@ static int handle_curl_result(struct slot_results *results)
13661366
* FAILONERROR it is lost, so we can give only the numeric
13671367
* status code.
13681368
*/
1369-
snprintf(curl_errorstr, sizeof(curl_errorstr),
1370-
"The requested URL returned error: %ld",
1371-
results->http_code);
1369+
xsnprintf(curl_errorstr, sizeof(curl_errorstr),
1370+
"The requested URL returned error: %ld",
1371+
results->http_code);
13721372
}
13731373

13741374
if (results->curl_result == CURLE_OK) {
@@ -1410,8 +1410,8 @@ int run_one_slot(struct active_request_slot *slot,
14101410
{
14111411
slot->results = results;
14121412
if (!start_active_slot(slot)) {
1413-
snprintf(curl_errorstr, sizeof(curl_errorstr),
1414-
"failed to start HTTP request");
1413+
xsnprintf(curl_errorstr, sizeof(curl_errorstr),
1414+
"failed to start HTTP request");
14151415
return HTTP_START_FAILED;
14161416
}
14171417

imap-send.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, char *f
964964
int gai;
965965
char portstr[6];
966966

967-
snprintf(portstr, sizeof(portstr), "%d", srvc->port);
967+
xsnprintf(portstr, sizeof(portstr), "%d", srvc->port);
968968

969969
memset(&hints, 0, sizeof(hints));
970970
hints.ai_socktype = SOCK_STREAM;

sha1_file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3762,8 +3762,8 @@ static int for_each_file_in_obj_subdir(int subdir_nr,
37623762
char hex[GIT_SHA1_HEXSZ+1];
37633763
struct object_id oid;
37643764

3765-
snprintf(hex, sizeof(hex), "%02x%s",
3766-
subdir_nr, de->d_name);
3765+
xsnprintf(hex, sizeof(hex), "%02x%s",
3766+
subdir_nr, de->d_name);
37673767
if (!get_oid_hex(hex, &oid)) {
37683768
if (obj_cb) {
37693769
r = obj_cb(&oid, path->buf, data);

submodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ static int find_first_merges(struct object_array *result, const char *path,
14021402
memset(&rev_opts, 0, sizeof(rev_opts));
14031403

14041404
/* get all revisions that merge commit a */
1405-
snprintf(merged_revision, sizeof(merged_revision), "^%s",
1405+
xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
14061406
oid_to_hex(&a->object.oid));
14071407
init_revisions(&revs, NULL);
14081408
rev_opts.submodule = path;

0 commit comments

Comments
 (0)