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>
This commit is contained in:
Jeff King 2017-03-28 15:46:56 -04:00 committed by Junio C Hamano
parent 0dc3b035e0
commit 1a168e5c86
5 changed files with 11 additions and 11 deletions

4
grep.c
View file

@ -1171,7 +1171,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
}
if (opt->linenum) {
char buf[32];
snprintf(buf, sizeof(buf), "%d", lno);
xsnprintf(buf, sizeof(buf), "%d", lno);
output_color(opt, buf, strlen(buf), opt->color_lineno);
output_sep(opt, sign);
}
@ -1653,7 +1653,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
opt->color_filename);
output_sep(opt, ':');
}
snprintf(buf, sizeof(buf), "%u\n", count);
xsnprintf(buf, sizeof(buf), "%u\n", count);
opt->output(opt, buf, strlen(buf));
return 1;
}

10
http.c
View file

@ -1366,9 +1366,9 @@ static int handle_curl_result(struct slot_results *results)
* FAILONERROR it is lost, so we can give only the numeric
* status code.
*/
snprintf(curl_errorstr, sizeof(curl_errorstr),
"The requested URL returned error: %ld",
results->http_code);
xsnprintf(curl_errorstr, sizeof(curl_errorstr),
"The requested URL returned error: %ld",
results->http_code);
}
if (results->curl_result == CURLE_OK) {
@ -1410,8 +1410,8 @@ int run_one_slot(struct active_request_slot *slot,
{
slot->results = results;
if (!start_active_slot(slot)) {
snprintf(curl_errorstr, sizeof(curl_errorstr),
"failed to start HTTP request");
xsnprintf(curl_errorstr, sizeof(curl_errorstr),
"failed to start HTTP request");
return HTTP_START_FAILED;
}

View file

@ -964,7 +964,7 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, char *f
int gai;
char portstr[6];
snprintf(portstr, sizeof(portstr), "%d", srvc->port);
xsnprintf(portstr, sizeof(portstr), "%d", srvc->port);
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;

View file

@ -3762,8 +3762,8 @@ static int for_each_file_in_obj_subdir(int subdir_nr,
char hex[GIT_SHA1_HEXSZ+1];
struct object_id oid;
snprintf(hex, sizeof(hex), "%02x%s",
subdir_nr, de->d_name);
xsnprintf(hex, sizeof(hex), "%02x%s",
subdir_nr, de->d_name);
if (!get_oid_hex(hex, &oid)) {
if (obj_cb) {
r = obj_cb(&oid, path->buf, data);

View file

@ -1402,7 +1402,7 @@ static int find_first_merges(struct object_array *result, const char *path,
memset(&rev_opts, 0, sizeof(rev_opts));
/* get all revisions that merge commit a */
snprintf(merged_revision, sizeof(merged_revision), "^%s",
xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
oid_to_hex(&a->object.oid));
init_revisions(&revs, NULL);
rev_opts.submodule = path;