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 <peff@peff.net>

Jeff King committed Mar 28, 2017 at 15:46 UTC 1a168e5c86d2c6cbb57429473357bdf1acdec63c
5 files changed +11 -11
grep.c
+2 -2
@@ -1171,7 +1171,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1171 }
1172 if (opt->linenum) {
1173 char buf[32];
1174 - snprintf(buf, sizeof(buf), "%d", lno);
1174 + xsnprintf(buf, sizeof(buf), "%d", lno);
1175 output_color(opt, buf, strlen(buf), opt->color_lineno);
1176 output_sep(opt, sign);
1177 }
@@ -1653,7 +1653,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1653 opt->color_filename);
1654 output_sep(opt, ':');
1655 }
1656 - snprintf(buf, sizeof(buf), "%u\n", count);
1656 + xsnprintf(buf, sizeof(buf), "%u\n", count);
1657 opt->output(opt, buf, strlen(buf));
1658 return 1;
1659 }
http.c
+5 -5
@@ -1366,9 +1366,9 @@ static int handle_curl_result(struct slot_results *results)
1366 * FAILONERROR it is lost, so we can give only the numeric
1367 * status code.
1368 */
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);
1372 }
1373
1374 if (results->curl_result == CURLE_OK) {
@@ -1410,8 +1410,8 @@ int run_one_slot(struct active_request_slot *slot,
1410 {
1411 slot->results = results;
1412 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");
1415 return HTTP_START_FAILED;
1416 }
1417
imap-send.c
+1 -1
@@ -964,7 +964,7 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, char *f
964 int gai;
965 char portstr[6];
966
967 - snprintf(portstr, sizeof(portstr), "%d", srvc->port);
967 + xsnprintf(portstr, sizeof(portstr), "%d", srvc->port);
968
969 memset(&hints, 0, sizeof(hints));
970 hints.ai_socktype = SOCK_STREAM;
sha1_file.c
+2 -2
@@ -3762,8 +3762,8 @@ static int for_each_file_in_obj_subdir(int subdir_nr,
3762 char hex[GIT_SHA1_HEXSZ+1];
3763 struct object_id oid;
3764
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);
3767 if (!get_oid_hex(hex, &oid)) {
3768 if (obj_cb) {
3769 r = obj_cb(&oid, path->buf, data);
submodule.c
+1 -1
@@ -1402,7 +1402,7 @@ static int find_first_merges(struct object_array *result, const char *path,
1402 memset(&rev_opts, 0, sizeof(rev_opts));
1403
1404 /* get all revisions that merge commit a */
1405 - snprintf(merged_revision, sizeof(merged_revision), "^%s",
1405 + xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
1406 oid_to_hex(&a->object.oid));
1407 init_revisions(&revs, NULL);
1408 rev_opts.submodule = path;