Skip to content

Commit

Permalink
Merge branch 'jk/war-on-sprintf'
Browse files Browse the repository at this point in the history
Many allocations that is manually counted (correctly) that are
followed by strcpy/sprintf have been replaced with a less error
prone constructs such as xstrfmt.

Macintosh-specific breakage was noticed and corrected in this
reroll.

* jk/war-on-sprintf: (70 commits)
  name-rev: use strip_suffix to avoid magic numbers
  use strbuf_complete to conditionally append slash
  fsck: use for_each_loose_file_in_objdir
  Makefile: drop D_INO_IN_DIRENT build knob
  fsck: drop inode-sorting code
  convert strncpy to memcpy
  notes: document length of fanout path with a constant
  color: add color_set helper for copying raw colors
  prefer memcpy to strcpy
  help: clean up kfmclient munging
  receive-pack: simplify keep_arg computation
  avoid sprintf and strcpy with flex arrays
  use alloc_ref rather than hand-allocating "struct ref"
  color: add overflow checks for parsing colors
  drop strcpy in favor of raw sha1_to_hex
  use sha1_to_hex_r() instead of strcpy
  daemon: use cld->env_array when re-spawning
  stat_tracking_info: convert to argv_array
  http-push: use an argv_array for setup_revisions
  fetch-pack: use argv_array for index-pack / unpack-objects
  ...
  • Loading branch information
Junio C Hamano committed Oct 20, 2015
2 parents 614a2ac + 34e02de commit 7889179
Show file tree
Hide file tree
Showing 89 changed files with 946 additions and 1,056 deletions.
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ all::
# Define HAVE_PATHS_H if you have paths.h and want to use the default PATH
# it specifies.
#
# Define NO_D_INO_IN_DIRENT if you don't have d_ino in your struct dirent.
#
# Define NO_D_TYPE_IN_DIRENT if your platform defines DT_UNKNOWN but lacks
# d_type in struct dirent (Cygwin 1.5, fixed in Cygwin 1.7).
#
Expand Down Expand Up @@ -1164,9 +1162,6 @@ endif
ifdef NO_D_TYPE_IN_DIRENT
BASIC_CFLAGS += -DNO_D_TYPE_IN_DIRENT
endif
ifdef NO_D_INO_IN_DIRENT
BASIC_CFLAGS += -DNO_D_INO_IN_DIRENT
endif
ifdef NO_GECOS_IN_PWENT
BASIC_CFLAGS += -DNO_GECOS_IN_PWENT
endif
Expand Down
30 changes: 15 additions & 15 deletions archive-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,21 @@ static void prepare_header(struct archiver_args *args,
struct ustar_header *header,
unsigned int mode, unsigned long size)
{
sprintf(header->mode, "%07o", mode & 07777);
sprintf(header->size, "%011lo", S_ISREG(mode) ? size : 0);
sprintf(header->mtime, "%011lo", (unsigned long) args->time);
xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
xsnprintf(header->size, sizeof(header->size), "%011lo", S_ISREG(mode) ? size : 0);
xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);

sprintf(header->uid, "%07o", 0);
sprintf(header->gid, "%07o", 0);
xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
strlcpy(header->uname, "root", sizeof(header->uname));
strlcpy(header->gname, "root", sizeof(header->gname));
sprintf(header->devmajor, "%07o", 0);
sprintf(header->devminor, "%07o", 0);
xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);

memcpy(header->magic, "ustar", 6);
memcpy(header->version, "00", 2);

sprintf(header->chksum, "%07o", ustar_header_chksum(header));
snprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
}

static int write_extended_header(struct archiver_args *args,
Expand All @@ -193,7 +193,7 @@ static int write_extended_header(struct archiver_args *args,
memset(&header, 0, sizeof(header));
*header.typeflag = TYPEFLAG_EXT_HEADER;
mode = 0100666;
sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
xsnprintf(header.name, sizeof(header.name), "%s.paxheader", sha1_to_hex(sha1));
prepare_header(args, &header, mode, size);
write_blocked(&header, sizeof(header));
write_blocked(buffer, size);
Expand Down Expand Up @@ -233,10 +233,10 @@ static int write_tar_entry(struct archiver_args *args,
size_t rest = pathlen - plen - 1;
if (plen > 0 && rest <= sizeof(header.name)) {
memcpy(header.prefix, path, plen);
memcpy(header.name, path + plen + 1, rest);
memcpy(header.name, path + plen + 1, rest);
} else {
sprintf(header.name, "%s.data",
sha1_to_hex(sha1));
xsnprintf(header.name, sizeof(header.name), "%s.data",
sha1_to_hex(sha1));
strbuf_append_ext_header(&ext_header, "path",
path, pathlen);
}
Expand All @@ -259,8 +259,8 @@ static int write_tar_entry(struct archiver_args *args,

if (S_ISLNK(mode)) {
if (size > sizeof(header.linkname)) {
sprintf(header.linkname, "see %s.paxheader",
sha1_to_hex(sha1));
xsnprintf(header.linkname, sizeof(header.linkname),
"see %s.paxheader", sha1_to_hex(sha1));
strbuf_append_ext_header(&ext_header, "linkpath",
buffer, size);
} else
Expand Down Expand Up @@ -301,7 +301,7 @@ static int write_global_extended_header(struct archiver_args *args)
memset(&header, 0, sizeof(header));
*header.typeflag = TYPEFLAG_GLOBAL_HEADER;
mode = 0100666;
strcpy(header.name, "pax_global_header");
xsnprintf(header.name, sizeof(header.name), "pax_global_header");
prepare_header(args, &header, mode, ext_header.len);
write_blocked(&header, sizeof(header));
write_blocked(ext_header.buf, ext_header.len);
Expand Down
5 changes: 3 additions & 2 deletions archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,14 @@ static void queue_directory(const unsigned char *sha1,
unsigned mode, int stage, struct archiver_context *c)
{
struct directory *d;
d = xmallocz(sizeof(*d) + base->len + 1 + strlen(filename));
size_t len = base->len + 1 + strlen(filename) + 1;
d = xmalloc(sizeof(*d) + len);
d->up = c->bottom;
d->baselen = base->len;
d->mode = mode;
d->stage = stage;
c->bottom = d;
d->len = sprintf(d->path, "%.*s%s/", (int)base->len, base->buf, filename);
d->len = xsnprintf(d->path, len, "%.*s%s/", (int)base->len, base->buf, filename);
hashcpy(d->oid.hash, sha1);
}

Expand Down
29 changes: 10 additions & 19 deletions builtin/apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ static enum ws_ignore {


static const char *patch_input_file;
static const char *root;
static int root_len;
static struct strbuf root = STRBUF_INIT;
static int read_stdin = 1;
static int options;

Expand Down Expand Up @@ -494,8 +493,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)
}

strbuf_remove(&name, 0, cp - name.buf);
if (root)
strbuf_insert(&name, 0, root, root_len);
if (root.len)
strbuf_insert(&name, 0, root.buf, root.len);
return squash_slash(strbuf_detach(&name, NULL));
}

Expand Down Expand Up @@ -697,11 +696,8 @@ static char *find_name_common(const char *line, const char *def,
return squash_slash(xstrdup(def));
}

if (root) {
char *ret = xmalloc(root_len + len + 1);
strcpy(ret, root);
memcpy(ret + root_len, start, len);
ret[root_len + len] = '\0';
if (root.len) {
char *ret = xstrfmt("%s%.*s", root.buf, len, start);
return squash_slash(ret);
}

Expand Down Expand Up @@ -1277,8 +1273,8 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
* the default name from the header.
*/
patch->def_name = git_header_name(line, len);
if (patch->def_name && root) {
char *s = xstrfmt("%s%s", root, patch->def_name);
if (patch->def_name && root.len) {
char *s = xstrfmt("%s%s", root.buf, patch->def_name);
free(patch->def_name);
patch->def_name = s;
}
Expand Down Expand Up @@ -4501,14 +4497,9 @@ static int option_parse_whitespace(const struct option *opt,
static int option_parse_directory(const struct option *opt,
const char *arg, int unset)
{
root_len = strlen(arg);
if (root_len && arg[root_len - 1] != '/') {
char *new_root;
root = new_root = xmalloc(root_len + 2);
strcpy(new_root, arg);
strcpy(new_root + root_len++, "/");
} else
root = arg;
strbuf_reset(&root);
strbuf_addstr(&root, arg);
strbuf_complete(&root, '/');
return 0;
}

Expand Down
13 changes: 7 additions & 6 deletions builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,13 @@ static void queue_blames(struct scoreboard *sb, struct origin *porigin,
static struct origin *make_origin(struct commit *commit, const char *path)
{
struct origin *o;
o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
size_t pathlen = strlen(path) + 1;
o = xcalloc(1, sizeof(*o) + pathlen);
o->commit = commit;
o->refcnt = 1;
o->next = commit->util;
commit->util = o;
strcpy(o->path, path);
memcpy(o->path, path, pathlen); /* includes NUL */
return o;
}

Expand Down Expand Up @@ -1879,9 +1880,9 @@ static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent,
int cnt;
const char *cp;
struct origin *suspect = ent->suspect;
char hex[41];
char hex[GIT_SHA1_HEXSZ + 1];

strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
sha1_to_hex_r(hex, suspect->commit->object.sha1);
printf("%s %d %d %d\n",
hex,
ent->s_lno + 1,
Expand Down Expand Up @@ -1917,11 +1918,11 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
const char *cp;
struct origin *suspect = ent->suspect;
struct commit_info ci;
char hex[41];
char hex[GIT_SHA1_HEXSZ + 1];
int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);

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

cp = nth_line(sb, ent->lno);
for (cnt = 0; cnt < ent->num_lines; cnt++) {
Expand Down
6 changes: 2 additions & 4 deletions builtin/clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ static int is_git_repository(struct strbuf *path)
int gitfile_error;
size_t orig_path_len = path->len;
assert(orig_path_len != 0);
if (path->buf[orig_path_len - 1] != '/')
strbuf_addch(path, '/');
strbuf_complete(path, '/');
strbuf_addstr(path, ".git");
if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf))
ret = 1;
Expand Down Expand Up @@ -206,8 +205,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
return res;
}

if (path->buf[original_len - 1] != '/')
strbuf_addch(path, '/');
strbuf_complete(path, '/');

len = path->len;
while ((e = readdir(dir)) != NULL) {
Expand Down
34 changes: 13 additions & 21 deletions builtin/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,6 @@ static int get_value(const char *key_, const char *regex_)

static char *normalize_value(const char *key, const char *value)
{
char *normalized;

if (!value)
return NULL;

Expand All @@ -258,27 +256,21 @@ static char *normalize_value(const char *key, const char *value)
* "~/foobar/" in the config file, and to expand the ~
* when retrieving the value.
*/
normalized = xstrdup(value);
else {
normalized = xmalloc(64);
if (types == TYPE_INT) {
int64_t v = git_config_int64(key, value);
sprintf(normalized, "%"PRId64, v);
}
else if (types == TYPE_BOOL)
sprintf(normalized, "%s",
git_config_bool(key, value) ? "true" : "false");
else if (types == TYPE_BOOL_OR_INT) {
int is_bool, v;
v = git_config_bool_or_int(key, value, &is_bool);
if (!is_bool)
sprintf(normalized, "%d", v);
else
sprintf(normalized, "%s", v ? "true" : "false");
}
return xstrdup(value);
if (types == TYPE_INT)
return xstrfmt("%"PRId64, git_config_int64(key, value));
if (types == TYPE_BOOL)
return xstrdup(git_config_bool(key, value) ? "true" : "false");
if (types == TYPE_BOOL_OR_INT) {
int is_bool, v;
v = git_config_bool_or_int(key, value, &is_bool);
if (!is_bool)
return xstrfmt("%d", v);
else
return xstrdup(v ? "true" : "false");
}

return normalized;
die("BUG: cannot normalize type %d", types);
}

static int get_color_found;
Expand Down
32 changes: 15 additions & 17 deletions builtin/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,36 +528,38 @@ static int update_local_ref(struct ref *ref,
}

if (in_merge_bases(current, updated)) {
char quickref[83];
struct strbuf quickref = STRBUF_INIT;
int r;
strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
strcat(quickref, "..");
strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
strbuf_add_unique_abbrev(&quickref, current->object.sha1, DEFAULT_ABBREV);
strbuf_addstr(&quickref, "..");
strbuf_add_unique_abbrev(&quickref, ref->new_sha1, DEFAULT_ABBREV);
if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
(recurse_submodules != RECURSE_SUBMODULES_ON))
check_for_new_submodule_commits(ref->new_sha1);
r = s_update_ref("fast-forward", ref, 1);
strbuf_addf(display, "%c %-*s %-*s -> %s%s",
r ? '!' : ' ',
TRANSPORT_SUMMARY_WIDTH, quickref,
TRANSPORT_SUMMARY_WIDTH, quickref.buf,
REFCOL_WIDTH, remote, pretty_ref,
r ? _(" (unable to update local ref)") : "");
strbuf_release(&quickref);
return r;
} else if (force || ref->force) {
char quickref[84];
struct strbuf quickref = STRBUF_INIT;
int r;
strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
strcat(quickref, "...");
strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
strbuf_add_unique_abbrev(&quickref, current->object.sha1, DEFAULT_ABBREV);
strbuf_addstr(&quickref, "...");
strbuf_add_unique_abbrev(&quickref, ref->new_sha1, DEFAULT_ABBREV);
if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
(recurse_submodules != RECURSE_SUBMODULES_ON))
check_for_new_submodule_commits(ref->new_sha1);
r = s_update_ref("forced-update", ref, 1);
strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
r ? '!' : '+',
TRANSPORT_SUMMARY_WIDTH, quickref,
TRANSPORT_SUMMARY_WIDTH, quickref.buf,
REFCOL_WIDTH, remote, pretty_ref,
r ? _("unable to update local ref") : _("forced update"));
strbuf_release(&quickref);
return r;
} else {
strbuf_addf(display, "! %-*s %-*s -> %s %s",
Expand Down Expand Up @@ -637,8 +639,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
continue;

if (rm->peer_ref) {
ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
strcpy(ref->name, rm->peer_ref->name);
ref = alloc_ref(rm->peer_ref->name);
hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
hashcpy(ref->new_sha1, rm->old_sha1);
ref->force = rm->peer_ref->force;
Expand Down Expand Up @@ -1156,11 +1157,8 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
die(_("--depth and --unshallow cannot be used together"));
else if (!is_repository_shallow())
die(_("--unshallow on a complete repository does not make sense"));
else {
static char inf_depth[12];
sprintf(inf_depth, "%d", INFINITE_DEPTH);
depth = inf_depth;
}
else
depth = xstrfmt("%d", INFINITE_DEPTH);
}

/* no need to be strict, transport_set_option() will validate it again */
Expand Down
Loading

0 comments on commit 7889179

Please sign in to comment.