Merge branch 'jk/war-on-sprintf'

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
  ...
This commit is contained in:
Junio C Hamano 2015-10-20 15:24:00 -07:00
Родитель 614a2aced1 34e02deb60
Коммит 78891795df
89 изменённых файлов: 945 добавлений и 1055 удалений

Просмотреть файл

@ -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).
#
@ -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

Просмотреть файл

@ -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,
@ -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);
@ -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);
}
@ -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
@ -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);

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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;
@ -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));
}
@ -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);
}
@ -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;
}
@ -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;
}

Просмотреть файл

@ -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;
}
@ -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,
@ -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++) {

Просмотреть файл

@ -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;
@ -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) {

Просмотреть файл

@ -246,8 +246,6 @@ free_strings:
static char *normalize_value(const char *key, const char *value)
{
char *normalized;
if (!value)
return NULL;
@ -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;

Просмотреть файл

@ -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",
@ -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;
@ -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 */

Просмотреть файл

@ -40,14 +40,6 @@ static int show_dangling = 1;
#define ERROR_PACK 04
#define ERROR_REFS 010
#ifdef NO_D_INO_IN_DIRENT
#define SORT_DIRENT 0
#define DIRENT_SORT_HINT(de) 0
#else
#define SORT_DIRENT 1
#define DIRENT_SORT_HINT(de) ((de)->d_ino)
#endif
static int fsck_config(const char *var, const char *value, void *cb)
{
if (strcmp(var, "fsck.skiplist") == 0) {
@ -374,102 +366,6 @@ static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
return fsck_obj(obj);
}
/*
* This is the sorting chunk size: make it reasonably
* big so that we can sort well..
*/
#define MAX_SHA1_ENTRIES (1024)
struct sha1_entry {
unsigned long ino;
unsigned char sha1[20];
};
static struct {
unsigned long nr;
struct sha1_entry *entry[MAX_SHA1_ENTRIES];
} sha1_list;
static int ino_compare(const void *_a, const void *_b)
{
const struct sha1_entry *a = _a, *b = _b;
unsigned long ino1 = a->ino, ino2 = b->ino;
return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
}
static void fsck_sha1_list(void)
{
int i, nr = sha1_list.nr;
if (SORT_DIRENT)
qsort(sha1_list.entry, nr,
sizeof(struct sha1_entry *), ino_compare);
for (i = 0; i < nr; i++) {
struct sha1_entry *entry = sha1_list.entry[i];
unsigned char *sha1 = entry->sha1;
sha1_list.entry[i] = NULL;
if (fsck_sha1(sha1))
errors_found |= ERROR_OBJECT;
free(entry);
}
sha1_list.nr = 0;
}
static void add_sha1_list(unsigned char *sha1, unsigned long ino)
{
struct sha1_entry *entry = xmalloc(sizeof(*entry));
int nr;
entry->ino = ino;
hashcpy(entry->sha1, sha1);
nr = sha1_list.nr;
if (nr == MAX_SHA1_ENTRIES) {
fsck_sha1_list();
nr = 0;
}
sha1_list.entry[nr] = entry;
sha1_list.nr = ++nr;
}
static inline int is_loose_object_file(struct dirent *de,
char *name, unsigned char *sha1)
{
if (strlen(de->d_name) != 38)
return 0;
memcpy(name + 2, de->d_name, 39);
return !get_sha1_hex(name, sha1);
}
static void fsck_dir(int i, char *path)
{
DIR *dir = opendir(path);
struct dirent *de;
char name[100];
if (!dir)
return;
if (verbose)
fprintf(stderr, "Checking directory %s\n", path);
sprintf(name, "%02x", i);
while ((de = readdir(dir)) != NULL) {
unsigned char sha1[20];
if (is_dot_or_dotdot(de->d_name))
continue;
if (is_loose_object_file(de, name, sha1)) {
add_sha1_list(sha1, DIRENT_SORT_HINT(de));
continue;
}
if (starts_with(de->d_name, "tmp_obj_"))
continue;
fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
}
closedir(dir);
}
static int default_refs;
static void fsck_handle_reflog_sha1(const char *refname, unsigned char *sha1)
@ -559,9 +455,28 @@ static void get_default_heads(void)
}
}
static int fsck_loose(const unsigned char *sha1, const char *path, void *data)
{
if (fsck_sha1(sha1))
errors_found |= ERROR_OBJECT;
return 0;
}
static int fsck_cruft(const char *basename, const char *path, void *data)
{
if (!starts_with(basename, "tmp_obj_"))
fprintf(stderr, "bad sha1 file: %s\n", path);
return 0;
}
static int fsck_subdir(int nr, const char *path, void *progress)
{
display_progress(progress, nr + 1);
return 0;
}
static void fsck_object_dir(const char *path)
{
int i;
struct progress *progress = NULL;
if (verbose)
@ -569,14 +484,11 @@ static void fsck_object_dir(const char *path)
if (show_progress)
progress = start_progress(_("Checking object directories"), 256);
for (i = 0; i < 256; i++) {
static char dir[4096];
sprintf(dir, "%s/%02x", path, i);
fsck_dir(i, dir);
display_progress(progress, i+1);
}
for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
progress);
display_progress(progress, 256);
stop_progress(&progress);
fsck_sha1_list();
}
static int fsck_head_link(void)
@ -688,16 +600,18 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
git_config(fsck_config, NULL);
fsck_head_link();
if (!connectivity_only)
if (!connectivity_only) {
fsck_object_dir(get_object_directory());
prepare_alt_odb();
for (alt = alt_odb_list; alt; alt = alt->next) {
char namebuf[PATH_MAX];
int namelen = alt->name - alt->base;
memcpy(namebuf, alt->base, namelen);
namebuf[namelen - 1] = 0;
fsck_object_dir(namebuf);
prepare_alt_odb();
for (alt = alt_odb_list; alt; alt = alt->next) {
/* directory name, minus trailing slash */
size_t namelen = alt->name - alt->base - 1;
struct strbuf name = STRBUF_INIT;
strbuf_add(&name, alt->base, namelen);
fsck_object_dir(name.buf);
strbuf_release(&name);
}
}
if (check_full) {

Просмотреть файл

@ -217,7 +217,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
return NULL;
if (gethostname(my_host, sizeof(my_host)))
strcpy(my_host, "unknown");
xsnprintf(my_host, sizeof(my_host), "unknown");
pidfile_path = git_pathdup("gc.pid");
fd = hold_lock_file_for_update(&lock, pidfile_path,

Просмотреть файл

@ -140,17 +140,10 @@ static void exec_man_konqueror(const char *path, const char *page)
/* It's simpler to launch konqueror using kfmclient. */
if (path) {
const char *file = strrchr(path, '/');
if (file && !strcmp(file + 1, "konqueror")) {
char *new = xstrdup(path);
char *dest = strrchr(new, '/');
/* strlen("konqueror") == strlen("kfmclient") */
strcpy(dest + 1, "kfmclient");
path = new;
}
if (file)
filename = file;
size_t len;
if (strip_suffix(path, "/konqueror", &len))
path = xstrfmt("%.*s/kfmclient", (int)len, path);
filename = basename((char *)path);
} else
path = "kfmclient";
strbuf_addf(&man_page, "man:%s(1)", page);
@ -183,7 +176,7 @@ static void add_man_viewer(const char *name)
while (*p)
p = &((*p)->next);
*p = xcalloc(1, (sizeof(**p) + len + 1));
strncpy((*p)->name, name, len);
memcpy((*p)->name, name, len); /* NUL-terminated by xcalloc */
}
static int supported_man_viewer(const char *name, size_t len)
@ -199,7 +192,7 @@ static void do_add_man_viewer_info(const char *name,
{
struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
strncpy(new->name, name, len);
memcpy(new->name, name, len); /* NUL-terminated by xcalloc */
new->info = xstrdup(value);
new->next = man_viewer_info_list;
man_viewer_info_list = new;
@ -295,16 +288,6 @@ static int is_git_command(const char *s)
is_in_cmdlist(&other_cmds, s);
}
static const char *prepend(const char *prefix, const char *cmd)
{
size_t pre_len = strlen(prefix);
size_t cmd_len = strlen(cmd);
char *p = xmalloc(pre_len + cmd_len + 1);
memcpy(p, prefix, pre_len);
strcpy(p + pre_len, cmd);
return p;
}
static const char *cmd_to_page(const char *git_cmd)
{
if (!git_cmd)
@ -312,9 +295,9 @@ static const char *cmd_to_page(const char *git_cmd)
else if (starts_with(git_cmd, "git"))
return git_cmd;
else if (is_git_command(git_cmd))
return prepend("git-", git_cmd);
return xstrfmt("git-%s", git_cmd);
else
return prepend("git", git_cmd);
return xstrfmt("git%s", git_cmd);
}
static void setup_man_path(void)

Просмотреть файл

@ -441,7 +441,7 @@ static void *unpack_entry_data(unsigned long offset, unsigned long size,
int hdrlen;
if (!is_delta_type(type)) {
hdrlen = sprintf(hdr, "%s %lu", typename(type), size) + 1;
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), size) + 1;
git_SHA1_Init(&c);
git_SHA1_Update(&c, hdr, hdrlen);
} else

Просмотреть файл

@ -36,10 +36,11 @@ static void safe_create_dir(const char *dir, int share)
die(_("Could not make %s writable by group"), dir);
}
static void copy_templates_1(char *path, int baselen,
char *template, int template_baselen,
static void copy_templates_1(struct strbuf *path, struct strbuf *template,
DIR *dir)
{
size_t path_baselen = path->len;
size_t template_baselen = template->len;
struct dirent *de;
/* Note: if ".git/hooks" file exists in the repository being
@ -49,77 +50,64 @@ static void copy_templates_1(char *path, int baselen,
* with the way the namespace under .git/ is organized, should
* be really carefully chosen.
*/
safe_create_dir(path, 1);
safe_create_dir(path->buf, 1);
while ((de = readdir(dir)) != NULL) {
struct stat st_git, st_template;
int namelen;
int exists = 0;
strbuf_setlen(path, path_baselen);
strbuf_setlen(template, template_baselen);
if (de->d_name[0] == '.')
continue;
namelen = strlen(de->d_name);
if ((PATH_MAX <= baselen + namelen) ||
(PATH_MAX <= template_baselen + namelen))
die(_("insanely long template name %s"), de->d_name);
memcpy(path + baselen, de->d_name, namelen+1);
memcpy(template + template_baselen, de->d_name, namelen+1);
if (lstat(path, &st_git)) {
strbuf_addstr(path, de->d_name);
strbuf_addstr(template, de->d_name);
if (lstat(path->buf, &st_git)) {
if (errno != ENOENT)
die_errno(_("cannot stat '%s'"), path);
die_errno(_("cannot stat '%s'"), path->buf);
}
else
exists = 1;
if (lstat(template, &st_template))
die_errno(_("cannot stat template '%s'"), template);
if (lstat(template->buf, &st_template))
die_errno(_("cannot stat template '%s'"), template->buf);
if (S_ISDIR(st_template.st_mode)) {
DIR *subdir = opendir(template);
int baselen_sub = baselen + namelen;
int template_baselen_sub = template_baselen + namelen;
DIR *subdir = opendir(template->buf);
if (!subdir)
die_errno(_("cannot opendir '%s'"), template);
path[baselen_sub++] =
template[template_baselen_sub++] = '/';
path[baselen_sub] =
template[template_baselen_sub] = 0;
copy_templates_1(path, baselen_sub,
template, template_baselen_sub,
subdir);
die_errno(_("cannot opendir '%s'"), template->buf);
strbuf_addch(path, '/');
strbuf_addch(template, '/');
copy_templates_1(path, template, subdir);
closedir(subdir);
}
else if (exists)
continue;
else if (S_ISLNK(st_template.st_mode)) {
char lnk[256];
int len;
len = readlink(template, lnk, sizeof(lnk));
if (len < 0)
die_errno(_("cannot readlink '%s'"), template);
if (sizeof(lnk) <= len)
die(_("insanely long symlink %s"), template);
lnk[len] = 0;
if (symlink(lnk, path))
die_errno(_("cannot symlink '%s' '%s'"), lnk, path);
struct strbuf lnk = STRBUF_INIT;
if (strbuf_readlink(&lnk, template->buf, 0) < 0)
die_errno(_("cannot readlink '%s'"), template->buf);
if (symlink(lnk.buf, path->buf))
die_errno(_("cannot symlink '%s' '%s'"),
lnk.buf, path->buf);
strbuf_release(&lnk);
}
else if (S_ISREG(st_template.st_mode)) {
if (copy_file(path, template, st_template.st_mode))
die_errno(_("cannot copy '%s' to '%s'"), template,
path);
if (copy_file(path->buf, template->buf, st_template.st_mode))
die_errno(_("cannot copy '%s' to '%s'"),
template->buf, path->buf);
}
else
error(_("ignoring template %s"), template);
error(_("ignoring template %s"), template->buf);
}
}
static void copy_templates(const char *template_dir)
{
char path[PATH_MAX];
char template_path[PATH_MAX];
int template_len;
struct strbuf path = STRBUF_INIT;
struct strbuf template_path = STRBUF_INIT;
size_t template_len;
DIR *dir;
const char *git_dir = get_git_dir();
int len = strlen(git_dir);
char *to_free = NULL;
if (!template_dir)
@ -132,26 +120,23 @@ static void copy_templates(const char *template_dir)
free(to_free);
return;
}
template_len = strlen(template_dir);
if (PATH_MAX <= (template_len+strlen("/config")))
die(_("insanely long template path %s"), template_dir);
strcpy(template_path, template_dir);
if (template_path[template_len-1] != '/') {
template_path[template_len++] = '/';
template_path[template_len] = 0;
}
dir = opendir(template_path);
strbuf_addstr(&template_path, template_dir);
strbuf_complete(&template_path, '/');
template_len = template_path.len;
dir = opendir(template_path.buf);
if (!dir) {
warning(_("templates not found %s"), template_dir);
goto free_return;
}
/* Make sure that template is from the correct vintage */
strcpy(template_path + template_len, "config");
strbuf_addstr(&template_path, "config");
repository_format_version = 0;
git_config_from_file(check_repository_format_version,
template_path, NULL);
template_path[template_len] = 0;
template_path.buf, NULL);
strbuf_setlen(&template_path, template_len);
if (repository_format_version &&
repository_format_version != GIT_REPO_VERSION) {
@ -162,17 +147,15 @@ static void copy_templates(const char *template_dir)
goto close_free_return;
}
memcpy(path, git_dir, len);
if (len && path[len - 1] != '/')
path[len++] = '/';
path[len] = 0;
copy_templates_1(path, len,
template_path, template_len,
dir);
strbuf_addstr(&path, get_git_dir());
strbuf_complete(&path, '/');
copy_templates_1(&path, &template_path, dir);
close_free_return:
closedir(dir);
free_return:
free(to_free);
strbuf_release(&path);
strbuf_release(&template_path);
}
static int git_init_db_config(const char *k, const char *v, void *cb)
@ -199,28 +182,20 @@ static int needs_work_tree_config(const char *git_dir, const char *work_tree)
static int create_default_files(const char *template_path)
{
const char *git_dir = get_git_dir();
unsigned len = strlen(git_dir);
static char path[PATH_MAX];
struct stat st1;
struct strbuf buf = STRBUF_INIT;
char *path;
char repo_version_string[10];
char junk[2];
int reinit;
int filemode;
if (len > sizeof(path)-50)
die(_("insane git directory %s"), git_dir);
memcpy(path, git_dir, len);
if (len && path[len-1] != '/')
path[len++] = '/';
/*
* Create .git/refs/{heads,tags}
*/
safe_create_dir(git_path("refs"), 1);
safe_create_dir(git_path("refs/heads"), 1);
safe_create_dir(git_path("refs/tags"), 1);
safe_create_dir(git_path_buf(&buf, "refs"), 1);
safe_create_dir(git_path_buf(&buf, "refs/heads"), 1);
safe_create_dir(git_path_buf(&buf, "refs/tags"), 1);
/* Just look for `init.templatedir` */
git_config(git_init_db_config, NULL);
@ -244,16 +219,16 @@ static int create_default_files(const char *template_path)
*/
if (shared_repository) {
adjust_shared_perm(get_git_dir());
adjust_shared_perm(git_path("refs"));
adjust_shared_perm(git_path("refs/heads"));
adjust_shared_perm(git_path("refs/tags"));
adjust_shared_perm(git_path_buf(&buf, "refs"));
adjust_shared_perm(git_path_buf(&buf, "refs/heads"));
adjust_shared_perm(git_path_buf(&buf, "refs/tags"));
}
/*
* Create the default symlink from ".git/HEAD" to the "master"
* branch, if it does not exist yet.
*/
strcpy(path + len, "HEAD");
path = git_path_buf(&buf, "HEAD");
reinit = (!access(path, R_OK)
|| readlink(path, junk, sizeof(junk)-1) != -1);
if (!reinit) {
@ -262,13 +237,12 @@ static int create_default_files(const char *template_path)
}
/* This forces creation of new config file */
sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
xsnprintf(repo_version_string, sizeof(repo_version_string),
"%d", GIT_REPO_VERSION);
git_config_set("core.repositoryformatversion", repo_version_string);
path[len] = 0;
strcpy(path + len, "config");
/* Check filemode trustability */
path = git_path_buf(&buf, "config");
filemode = TEST_FILEMODE;
if (TEST_FILEMODE && !lstat(path, &st1)) {
struct stat st2;
@ -289,14 +263,13 @@ static int create_default_files(const char *template_path)
/* allow template config file to override the default */
if (log_all_ref_updates == -1)
git_config_set("core.logallrefupdates", "true");
if (needs_work_tree_config(git_dir, work_tree))
if (needs_work_tree_config(get_git_dir(), work_tree))
git_config_set("core.worktree", work_tree);
}
if (!reinit) {
/* Check if symlink is supported in the work tree */
path[len] = 0;
strcpy(path + len, "tXXXXXX");
path = git_path_buf(&buf, "tXXXXXX");
if (!close(xmkstemp(path)) &&
!unlink(path) &&
!symlink("testing", path) &&
@ -307,31 +280,35 @@ static int create_default_files(const char *template_path)
git_config_set("core.symlinks", "false");
/* Check if the filesystem is case-insensitive */
path[len] = 0;
strcpy(path + len, "CoNfIg");
path = git_path_buf(&buf, "CoNfIg");
if (!access(path, F_OK))
git_config_set("core.ignorecase", "true");
probe_utf8_pathname_composition(path, len);
probe_utf8_pathname_composition();
}
strbuf_release(&buf);
return reinit;
}
static void create_object_directory(void)
{
const char *object_directory = get_object_directory();
int len = strlen(object_directory);
char *path = xmalloc(len + 40);
struct strbuf path = STRBUF_INIT;
size_t baselen;
memcpy(path, object_directory, len);
strbuf_addstr(&path, get_object_directory());
baselen = path.len;
safe_create_dir(object_directory, 1);
strcpy(path+len, "/pack");
safe_create_dir(path, 1);
strcpy(path+len, "/info");
safe_create_dir(path, 1);
safe_create_dir(path.buf, 1);
free(path);
strbuf_setlen(&path, baselen);
strbuf_addstr(&path, "/pack");
safe_create_dir(path.buf, 1);
strbuf_setlen(&path, baselen);
strbuf_addstr(&path, "/info");
safe_create_dir(path.buf, 1);
strbuf_release(&path);
}
int set_git_dir_init(const char *git_dir, const char *real_git_dir,
@ -414,13 +391,13 @@ int init_db(const char *template_dir, unsigned int flags)
*/
if (shared_repository < 0)
/* force to the mode value */
sprintf(buf, "0%o", -shared_repository);
xsnprintf(buf, sizeof(buf), "0%o", -shared_repository);
else if (shared_repository == PERM_GROUP)
sprintf(buf, "%d", OLD_PERM_GROUP);
xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
else if (shared_repository == PERM_EVERYBODY)
sprintf(buf, "%d", OLD_PERM_EVERYBODY);
xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
else
die("oops");
die("BUG: invalid value for shared_repository");
git_config_set("core.sharedrepository", buf);
git_config_set("receive.denyNonFastforwards", "true");
}

Просмотреть файл

@ -796,8 +796,7 @@ static int reopen_stdout(struct commit *commit, const char *subject,
if (filename.len >=
PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
return error(_("name of output directory is too long"));
if (filename.buf[filename.len - 1] != '/')
strbuf_addch(&filename, '/');
strbuf_complete(&filename, '/');
}
if (rev->numbered_files)

Просмотреть файл

@ -93,12 +93,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
if (argv[i]) {
int j;
pattern = xcalloc(argc - i + 1, sizeof(const char *));
for (j = i; j < argc; j++) {
int len = strlen(argv[j]);
char *p = xmalloc(len + 3);
sprintf(p, "*/%s", argv[j]);
pattern[j - i] = p;
}
for (j = i; j < argc; j++)
pattern[j - i] = xstrfmt("*/%s", argv[j]);
}
remote = remote_get(dest);
if (!remote) {

Просмотреть файл

@ -96,12 +96,13 @@ static int show_tree(const unsigned char *sha1, struct strbuf *base,
if (!strcmp(type, blob_type)) {
unsigned long size;
if (sha1_object_info(sha1, &size) == OBJ_BAD)
strcpy(size_text, "BAD");
xsnprintf(size_text, sizeof(size_text),
"BAD");
else
snprintf(size_text, sizeof(size_text),
"%lu", size);
xsnprintf(size_text, sizeof(size_text),
"%lu", size);
} else
strcpy(size_text, "-");
xsnprintf(size_text, sizeof(size_text), "-");
printf("%06o %s %s %7s\t", mode, type,
find_unique_abbrev(sha1, abbrev),
size_text);

Просмотреть файл

@ -98,30 +98,37 @@ static int populate_maildir_list(struct string_list *list, const char *path)
{
DIR *dir;
struct dirent *dent;
char name[PATH_MAX];
char *name = NULL;
char *subs[] = { "cur", "new", NULL };
char **sub;
int ret = -1;
for (sub = subs; *sub; ++sub) {
snprintf(name, sizeof(name), "%s/%s", path, *sub);
free(name);
name = xstrfmt("%s/%s", path, *sub);
if ((dir = opendir(name)) == NULL) {
if (errno == ENOENT)
continue;
error("cannot opendir %s (%s)", name, strerror(errno));
return -1;
goto out;
}
while ((dent = readdir(dir)) != NULL) {
if (dent->d_name[0] == '.')
continue;
snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
free(name);
name = xstrfmt("%s/%s", *sub, dent->d_name);
string_list_insert(list, name);
}
closedir(dir);
}
return 0;
ret = 0;
out:
free(name);
return ret;
}
static int maildir_filename_cmp(const char *a, const char *b)
@ -148,8 +155,8 @@ static int maildir_filename_cmp(const char *a, const char *b)
static int split_maildir(const char *maildir, const char *dir,
int nr_prec, int skip)
{
char file[PATH_MAX];
char name[PATH_MAX];
char *file = NULL;
FILE *f = NULL;
int ret = -1;
int i;
struct string_list list = STRING_LIST_INIT_DUP;
@ -160,8 +167,11 @@ static int split_maildir(const char *maildir, const char *dir,
goto out;
for (i = 0; i < list.nr; i++) {
FILE *f;
snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string);
char *name;
free(file);
file = xstrfmt("%s/%s", maildir, list.items[i].string);
f = fopen(file, "r");
if (!f) {
error("cannot open mail %s (%s)", file, strerror(errno));
@ -173,14 +183,19 @@ static int split_maildir(const char *maildir, const char *dir,
goto out;
}
sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
name = xstrfmt("%s/%0*d", dir, nr_prec, ++skip);
split_one(f, name, 1);
free(name);
fclose(f);
f = NULL;
}
ret = skip;
out:
if (f)
fclose(f);
free(file);
string_list_clear(&list, 1);
return ret;
}
@ -188,7 +203,6 @@ out:
static int split_mbox(const char *file, const char *dir, int allow_bare,
int nr_prec, int skip)
{
char name[PATH_MAX];
int ret = -1;
int peek;
@ -215,8 +229,9 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
}
while (!file_done) {
sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
char *name = xstrfmt("%s/%0*d", dir, nr_prec, ++skip);
file_done = split_one(f, name, allow_bare);
free(name);
}
if (f != stdin)

Просмотреть файл

@ -9,7 +9,7 @@ static int merge_entry(int pos, const char *path)
{
int found;
const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
char hexbuf[4][60];
char hexbuf[4][GIT_SHA1_HEXSZ + 1];
char ownbuf[4][60];
if (pos >= active_nr)
@ -22,8 +22,8 @@ static int merge_entry(int pos, const char *path)
if (strcmp(ce->name, path))
break;
found++;
strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
sprintf(ownbuf[stage], "%o", ce->ce_mode);
sha1_to_hex_r(hexbuf[stage], ce->sha1);
xsnprintf(ownbuf[stage], sizeof(ownbuf[stage]), "%o", ce->ce_mode);
arguments[stage] = hexbuf[stage];
arguments[stage + 4] = ownbuf[stage];
} while (++pos < active_nr);

Просмотреть файл

@ -14,7 +14,7 @@ static const char *better_branch_name(const char *branch)
if (strlen(branch) != 40)
return branch;
sprintf(githead_env, "GITHEAD_%s", branch);
xsnprintf(githead_env, sizeof(githead_env), "GITHEAD_%s", branch);
name = getenv(githead_env);
return name ? name : branch;
}

Просмотреть файл

@ -1319,13 +1319,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
if (verify_signatures) {
for (p = remoteheads; p; p = p->next) {
struct commit *commit = p->item;
char hex[41];
char hex[GIT_SHA1_HEXSZ + 1];
struct signature_check signature_check;
memset(&signature_check, 0, sizeof(signature_check));
check_commit_signature(commit, &signature_check);
strcpy(hex, find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
find_unique_abbrev_r(hex, commit->object.sha1, DEFAULT_ABBREV);
switch (signature_check.result) {
case 'G':
break;
@ -1415,15 +1415,15 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
/* Again the most common case of merging one remote. */
struct strbuf msg = STRBUF_INIT;
struct commit *commit;
char hex[41];
strcpy(hex, find_unique_abbrev(head_commit->object.sha1, DEFAULT_ABBREV));
if (verbosity >= 0)
printf(_("Updating %s..%s\n"),
hex,
find_unique_abbrev(remoteheads->item->object.sha1,
DEFAULT_ABBREV));
if (verbosity >= 0) {
char from[GIT_SHA1_HEXSZ + 1], to[GIT_SHA1_HEXSZ + 1];
find_unique_abbrev_r(from, head_commit->object.sha1,
DEFAULT_ABBREV);
find_unique_abbrev_r(to, remoteheads->item->object.sha1,
DEFAULT_ABBREV);
printf(_("Updating %s..%s\n"), from, to);
}
strbuf_addstr(&msg, "Fast-forward");
if (have_message)
strbuf_addstr(&msg,

Просмотреть файл

@ -55,20 +55,16 @@ copy_data:
parents;
parents = parents->next, parent_number++) {
if (parent_number > 1) {
int len = strlen(tip_name);
char *new_name = xmalloc(len +
1 + decimal_length(generation) + /* ~<n> */
1 + 2 + /* ^NN */
1);
size_t len;
char *new_name;
if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
len -= 2;
strip_suffix(tip_name, "^0", &len);
if (generation > 0)
sprintf(new_name, "%.*s~%d^%d", len, tip_name,
generation, parent_number);
new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
generation, parent_number);
else
sprintf(new_name, "%.*s^%d", len, tip_name,
parent_number);
new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
parent_number);
name_rev(parents->item, new_name, 0,
distance + MERGE_TRAVERSAL_WEIGHT, 0);

Просмотреть файл

@ -90,7 +90,7 @@ static int debug_merge(const struct cache_entry * const *stages,
debug_stage("index", stages[0], o);
for (i = 1; i <= o->merge_size; i++) {
char buf[24];
sprintf(buf, "ent#%d", i);
xsnprintf(buf, sizeof(buf), "ent#%d", i);
debug_stage(buf, stages[i], o);
}
return 0;

Просмотреть файл

@ -280,10 +280,10 @@ static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2
static void report_message(const char *prefix, const char *err, va_list params)
{
int sz = strlen(prefix);
int sz;
char msg[4096];
strncpy(msg, prefix, sz);
sz = xsnprintf(msg, sizeof(msg), "%s", prefix);
sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
if (sz > (sizeof(msg) - 1))
sz = sizeof(msg) - 1;
@ -1071,8 +1071,11 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
const char *dst_name;
struct string_list_item *item;
struct command *dst_cmd;
unsigned char sha1[20];
char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
unsigned char sha1[GIT_SHA1_RAWSZ];
char cmd_oldh[GIT_SHA1_HEXSZ + 1],
cmd_newh[GIT_SHA1_HEXSZ + 1],
dst_oldh[GIT_SHA1_HEXSZ + 1],
dst_newh[GIT_SHA1_HEXSZ + 1];
int flag;
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)
dst_cmd->skip_update = 1;
strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
find_unique_abbrev_r(cmd_oldh, cmd->old_sha1, DEFAULT_ABBREV);
find_unique_abbrev_r(cmd_newh, cmd->new_sha1, DEFAULT_ABBREV);
find_unique_abbrev_r(dst_oldh, dst_cmd->old_sha1, DEFAULT_ABBREV);
find_unique_abbrev_r(dst_newh, dst_cmd->new_sha1, DEFAULT_ABBREV);
rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
" its target '%s' (%s..%s)",
cmd->ref_name, cmd_oldh, cmd_newh,
@ -1521,15 +1524,18 @@ static const char *unpack(int err_fd, struct shallow_info *si)
if (status)
return "unpack-objects abnormal exit";
} else {
int s;
char keep_arg[256];
s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
strcpy(keep_arg + s, "localhost");
char hostname[256];
argv_array_pushl(&child.args, "index-pack",
"--stdin", hdr_arg, keep_arg, NULL);
"--stdin", hdr_arg, NULL);
if (gethostname(hostname, sizeof(hostname)))
xsnprintf(hostname, sizeof(hostname), "localhost");
argv_array_pushf(&child.args,
"--keep=receive-pack %"PRIuMAX" on %s",
(uintmax_t)getpid(),
hostname);
if (fsck_objects)
argv_array_pushf(&child.args, "--strict%s",
fsck_msg_types.buf);

Просмотреть файл

@ -1,6 +1,7 @@
#include "builtin.h"
#include "transport.h"
#include "run-command.h"
#include "pkt-line.h"
/*
* URL syntax:
@ -142,36 +143,11 @@ static const char **parse_argv(const char *arg, const char *service)
static void send_git_request(int stdin_fd, const char *serv, const char *repo,
const char *vhost)
{
size_t bufferspace;
size_t wpos = 0;
char *buffer;
/*
* Request needs 12 bytes extra if there is vhost (xxxx \0host=\0) and
* 6 bytes extra (xxxx \0) if there is no vhost.
*/
if (vhost)
bufferspace = strlen(serv) + strlen(repo) + strlen(vhost) + 12;
if (!vhost)
packet_write(stdin_fd, "%s %s%c", serv, repo, 0);
else
bufferspace = strlen(serv) + strlen(repo) + 6;
if (bufferspace > 0xFFFF)
die("Request too large to send");
buffer = xmalloc(bufferspace);
/* Make the packet. */
wpos = sprintf(buffer, "%04x%s %s%c", (unsigned)bufferspace,
serv, repo, 0);
/* Add vhost if any. */
if (vhost)
sprintf(buffer + wpos, "host=%s%c", vhost, 0);
/* Send the request */
if (write_in_full(stdin_fd, buffer, bufferspace) < 0)
die_errno("Failed to send request");
free(buffer);
packet_write(stdin_fd, "%s %s%chost=%s%c", serv, repo, 0,
vhost, 0);
}
static int run_child(const char *arg, const char *service)

Просмотреть файл

@ -217,7 +217,7 @@ static void print_var_int(const char *var, int val)
static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
{
int cnt, flags = info->flags;
char hex[41] = "";
char hex[GIT_SHA1_HEXSZ + 1] = "";
struct commit_list *tried;
struct rev_info *revs = info->revs;
@ -242,7 +242,7 @@ static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
cnt = reaches;
if (revs->commits)
strcpy(hex, sha1_to_hex(revs->commits->item->object.sha1));
sha1_to_hex_r(hex, revs->commits->item->object.sha1);
if (flags & BISECT_SHOW_ALL) {
traverse_commit_list(revs, show_commit, show_object, info);

Просмотреть файл

@ -743,6 +743,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
fake_av[1] = NULL;
av = fake_av;
ac = 1;
if (!*av)
die("no branches given, and HEAD is not valid");
}
if (ac != 1)
die("--reflog option needs one branch name");

Просмотреть файл

@ -12,7 +12,7 @@ static char *create_temp_file(unsigned char *sha1)
if (!buf || type != OBJ_BLOB)
die("unable to read blob object %s", sha1_to_hex(sha1));
strcpy(path, ".merge_file_XXXXXX");
xsnprintf(path, sizeof(path), ".merge_file_XXXXXX");
fd = xmkstemp(path);
if (write_in_full(fd, buf, size) != size)
die_errno("unable to write temp-file");

Просмотреть файл

@ -49,15 +49,14 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
__attribute__((format (printf, 1, 2)))
static void error_clnt(const char *fmt, ...)
{
char buf[1024];
struct strbuf buf = STRBUF_INIT;
va_list params;
int len;
va_start(params, fmt);
len = vsprintf(buf, fmt, params);
strbuf_vaddf(&buf, fmt, params);
va_end(params);
send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
die("sent error to the client: %s", buf);
send_sideband(1, 3, buf.buf, buf.len, LARGE_PACKET_MAX);
die("sent error to the client: %s", buf.buf);
}
static ssize_t process_input(int child_fd, int band)

Просмотреть файл

@ -200,8 +200,8 @@ static int deflate_to_pack(struct bulk_checkin_state *state,
if (seekback == (off_t) -1)
return error("cannot find the current offset");
header_len = sprintf((char *)obuf, "%s %" PRIuMAX,
typename(type), (uintmax_t)size) + 1;
header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX,
typename(type), (uintmax_t)size) + 1;
git_SHA1_Init(&ctx);
git_SHA1_Update(&ctx, obuf, header_len);

35
cache.h
Просмотреть файл

@ -724,6 +724,8 @@ extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
__attribute__((format (printf, 3, 4)));
extern void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
__attribute__((format (printf, 2, 3)));
extern char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
__attribute__((format (printf, 2, 3)));
extern void strbuf_git_path_submodule(struct strbuf *sb, const char *path,
const char *fmt, ...)
__attribute__((format (printf, 3, 4)));
@ -784,7 +786,24 @@ extern char *sha1_pack_name(const unsigned char *sha1);
*/
extern char *sha1_pack_index_name(const unsigned char *sha1);
extern const char *find_unique_abbrev(const unsigned char *sha1, int);
/*
* Return an abbreviated sha1 unique within this repository's object database.
* The result will be at least `len` characters long, and will be NUL
* terminated.
*
* The non-`_r` version returns a static buffer which will be overwritten by
* subsequent calls.
*
* The `_r` variant writes to a buffer supplied by the caller, which must be at
* least `GIT_SHA1_HEXSZ + 1` bytes. The return value is the number of bytes
* written (excluding the NUL terminator).
*
* Note that while this version avoids the static buffer, it is not fully
* reentrant, as it calls into other non-reentrant git code.
*/
extern const char *find_unique_abbrev(const unsigned char *sha1, int len);
extern int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len);
extern const unsigned char null_sha1[GIT_SHA1_RAWSZ];
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
@ -1066,6 +1085,18 @@ extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
extern int get_oid_hex(const char *hex, struct object_id *sha1);
/*
* Convert a binary sha1 to its hex equivalent. The `_r` variant is reentrant,
* and writes the NUL-terminated output to the buffer `out`, which must be at
* least `GIT_SHA1_HEXSZ + 1` bytes, and returns a pointer to out for
* convenience.
*
* The non-`_r` variant returns a static buffer, but uses a ring of 4
* buffers, making it safe to make multiple calls for a single statement, like:
*
* printf("%s -> %s", sha1_to_hex(one), sha1_to_hex(two));
*/
extern char *sha1_to_hex_r(char *out, const unsigned char *sha1);
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as sha1_to_hex */
@ -1280,7 +1311,7 @@ extern void close_all_packs(void);
extern void unuse_pack(struct pack_window **);
extern void free_pack_by_name(const char *);
extern void clear_delta_base_cache(void);
extern struct packed_git *add_packed_git(const char *, int, int);
extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
/*
* Return the SHA-1 of the nth object within the specified packfile.

46
color.c
Просмотреть файл

@ -145,27 +145,34 @@ int color_parse(const char *value, char *dst)
return color_parse_mem(value, strlen(value), dst);
}
void color_set(char *dst, const char *color_bytes)
{
xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
}
/*
* Write the ANSI color codes for "c" to "out"; the string should
* already have the ANSI escape code in it. "out" should have enough
* space in it to fit any color.
*/
static char *color_output(char *out, const struct color *c, char type)
static char *color_output(char *out, int len, const struct color *c, char type)
{
switch (c->type) {
case COLOR_UNSPECIFIED:
case COLOR_NORMAL:
break;
case COLOR_ANSI:
if (len < 2)
die("BUG: color parsing ran out of space");
*out++ = type;
*out++ = '0' + c->value;
break;
case COLOR_256:
out += sprintf(out, "%c8;5;%d", type, c->value);
out += xsnprintf(out, len, "%c8;5;%d", type, c->value);
break;
case COLOR_RGB:
out += sprintf(out, "%c8;2;%d;%d;%d", type,
c->red, c->green, c->blue);
out += xsnprintf(out, len, "%c8;2;%d;%d;%d", type,
c->red, c->green, c->blue);
break;
}
return out;
@ -180,12 +187,13 @@ int color_parse_mem(const char *value, int value_len, char *dst)
{
const char *ptr = value;
int len = value_len;
char *end = dst + COLOR_MAXLEN;
unsigned int attr = 0;
struct color fg = { COLOR_UNSPECIFIED };
struct color bg = { COLOR_UNSPECIFIED };
if (!strncasecmp(value, "reset", len)) {
strcpy(dst, GIT_COLOR_RESET);
xsnprintf(dst, end - dst, GIT_COLOR_RESET);
return 0;
}
@ -224,12 +232,19 @@ int color_parse_mem(const char *value, int value_len, char *dst)
goto bad;
}
#undef OUT
#define OUT(x) do { \
if (dst == end) \
die("BUG: color parsing ran out of space"); \
*dst++ = (x); \
} while(0)
if (attr || !color_empty(&fg) || !color_empty(&bg)) {
int sep = 0;
int i;
*dst++ = '\033';
*dst++ = '[';
OUT('\033');
OUT('[');
for (i = 0; attr; i++) {
unsigned bit = (1 << i);
@ -237,27 +252,28 @@ int color_parse_mem(const char *value, int value_len, char *dst)
continue;
attr &= ~bit;
if (sep++)
*dst++ = ';';
dst += sprintf(dst, "%d", i);
OUT(';');
dst += xsnprintf(dst, end - dst, "%d", i);
}
if (!color_empty(&fg)) {
if (sep++)
*dst++ = ';';
OUT(';');
/* foreground colors are all in the 3x range */
dst = color_output(dst, &fg, '3');
dst = color_output(dst, end - dst, &fg, '3');
}
if (!color_empty(&bg)) {
if (sep++)
*dst++ = ';';
OUT(';');
/* background colors are all in the 4x range */
dst = color_output(dst, &bg, '4');
dst = color_output(dst, end - dst, &bg, '4');
}
*dst++ = 'm';
OUT('m');
}
*dst = 0;
OUT(0);
return 0;
bad:
return error(_("invalid color value: %.*s"), value_len, value);
#undef OUT
}
int git_config_colorbool(const char *var, const char *value)

Просмотреть файл

@ -75,6 +75,13 @@ extern int color_stdout_is_tty;
int git_color_config(const char *var, const char *value, void *cb);
int git_color_default_config(const char *var, const char *value, void *cb);
/*
* Set the color buffer (which must be COLOR_MAXLEN bytes)
* to the raw color bytes; this is useful for initializing
* default color variables.
*/
void color_set(char *dst, const char *color_bytes);
int git_config_colorbool(const char *var, const char *value);
int want_color(int var);
int color_parse(const char *value, char *dst);

Просмотреть файл

@ -16,6 +16,6 @@ const char *githstrerror(int err)
case TRY_AGAIN:
return "Non-authoritative \"host not found\", or SERVERFAIL";
}
sprintf(buffer, "Name resolution error %d", err);
snprintf(buffer, sizeof(buffer), "Name resolution error %d", err);
return buffer;
}

Просмотреть файл

@ -53,11 +53,11 @@ inet_ntop4(const u_char *src, char *dst, size_t size)
nprinted = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
if (nprinted < 0)
return (NULL); /* we assume "errno" was set by "snprintf()" */
if ((size_t)nprinted > size) {
if ((size_t)nprinted >= size) {
errno = ENOSPC;
return (NULL);
}
strcpy(dst, tmp);
strlcpy(dst, tmp, size);
return (dst);
}
@ -154,7 +154,7 @@ inet_ntop6(const u_char *src, char *dst, size_t size)
errno = ENOSPC;
return (NULL);
}
strcpy(dst, tmp);
strlcpy(dst, tmp, size);
return (dst);
}
#endif

Просмотреть файл

@ -2133,9 +2133,11 @@ int uname(struct utsname *buf)
{
DWORD v = GetVersion();
memset(buf, 0, sizeof(*buf));
strcpy(buf->sysname, "Windows");
sprintf(buf->release, "%u.%u", v & 0xff, (v >> 8) & 0xff);
xsnprintf(buf->sysname, sizeof(buf->sysname), "Windows");
xsnprintf(buf->release, sizeof(buf->release),
"%u.%u", v & 0xff, (v >> 8) & 0xff);
/* assuming NT variants only.. */
sprintf(buf->version, "%u", (v >> 16) & 0x7fff);
xsnprintf(buf->version, sizeof(buf->version),
"%u", (v >> 16) & 0x7fff);
return 0;
}

Просмотреть файл

@ -957,8 +957,9 @@ char *strdup(const char *s1)
{
char *s2 = 0;
if (s1) {
s2 = malloc(strlen(s1) + 1);
strcpy(s2, s1);
size_t len = strlen(s1) + 1;
s2 = malloc(len);
memcpy(s2, s1, len);
}
return s2;
}

Просмотреть файл

@ -36,24 +36,26 @@ static size_t has_non_ascii(const char *s, size_t maxlen, size_t *strlen_c)
}
void probe_utf8_pathname_composition(char *path, int len)
void probe_utf8_pathname_composition(void)
{
struct strbuf path = STRBUF_INIT;
static const char *auml_nfc = "\xc3\xa4";
static const char *auml_nfd = "\x61\xcc\x88";
int output_fd;
if (precomposed_unicode != -1)
return; /* We found it defined in the global config, respect it */
strcpy(path + len, auml_nfc);
output_fd = open(path, O_CREAT|O_EXCL|O_RDWR, 0600);
git_path_buf(&path, "%s", auml_nfc);
output_fd = open(path.buf, O_CREAT|O_EXCL|O_RDWR, 0600);
if (output_fd >= 0) {
close(output_fd);
strcpy(path + len, auml_nfd);
precomposed_unicode = access(path, R_OK) ? 0 : 1;
git_path_buf(&path, "%s", auml_nfd);
precomposed_unicode = access(path.buf, R_OK) ? 0 : 1;
git_config_set("core.precomposeunicode", precomposed_unicode ? "true" : "false");
strcpy(path + len, auml_nfc);
if (unlink(path))
die_errno(_("failed to unlink '%s'"), path);
git_path_buf(&path, "%s", auml_nfc);
if (unlink(path.buf))
die_errno(_("failed to unlink '%s'"), path.buf);
}
strbuf_release(&path);
}
@ -139,9 +141,8 @@ struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)
size_t inleft = namelenz;
char *outpos = &prec_dir->dirent_nfc->d_name[0];
size_t outsz = prec_dir->dirent_nfc->max_name_len;
size_t cnt;
errno = 0;
cnt = iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
iconv(prec_dir->ic_precompose, &cp, &inleft, &outpos, &outsz);
if (errno || inleft) {
/*
* iconv() failed and errno could be E2BIG, EILSEQ, EINVAL, EBADF

Просмотреть файл

@ -27,7 +27,7 @@ typedef struct {
} PREC_DIR;
void precompose_argv(int argc, const char **argv);
void probe_utf8_pathname_composition(char *, int);
void probe_utf8_pathname_composition(void);
PREC_DIR *precompose_utf8_opendir(const char *dirname);
struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *dirp);

Просмотреть файл

@ -539,7 +539,7 @@ void winansi_init(void)
return;
/* create a named pipe to communicate with the console thread */
sprintf(name, "\\\\.\\pipe\\winansi%lu", GetCurrentProcessId());
xsnprintf(name, sizeof(name), "\\\\.\\pipe\\winansi%lu", GetCurrentProcessId());
hwrite = CreateNamedPipe(name, PIPE_ACCESS_OUTBOUND,
PIPE_TYPE_BYTE | PIPE_WAIT, 1, BUFFER_SIZE, 0, 0, NULL);
if (hwrite == INVALID_HANDLE_VALUE)

Просмотреть файл

@ -166,7 +166,6 @@ endif
ifeq ($(uname_O),Cygwin)
ifeq ($(shell expr "$(uname_R)" : '1\.[1-6]\.'),4)
NO_D_TYPE_IN_DIRENT = YesPlease
NO_D_INO_IN_DIRENT = YesPlease
NO_STRCASESTR = YesPlease
NO_MEMMEM = YesPlease
NO_MKSTEMPS = YesPlease
@ -370,7 +369,6 @@ ifeq ($(uname_S),Windows)
NO_POSIX_GOODIES = UnfortunatelyYes
NATIVE_CRLF = YesPlease
DEFAULT_HELP_FORMAT = html
NO_D_INO_IN_DIRENT = YesPlease
CC = compat/vcbuild/scripts/clink.pl
AR = compat/vcbuild/scripts/lib.pl
@ -520,7 +518,6 @@ ifneq (,$(findstring MINGW,$(uname_S)))
NO_INET_NTOP = YesPlease
NO_POSIX_GOODIES = UnfortunatelyYes
DEFAULT_HELP_FORMAT = html
NO_D_INO_IN_DIRENT = YesPlease
COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -D_USE_32BIT_TIME_T -DNOGDI -Icompat -Icompat/win32
COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
COMPAT_OBJS += compat/mingw.o compat/winansi.o \

Просмотреть файл

@ -767,13 +767,6 @@ elif test x$ac_cv_member_struct_stat_st_mtim_tv_nsec != xyes; then
GIT_CONF_SUBST([NO_NSEC])
fi
#
# Define NO_D_INO_IN_DIRENT if you don't have d_ino in your struct dirent.
AC_CHECK_MEMBER(struct dirent.d_ino,
[NO_D_INO_IN_DIRENT=],
[NO_D_INO_IN_DIRENT=YesPlease],
[#include <dirent.h>])
GIT_CONF_SUBST([NO_D_INO_IN_DIRENT])
#
# Define NO_D_TYPE_IN_DIRENT if your platform defines DT_UNKNOWN but lacks
# d_type in struct dirent (latest Cygwin -- will be fixed soonish).
AC_CHECK_MEMBER(struct dirent.d_type,

Просмотреть файл

@ -333,7 +333,7 @@ static const char *ai_name(const struct addrinfo *ai)
static char addr[NI_MAXHOST];
if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
NI_NUMERICHOST) != 0)
strcpy(addr, "(unknown)");
xsnprintf(addr, sizeof(addr), "(unknown)");
return addr;
}

Просмотреть файл

@ -1289,7 +1289,8 @@ static struct stream_filter *ident_filter(const unsigned char *sha1)
{
struct ident_filter *ident = xmalloc(sizeof(*ident));
sprintf(ident->ident, ": %s $", sha1_to_hex(sha1));
xsnprintf(ident->ident, sizeof(ident->ident),
": %s $", sha1_to_hex(sha1));
strbuf_init(&ident->left, 0);
ident->filter.vtbl = &ident_vtbl;
ident->state = 0;

Просмотреть файл

@ -811,8 +811,6 @@ static char **cld_argv;
static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
{
struct child_process cld = CHILD_PROCESS_INIT;
char addrbuf[300] = "REMOTE_ADDR=", portbuf[300];
char *env[] = { addrbuf, portbuf, NULL };
if (max_connections && live_children >= max_connections) {
kill_some_child();
@ -826,27 +824,23 @@ static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
}
if (addr->sa_family == AF_INET) {
char buf[128] = "";
struct sockaddr_in *sin_addr = (void *) addr;
inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf + 12,
sizeof(addrbuf) - 12);
snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
ntohs(sin_addr->sin_port));
inet_ntop(addr->sa_family, &sin_addr->sin_addr, buf, sizeof(buf));
argv_array_pushf(&cld.env_array, "REMOTE_ADDR=%s", buf);
argv_array_pushf(&cld.env_array, "REMOTE_PORT=%d",
ntohs(sin_addr->sin_port));
#ifndef NO_IPV6
} else if (addr->sa_family == AF_INET6) {
char buf[128] = "";
struct sockaddr_in6 *sin6_addr = (void *) addr;
char *buf = addrbuf + 12;
*buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf,
sizeof(addrbuf) - 13);
strcat(buf, "]");
snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
ntohs(sin6_addr->sin6_port));
inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(buf));
argv_array_pushf(&cld.env_array, "REMOTE_ADDR=[%s]", buf);
argv_array_pushf(&cld.env_array, "REMOTE_PORT=%d",
ntohs(sin6_addr->sin6_port));
#endif
}
cld.env = (const char **)env;
cld.argv = (const char **)cld_argv;
cld.in = incoming;
cld.out = dup(incoming);
@ -901,7 +895,7 @@ static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len);
break;
default:
strcpy(ip, "<unknown>");
xsnprintf(ip, sizeof(ip), "<unknown>");
}
return ip;
}
@ -916,7 +910,7 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
int gai;
long flags;
sprintf(pbuf, "%d", listen_port);
xsnprintf(pbuf, sizeof(pbuf), "%d", listen_port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

Просмотреть файл

@ -136,15 +136,13 @@ static int queue_diff(struct diff_options *o,
if (name1) {
strbuf_addstr(&buffer1, name1);
if (buffer1.len && buffer1.buf[buffer1.len - 1] != '/')
strbuf_addch(&buffer1, '/');
strbuf_complete(&buffer1, '/');
len1 = buffer1.len;
}
if (name2) {
strbuf_addstr(&buffer2, name2);
if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/')
strbuf_addch(&buffer2, '/');
strbuf_complete(&buffer2, '/');
len2 = buffer2.len;
}

21
diff.c
Просмотреть файл

@ -322,7 +322,7 @@ static struct diff_tempfile {
*/
const char *name;
char hex[41];
char hex[GIT_SHA1_HEXSZ + 1];
char mode[10];
/*
@ -2882,9 +2882,8 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
die_errno("unable to write temp-file");
close_tempfile(&temp->tempfile);
temp->name = get_tempfile_path(&temp->tempfile);
strcpy(temp->hex, sha1_to_hex(sha1));
temp->hex[40] = 0;
sprintf(temp->mode, "%06o", mode);
sha1_to_hex_r(temp->hex, sha1);
xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
strbuf_release(&buf);
strbuf_release(&template);
free(path_dup);
@ -2901,8 +2900,8 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
* a '+' entry produces this for file-1.
*/
temp->name = "/dev/null";
strcpy(temp->hex, ".");
strcpy(temp->mode, ".");
xsnprintf(temp->hex, sizeof(temp->hex), ".");
xsnprintf(temp->mode, sizeof(temp->mode), ".");
return temp;
}
@ -2930,16 +2929,16 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
/* we can borrow from the file in the work tree */
temp->name = name;
if (!one->sha1_valid)
strcpy(temp->hex, sha1_to_hex(null_sha1));
sha1_to_hex_r(temp->hex, null_sha1);
else
strcpy(temp->hex, sha1_to_hex(one->sha1));
sha1_to_hex_r(temp->hex, one->sha1);
/* Even though we may sometimes borrow the
* contents from the work tree, we always want
* one->mode. mode is trustworthy even when
* !(one->sha1_valid), as long as
* DIFF_FILE_VALID(one).
*/
sprintf(temp->mode, "%06o", one->mode);
xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
}
return temp;
}
@ -4085,9 +4084,9 @@ const char *diff_unique_abbrev(const unsigned char *sha1, int len)
if (abblen < 37) {
static char hex[41];
if (len < abblen && abblen <= len + 2)
sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
else
sprintf(hex, "%s...", abbrev);
xsnprintf(hex, sizeof(hex), "%s...", abbrev);
return hex;
}
return sha1_to_hex(sha1);

6
dir.c
Просмотреть файл

@ -1596,8 +1596,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,
}
strbuf_addstr(path, cdir->ucd->name);
/* treat_one_path() does this before it calls treat_directory() */
if (path->buf[path->len - 1] != '/')
strbuf_addch(path, '/');
strbuf_complete(path, '/');
if (cdir->ucd->check_only)
/*
* check_only is set as a result of treat_directory() getting
@ -2212,8 +2211,7 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
else
return -1;
}
if (path->buf[original_len - 1] != '/')
strbuf_addch(path, '/');
strbuf_complete(path, '/');
len = path->len;
while ((e = readdir(dir)) != NULL) {

Просмотреть файл

@ -96,8 +96,8 @@ static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempf
{
int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
if (to_tempfile) {
strcpy(path, symlink
? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s",
symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
return mkstemp(path);
} else {
return create_file(path, !symlink ? ce->ce_mode : 0666);

Просмотреть файл

@ -143,11 +143,8 @@ static char *git_path_from_env(const char *envvar, const char *git_dir,
const char *path, int *fromenv)
{
const char *value = getenv(envvar);
if (!value) {
char *buf = xmalloc(strlen(git_dir) + strlen(path) + 2);
sprintf(buf, "%s/%s", git_dir, path);
return buf;
}
if (!value)
return xstrfmt("%s/%s", git_dir, path);
if (fromenv)
*fromenv = 1;
return xstrdup(value);

Просмотреть файл

@ -644,8 +644,9 @@ static void *pool_calloc(size_t count, size_t size)
static char *pool_strdup(const char *s)
{
char *r = pool_alloc(strlen(s) + 1);
strcpy(r, s);
size_t len = strlen(s) + 1;
char *r = pool_alloc(len);
memcpy(r, s, len);
return r;
}
@ -702,7 +703,7 @@ static struct atom_str *to_atom(const char *s, unsigned short len)
c = pool_alloc(sizeof(struct atom_str) + len + 1);
c->str_len = len;
strncpy(c->str_dat, s, len);
memcpy(c->str_dat, s, len);
c->str_dat[len] = 0;
c->next_atom = atom_table[hc];
atom_table[hc] = c;
@ -863,13 +864,15 @@ static void start_packfile(void)
{
static char tmp_file[PATH_MAX];
struct packed_git *p;
int namelen;
struct pack_header hdr;
int pack_fd;
pack_fd = odb_mkstemp(tmp_file, sizeof(tmp_file),
"pack/tmp_pack_XXXXXX");
p = xcalloc(1, sizeof(*p) + strlen(tmp_file) + 2);
strcpy(p->pack_name, tmp_file);
namelen = strlen(tmp_file) + 2;
p = xcalloc(1, sizeof(*p) + namelen);
xsnprintf(p->pack_name, namelen, "%s", tmp_file);
p->pack_fd = pack_fd;
p->do_not_close = 1;
pack_file = sha1fd(pack_fd, p->pack_name);
@ -1035,8 +1038,8 @@ static int store_object(
git_SHA_CTX c;
git_zstream s;
hdrlen = sprintf((char *)hdr,"%s %lu", typename(type),
(unsigned long)dat->len) + 1;
hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu",
typename(type), (unsigned long)dat->len) + 1;
git_SHA1_Init(&c);
git_SHA1_Update(&c, hdr, hdrlen);
git_SHA1_Update(&c, dat->buf, dat->len);

Просмотреть файл

@ -681,11 +681,10 @@ static int get_pack(struct fetch_pack_args *args,
int xd[2], char **pack_lockfile)
{
struct async demux;
const char *argv[22];
char keep_arg[256];
char hdr_arg[256];
const char **av, *cmd_name;
int do_keep = args->keep_pack;
const char *cmd_name;
struct pack_header header;
int pass_header = 0;
struct child_process cmd = CHILD_PROCESS_INIT;
int ret;
@ -705,17 +704,11 @@ static int get_pack(struct fetch_pack_args *args,
else
demux.out = xd[0];
cmd.argv = argv;
av = argv;
*hdr_arg = 0;
if (!args->keep_pack && unpack_limit) {
struct pack_header header;
if (read_pack_header(demux.out, &header))
die("protocol error: bad pack header");
snprintf(hdr_arg, sizeof(hdr_arg),
"--pack_header=%"PRIu32",%"PRIu32,
ntohl(header.hdr_version), ntohl(header.hdr_entries));
pass_header = 1;
if (ntohl(header.hdr_entries) < unpack_limit)
do_keep = 0;
else
@ -723,44 +716,49 @@ static int get_pack(struct fetch_pack_args *args,
}
if (alternate_shallow_file) {
*av++ = "--shallow-file";
*av++ = alternate_shallow_file;
argv_array_push(&cmd.args, "--shallow-file");
argv_array_push(&cmd.args, alternate_shallow_file);
}
if (do_keep) {
if (pack_lockfile)
cmd.out = -1;
*av++ = cmd_name = "index-pack";
*av++ = "--stdin";
cmd_name = "index-pack";
argv_array_push(&cmd.args, cmd_name);
argv_array_push(&cmd.args, "--stdin");
if (!args->quiet && !args->no_progress)
*av++ = "-v";
argv_array_push(&cmd.args, "-v");
if (args->use_thin_pack)
*av++ = "--fix-thin";
argv_array_push(&cmd.args, "--fix-thin");
if (args->lock_pack || unpack_limit) {
int s = sprintf(keep_arg,
"--keep=fetch-pack %"PRIuMAX " on ", (uintmax_t) getpid());
if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
strcpy(keep_arg + s, "localhost");
*av++ = keep_arg;
char hostname[256];
if (gethostname(hostname, sizeof(hostname)))
xsnprintf(hostname, sizeof(hostname), "localhost");
argv_array_pushf(&cmd.args,
"--keep=fetch-pack %"PRIuMAX " on %s",
(uintmax_t)getpid(), hostname);
}
if (args->check_self_contained_and_connected)
*av++ = "--check-self-contained-and-connected";
argv_array_push(&cmd.args, "--check-self-contained-and-connected");
}
else {
*av++ = cmd_name = "unpack-objects";
cmd_name = "unpack-objects";
argv_array_push(&cmd.args, cmd_name);
if (args->quiet || args->no_progress)
*av++ = "-q";
argv_array_push(&cmd.args, "-q");
args->check_self_contained_and_connected = 0;
}
if (*hdr_arg)
*av++ = hdr_arg;
if (pass_header)
argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
ntohl(header.hdr_version),
ntohl(header.hdr_entries));
if (fetch_fsck_objects >= 0
? fetch_fsck_objects
: transfer_fsck_objects >= 0
? transfer_fsck_objects
: 0)
*av++ = "--strict";
*av++ = NULL;
argv_array_push(&cmd.args, "--strict");
cmd.in = demux.out;
cmd.git_cmd = 1;

Просмотреть файл

@ -229,7 +229,7 @@ typedef unsigned long uintptr_t;
#else
#define precompose_str(in,i_nfd2nfc)
#define precompose_argv(c,v)
#define probe_utf8_pathname_composition(a,b)
#define probe_utf8_pathname_composition()
#endif
#ifdef MKDIR_WO_TRAILING_SLASH
@ -744,6 +744,9 @@ static inline size_t xsize_t(off_t len)
return (size_t)len;
}
__attribute__((format (printf, 3, 4)))
extern int xsnprintf(char *dst, size_t max, const char *fmt, ...);
/* in ctype.c, for kwset users */
extern const unsigned char tolower_trans_tbl[256];

36
grep.c
Просмотреть файл

@ -31,14 +31,14 @@ void init_grep_defaults(void)
opt->max_depth = -1;
opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
opt->extended_regexp_option = 0;
strcpy(opt->color_context, "");
strcpy(opt->color_filename, "");
strcpy(opt->color_function, "");
strcpy(opt->color_lineno, "");
strcpy(opt->color_match_context, GIT_COLOR_BOLD_RED);
strcpy(opt->color_match_selected, GIT_COLOR_BOLD_RED);
strcpy(opt->color_selected, "");
strcpy(opt->color_sep, GIT_COLOR_CYAN);
color_set(opt->color_context, "");
color_set(opt->color_filename, "");
color_set(opt->color_function, "");
color_set(opt->color_lineno, "");
color_set(opt->color_match_context, GIT_COLOR_BOLD_RED);
color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED);
color_set(opt->color_selected, "");
color_set(opt->color_sep, GIT_COLOR_CYAN);
opt->color = -1;
}
@ -151,14 +151,14 @@ void grep_init(struct grep_opt *opt, const char *prefix)
opt->regflags = def->regflags;
opt->relative = def->relative;
strcpy(opt->color_context, def->color_context);
strcpy(opt->color_filename, def->color_filename);
strcpy(opt->color_function, def->color_function);
strcpy(opt->color_lineno, def->color_lineno);
strcpy(opt->color_match_context, def->color_match_context);
strcpy(opt->color_match_selected, def->color_match_selected);
strcpy(opt->color_selected, def->color_selected);
strcpy(opt->color_sep, def->color_sep);
color_set(opt->color_context, def->color_context);
color_set(opt->color_filename, def->color_filename);
color_set(opt->color_function, def->color_function);
color_set(opt->color_lineno, def->color_lineno);
color_set(opt->color_match_context, def->color_match_context);
color_set(opt->color_match_selected, def->color_match_selected);
color_set(opt->color_selected, def->color_selected);
color_set(opt->color_sep, def->color_sep);
}
void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)
@ -306,9 +306,9 @@ static NORETURN void compile_regexp_failed(const struct grep_pat *p,
char where[1024];
if (p->no)
sprintf(where, "In '%s' at %d, ", p->origin, p->no);
xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no);
else if (p->origin)
sprintf(where, "%s, ", p->origin);
xsnprintf(where, sizeof(where), "%s, ", p->origin);
else
where[0] = 0;

13
hex.c
Просмотреть файл

@ -61,12 +61,10 @@ int get_oid_hex(const char *hex, struct object_id *oid)
return get_sha1_hex(hex, oid->hash);
}
char *sha1_to_hex(const unsigned char *sha1)
char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
{
static int bufno;
static char hexbuffer[4][GIT_SHA1_HEXSZ + 1];
static const char hex[] = "0123456789abcdef";
char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
char *buf = buffer;
int i;
for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
@ -79,6 +77,13 @@ char *sha1_to_hex(const unsigned char *sha1)
return buffer;
}
char *sha1_to_hex(const unsigned char *sha1)
{
static int bufno;
static char hexbuffer[4][GIT_SHA1_HEXSZ + 1];
return sha1_to_hex_r(hexbuffer[3 & ++bufno], sha1);
}
char *oid_to_hex(const struct object_id *oid)
{
return sha1_to_hex(oid->hash);

Просмотреть файл

@ -10,6 +10,7 @@
#include "remote.h"
#include "list-objects.h"
#include "sigchain.h"
#include "argv-array.h"
#ifdef EXPAT_NEEDS_XMLPARSE_H
#include <xmlparse.h>
@ -361,7 +362,7 @@ static void start_put(struct transfer_request *request)
git_zstream stream;
unpacked = read_sha1_file(request->obj->sha1, &type, &len);
hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
/* Set it up */
git_deflate_init(&stream, zlib_compression_level);
@ -786,21 +787,21 @@ xml_start_tag(void *userData, const char *name, const char **atts)
{
struct xml_ctx *ctx = (struct xml_ctx *)userData;
const char *c = strchr(name, ':');
int new_len;
int old_namelen, new_len;
if (c == NULL)
c = name;
else
c++;
new_len = strlen(ctx->name) + strlen(c) + 2;
old_namelen = strlen(ctx->name);
new_len = old_namelen + strlen(c) + 2;
if (new_len > ctx->len) {
ctx->name = xrealloc(ctx->name, new_len);
ctx->len = new_len;
}
strcat(ctx->name, ".");
strcat(ctx->name, c);
xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
free(ctx->cdata);
ctx->cdata = NULL;
@ -881,7 +882,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
free(escaped);
sprintf(timeout_header, "Timeout: Second-%ld", timeout);
xsnprintf(timeout_header, sizeof(timeout_header), "Timeout: Second-%ld", timeout);
dav_headers = curl_slist_append(dav_headers, timeout_header);
dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
@ -1459,8 +1460,6 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)
{
struct strbuf *buf = (struct strbuf *)ls->userData;
struct object *o;
int len;
char *ref_info;
struct ref *ref;
ref = alloc_ref(ls->dentry_name);
@ -1484,23 +1483,14 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)
return;
}
len = strlen(ls->dentry_name) + 42;
ref_info = xcalloc(len + 1, 1);
sprintf(ref_info, "%s %s\n",
sha1_to_hex(ref->old_sha1), ls->dentry_name);
fwrite_buffer(ref_info, 1, len, buf);
free(ref_info);
strbuf_addf(buf, "%s\t%s\n",
sha1_to_hex(ref->old_sha1), ls->dentry_name);
if (o->type == OBJ_TAG) {
o = deref_tag(o, ls->dentry_name, 0);
if (o) {
len = strlen(ls->dentry_name) + 45;
ref_info = xcalloc(len + 1, 1);
sprintf(ref_info, "%s %s^{}\n",
sha1_to_hex(o->sha1), ls->dentry_name);
fwrite_buffer(ref_info, 1, len, buf);
free(ref_info);
}
if (o)
strbuf_addf(buf, "%s\t%s^{}\n",
sha1_to_hex(o->sha1), ls->dentry_name);
}
free(ref);
}
@ -1866,10 +1856,7 @@ int main(int argc, char **argv)
new_refs = 0;
for (ref = remote_refs; ref; ref = ref->next) {
char old_hex[60], *new_hex;
const char *commit_argv[5];
int commit_argc;
char *new_sha1_hex, *old_sha1_hex;
struct argv_array commit_argv = ARGV_ARRAY_INIT;
if (!ref->peer_ref)
continue;
@ -1923,13 +1910,12 @@ int main(int argc, char **argv)
}
hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
new_refs++;
strcpy(old_hex, sha1_to_hex(ref->old_sha1));
new_hex = sha1_to_hex(ref->new_sha1);
fprintf(stderr, "updating '%s'", ref->name);
if (strcmp(ref->name, ref->peer_ref->name))
fprintf(stderr, " using '%s'", ref->peer_ref->name);
fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
fprintf(stderr, "\n from %s\n to %s\n",
sha1_to_hex(ref->old_sha1), sha1_to_hex(ref->new_sha1));
if (dry_run) {
if (helper_status)
printf("ok %s\n", ref->name);
@ -1948,27 +1934,15 @@ int main(int argc, char **argv)
}
/* Set up revision info for this refspec */
commit_argc = 3;
new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
old_sha1_hex = NULL;
commit_argv[1] = "--objects";
commit_argv[2] = new_sha1_hex;
if (!push_all && !is_null_sha1(ref->old_sha1)) {
old_sha1_hex = xmalloc(42);
sprintf(old_sha1_hex, "^%s",
sha1_to_hex(ref->old_sha1));
commit_argv[3] = old_sha1_hex;
commit_argc++;
}
commit_argv[commit_argc] = NULL;
argv_array_push(&commit_argv, ""); /* ignored */
argv_array_push(&commit_argv, "--objects");
argv_array_push(&commit_argv, sha1_to_hex(ref->new_sha1));
if (!push_all && !is_null_sha1(ref->old_sha1))
argv_array_pushf(&commit_argv, "^%s",
sha1_to_hex(ref->old_sha1));
init_revisions(&revs, setup_git_directory());
setup_revisions(commit_argc, commit_argv, &revs, NULL);
setup_revisions(commit_argv.argc, commit_argv.argv, &revs, NULL);
revs.edge_hint = 0; /* just in case */
free(new_sha1_hex);
if (old_sha1_hex) {
free(old_sha1_hex);
commit_argv[1] = NULL;
}
/* Generate a list of objects that need to be pushed */
pushing = 0;
@ -1997,6 +1971,7 @@ int main(int argc, char **argv)
printf("%s %s\n", !rc ? "ok" : "error", ref->name);
unlock_remote(ref_lock);
check_locks();
argv_array_clear(&commit_argv);
}
/* Update remote server info if appropriate */

Просмотреть файл

@ -29,7 +29,7 @@ struct object_request {
struct alternates_request {
struct walker *walker;
const char *base;
char *url;
struct strbuf *url;
struct strbuf *buffer;
struct active_request_slot *slot;
int http_specific;
@ -195,10 +195,11 @@ static void process_alternates_response(void *callback_data)
/* Try reusing the slot to get non-http alternates */
alt_req->http_specific = 0;
sprintf(alt_req->url, "%s/objects/info/alternates",
base);
strbuf_reset(alt_req->url);
strbuf_addf(alt_req->url, "%s/objects/info/alternates",
base);
curl_easy_setopt(slot->curl, CURLOPT_URL,
alt_req->url);
alt_req->url->buf);
active_requests++;
slot->in_use = 1;
if (slot->finished != NULL)
@ -312,7 +313,7 @@ static void process_alternates_response(void *callback_data)
static void fetch_alternates(struct walker *walker, const char *base)
{
struct strbuf buffer = STRBUF_INIT;
char *url;
struct strbuf url = STRBUF_INIT;
struct active_request_slot *slot;
struct alternates_request alt_req;
struct walker_data *cdata = walker->data;
@ -338,7 +339,7 @@ static void fetch_alternates(struct walker *walker, const char *base)
if (walker->get_verbosely)
fprintf(stderr, "Getting alternates list for %s\n", base);
url = xstrfmt("%s/objects/info/http-alternates", base);
strbuf_addf(&url, "%s/objects/info/http-alternates", base);
/*
* Use a callback to process the result, since another request
@ -351,10 +352,10 @@ static void fetch_alternates(struct walker *walker, const char *base)
curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
curl_easy_setopt(slot->curl, CURLOPT_URL, url);
curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf);
alt_req.base = base;
alt_req.url = url;
alt_req.url = &url;
alt_req.buffer = &buffer;
alt_req.http_specific = 1;
alt_req.slot = slot;
@ -365,7 +366,7 @@ static void fetch_alternates(struct walker *walker, const char *base)
cdata->got_alternates = -1;
strbuf_release(&buffer);
free(url);
strbuf_release(&url);
}
static int fetch_indices(struct walker *walker, struct alt_base *repo)

13
http.c
Просмотреть файл

@ -1122,7 +1122,7 @@ static void write_accept_language(struct strbuf *buf)
decimal_places++, max_q *= 10)
;
sprintf(q_format, ";q=0.%%0%dd", decimal_places);
xsnprintf(q_format, sizeof(q_format), ";q=0.%%0%dd", decimal_places);
strbuf_addstr(buf, "Accept-Language: ");
@ -1529,6 +1529,7 @@ int finish_http_pack_request(struct http_pack_request *preq)
struct packed_git **lst;
struct packed_git *p = preq->target;
char *tmp_idx;
size_t len;
struct child_process ip = CHILD_PROCESS_INIT;
const char *ip_argv[8];
@ -1542,9 +1543,9 @@ int finish_http_pack_request(struct http_pack_request *preq)
lst = &((*lst)->next);
*lst = (*lst)->next;
tmp_idx = xstrdup(preq->tmpfile);
strcpy(tmp_idx + strlen(tmp_idx) - strlen(".pack.temp"),
".idx.temp");
if (!strip_suffix(preq->tmpfile, ".pack.temp", &len))
die("BUG: pack tmpfile does not end in .pack.temp?");
tmp_idx = xstrfmt("%.*s.idx.temp", (int)len, preq->tmpfile);
ip_argv[0] = "index-pack";
ip_argv[1] = "-o";
@ -1619,7 +1620,7 @@ struct http_pack_request *new_http_pack_request(
fprintf(stderr,
"Resuming fetch of pack %s at byte %ld\n",
sha1_to_hex(target->sha1), prev_posn);
sprintf(range, "Range: bytes=%ld-", prev_posn);
xsnprintf(range, sizeof(range), "Range: bytes=%ld-", prev_posn);
preq->range_header = curl_slist_append(NULL, range);
curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
preq->range_header);
@ -1779,7 +1780,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
fprintf(stderr,
"Resuming fetch of object %s at byte %ld\n",
hex, prev_posn);
sprintf(range, "Range: bytes=%ld-", prev_posn);
xsnprintf(range, sizeof(range), "Range: bytes=%ld-", prev_posn);
range_header = curl_slist_append(range_header, range);
curl_easy_setopt(freq->slot->curl,
CURLOPT_HTTPHEADER, range_header);

Просмотреть файл

@ -889,9 +889,8 @@ static char *cram(const char *challenge_64, const char *user, const char *pass)
}
/* response: "<user> <digest in hex>" */
resp_len = strlen(user) + 1 + strlen(hex) + 1;
response = xmalloc(resp_len);
sprintf(response, "%s %s", user, hex);
response = xstrfmt("%s %s", user, hex);
resp_len = strlen(response) + 1;
response_64 = xmalloc(ENCODED_SIZE(resp_len) + 1);
encoded_len = EVP_EncodeBlock((unsigned char *)response_64,

Просмотреть файл

@ -145,11 +145,11 @@ static struct ll_merge_driver ll_merge_drv[] = {
{ "union", "built-in union merge", ll_union_merge },
};
static void create_temp(mmfile_t *src, char *path)
static void create_temp(mmfile_t *src, char *path, size_t len)
{
int fd;
strcpy(path, ".merge_file_XXXXXX");
xsnprintf(path, len, ".merge_file_XXXXXX");
fd = xmkstemp(path);
if (write_in_full(fd, src->ptr, src->size) != src->size)
die_errno("unable to write temp-file");
@ -190,10 +190,10 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
result->ptr = NULL;
result->size = 0;
create_temp(orig, temp[0]);
create_temp(src1, temp[1]);
create_temp(src2, temp[2]);
sprintf(temp[3], "%d", marker_size);
create_temp(orig, temp[0], sizeof(temp[0]));
create_temp(src1, temp[1], sizeof(temp[1]));
create_temp(src2, temp[2], sizeof(temp[2]));
xsnprintf(temp[3], sizeof(temp[3]), "%d", marker_size);
strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);

Просмотреть файл

@ -162,11 +162,10 @@ static void read_mailmap_line(struct string_list *map, char *buffer,
char *cp;
free(*repo_abbrev);
*repo_abbrev = xmalloc(len);
for (cp = buffer + abblen; isspace(*cp); cp++)
; /* nothing */
strcpy(*repo_abbrev, cp);
*repo_abbrev = xstrdup(cp);
}
return;
}

Просмотреть файл

@ -630,25 +630,24 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
static int dir_in_way(const char *path, int check_working_copy)
{
int pos, pathlen = strlen(path);
char *dirpath = xmalloc(pathlen + 2);
int pos;
struct strbuf dirpath = STRBUF_INIT;
struct stat st;
strcpy(dirpath, path);
dirpath[pathlen] = '/';
dirpath[pathlen+1] = '\0';
strbuf_addstr(&dirpath, path);
strbuf_addch(&dirpath, '/');
pos = cache_name_pos(dirpath, pathlen+1);
pos = cache_name_pos(dirpath.buf, dirpath.len);
if (pos < 0)
pos = -1 - pos;
if (pos < active_nr &&
!strncmp(dirpath, active_cache[pos]->name, pathlen+1)) {
free(dirpath);
!strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) {
strbuf_release(&dirpath);
return 1;
}
free(dirpath);
strbuf_release(&dirpath);
return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
}

Просмотреть файл

@ -539,6 +539,9 @@ static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
return fanout + 1;
}
/* hex SHA1 + 19 * '/' + NUL */
#define FANOUT_PATH_MAX 40 + 19 + 1
static void construct_path_with_fanout(const unsigned char *sha1,
unsigned char fanout, char *path)
{
@ -551,7 +554,7 @@ static void construct_path_with_fanout(const unsigned char *sha1,
path[i++] = '/';
fanout--;
}
strcpy(path + i, hex_sha1 + j);
xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_sha1 + j);
}
static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
@ -562,7 +565,7 @@ static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
void *p;
int ret = 0;
struct leaf_node *l;
static char path[40 + 19 + 1]; /* hex SHA1 + 19 * '/' + NUL */
static char path[FANOUT_PATH_MAX];
fanout = determine_fanout(tree, n, fanout);
for (i = 0; i < 16; i++) {
@ -595,7 +598,7 @@ redo:
/* invoke callback with subtree */
unsigned int path_len =
l->key_sha1[19] * 2 + fanout;
assert(path_len < 40 + 19);
assert(path_len < FANOUT_PATH_MAX - 1);
construct_path_with_fanout(l->key_sha1, fanout,
path);
/* Create trailing slash, if needed */

Просмотреть файл

@ -252,16 +252,11 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
static char *pack_bitmap_filename(struct packed_git *p)
{
char *idx_name;
int len;
size_t len;
len = strlen(p->pack_name) - strlen(".pack");
idx_name = xmalloc(len + strlen(".bitmap") + 1);
memcpy(idx_name, p->pack_name, len);
memcpy(idx_name + len, ".bitmap", strlen(".bitmap") + 1);
return idx_name;
if (!strip_suffix(p->pack_name, ".pack", &len))
die("BUG: pack_name does not end in .pack");
return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
}
static int open_pack_bitmap_1(struct packed_git *packfile)

80
path.c
Просмотреть файл

@ -395,6 +395,16 @@ static void do_git_path(struct strbuf *buf, const char *fmt, va_list args)
strbuf_cleanup_path(buf);
}
char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
{
va_list args;
strbuf_reset(buf);
va_start(args, fmt);
do_git_path(buf, fmt, args);
va_end(args);
return buf->buf;
}
void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
{
va_list args;
@ -452,8 +462,7 @@ static void do_submodule_path(struct strbuf *buf, const char *path,
struct strbuf git_submodule_dir = STRBUF_INIT;
strbuf_addstr(buf, path);
if (buf->len && buf->buf[buf->len - 1] != '/')
strbuf_addch(buf, '/');
strbuf_complete(buf, '/');
strbuf_addstr(buf, ".git");
git_dir = read_gitfile(buf->buf);
@ -611,8 +620,8 @@ return_null:
*/
const char *enter_repo(const char *path, int strict)
{
static char used_path[PATH_MAX];
static char validated_path[PATH_MAX];
static struct strbuf validated_path = STRBUF_INIT;
static struct strbuf used_path = STRBUF_INIT;
if (!path)
return NULL;
@ -627,46 +636,47 @@ const char *enter_repo(const char *path, int strict)
while ((1 < len) && (path[len-1] == '/'))
len--;
/*
* We can handle arbitrary-sized buffers, but this remains as a
* sanity check on untrusted input.
*/
if (PATH_MAX <= len)
return NULL;
strncpy(used_path, path, len); used_path[len] = 0 ;
strcpy(validated_path, used_path);
if (used_path[0] == '~') {
char *newpath = expand_user_path(used_path);
if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
free(newpath);
strbuf_reset(&used_path);
strbuf_reset(&validated_path);
strbuf_add(&used_path, path, len);
strbuf_add(&validated_path, path, len);
if (used_path.buf[0] == '~') {
char *newpath = expand_user_path(used_path.buf);
if (!newpath)
return NULL;
}
/*
* Copy back into the static buffer. A pity
* since newpath was not bounded, but other
* branches of the if are limited by PATH_MAX
* anyway.
*/
strcpy(used_path, newpath); free(newpath);
strbuf_attach(&used_path, newpath, strlen(newpath),
strlen(newpath));
}
else if (PATH_MAX - 10 < len)
return NULL;
len = strlen(used_path);
for (i = 0; suffix[i]; i++) {
struct stat st;
strcpy(used_path + len, suffix[i]);
if (!stat(used_path, &st) &&
size_t baselen = used_path.len;
strbuf_addstr(&used_path, suffix[i]);
if (!stat(used_path.buf, &st) &&
(S_ISREG(st.st_mode) ||
(S_ISDIR(st.st_mode) && is_git_directory(used_path)))) {
strcat(validated_path, suffix[i]);
(S_ISDIR(st.st_mode) && is_git_directory(used_path.buf)))) {
strbuf_addstr(&validated_path, suffix[i]);
break;
}
strbuf_setlen(&used_path, baselen);
}
if (!suffix[i])
return NULL;
gitfile = read_gitfile(used_path);
if (gitfile)
strcpy(used_path, gitfile);
if (chdir(used_path))
gitfile = read_gitfile(used_path.buf);
if (gitfile) {
strbuf_reset(&used_path);
strbuf_addstr(&used_path, gitfile);
}
if (chdir(used_path.buf))
return NULL;
path = validated_path;
path = validated_path.buf;
}
else {
const char *gitfile = read_gitfile(path);
@ -855,7 +865,7 @@ const char *relative_path(const char *in, const char *prefix,
*/
const char *remove_leading_path(const char *in, const char *prefix)
{
static char buf[PATH_MAX + 1];
static struct strbuf buf = STRBUF_INIT;
int i = 0, j = 0;
if (!prefix || !prefix[0])
@ -884,11 +894,13 @@ const char *remove_leading_path(const char *in, const char *prefix)
return in;
while (is_dir_sep(in[j]))
j++;
strbuf_reset(&buf);
if (!in[j])
strcpy(buf, ".");
strbuf_addstr(&buf, ".");
else
strcpy(buf, in + j);
return buf;
strbuf_addstr(&buf, in + j);
return buf.buf;
}
/*

Просмотреть файл

@ -25,7 +25,7 @@ struct throughput {
unsigned int last_bytes[TP_IDX_MAX];
unsigned int last_misecs[TP_IDX_MAX];
unsigned int idx;
char display[32];
struct strbuf display;
};
struct progress {
@ -98,7 +98,7 @@ static int display(struct progress *progress, unsigned n, const char *done)
}
progress->last_value = n;
tp = (progress->throughput) ? progress->throughput->display : "";
tp = (progress->throughput) ? progress->throughput->display.buf : "";
eol = done ? done : " \r";
if (progress->total) {
unsigned percent = n * 100 / progress->total;
@ -129,6 +129,7 @@ static int display(struct progress *progress, unsigned n, const char *done)
static void throughput_string(struct strbuf *buf, off_t total,
unsigned int rate)
{
strbuf_reset(buf);
strbuf_addstr(buf, ", ");
strbuf_humanise_bytes(buf, total);
strbuf_addstr(buf, " | ");
@ -141,7 +142,6 @@ void display_throughput(struct progress *progress, off_t total)
struct throughput *tp;
uint64_t now_ns;
unsigned int misecs, count, rate;
struct strbuf buf = STRBUF_INIT;
if (!progress)
return;
@ -154,6 +154,7 @@ void display_throughput(struct progress *progress, off_t total)
if (tp) {
tp->prev_total = tp->curr_total = total;
tp->prev_ns = now_ns;
strbuf_init(&tp->display, 0);
}
return;
}
@ -193,9 +194,7 @@ void display_throughput(struct progress *progress, off_t total)
tp->last_misecs[tp->idx] = misecs;
tp->idx = (tp->idx + 1) % TP_IDX_MAX;
throughput_string(&buf, total, rate);
strncpy(tp->display, buf.buf, sizeof(tp->display));
strbuf_release(&buf);
throughput_string(&tp->display, total, rate);
if (progress->last_value != -1 && progress_update)
display(progress, progress->last_value, NULL);
}
@ -250,20 +249,19 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
if (tp) {
struct strbuf strbuf = STRBUF_INIT;
unsigned int rate = !tp->avg_misecs ? 0 :
tp->avg_bytes / tp->avg_misecs;
throughput_string(&strbuf, tp->curr_total, rate);
strncpy(tp->display, strbuf.buf, sizeof(tp->display));
strbuf_release(&strbuf);
throughput_string(&tp->display, tp->curr_total, rate);
}
progress_update = 1;
sprintf(bufp, ", %s.\n", msg);
xsnprintf(bufp, len + 1, ", %s.\n", msg);
display(progress, progress->last_value, bufp);
if (buf != bufp)
free(bufp);
}
clear_progress_signal();
if (progress->throughput)
strbuf_release(&progress->throughput->display);
free(progress->throughput);
free(progress);
}

Просмотреть файл

@ -343,9 +343,7 @@ static int grab_objectname(const char *name, const unsigned char *sha1,
struct atom_value *v)
{
if (!strcmp(name, "objectname")) {
char *s = xmalloc(41);
strcpy(s, sha1_to_hex(sha1));
v->s = s;
v->s = xstrdup(sha1_to_hex(sha1));
return 1;
}
if (!strcmp(name, "objectname:short")) {
@ -370,10 +368,8 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
if (!strcmp(name, "objecttype"))
v->s = typename(obj->type);
else if (!strcmp(name, "objectsize")) {
char *s = xmalloc(40);
sprintf(s, "%lu", sz);
v->ul = sz;
v->s = s;
v->s = xstrfmt("%lu", sz);
}
else if (deref)
grab_objectname(name, obj->sha1, v);
@ -397,11 +393,8 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob
v->s = tag->tag;
else if (!strcmp(name, "type") && tag->tagged)
v->s = typename(tag->tagged->type);
else if (!strcmp(name, "object") && tag->tagged) {
char *s = xmalloc(41);
strcpy(s, sha1_to_hex(tag->tagged->sha1));
v->s = s;
}
else if (!strcmp(name, "object") && tag->tagged)
v->s = xstrdup(sha1_to_hex(tag->tagged->sha1));
}
}
@ -419,32 +412,22 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
if (deref)
name++;
if (!strcmp(name, "tree")) {
char *s = xmalloc(41);
strcpy(s, sha1_to_hex(commit->tree->object.sha1));
v->s = s;
v->s = xstrdup(sha1_to_hex(commit->tree->object.sha1));
}
if (!strcmp(name, "numparent")) {
char *s = xmalloc(40);
else if (!strcmp(name, "numparent")) {
v->ul = commit_list_count(commit->parents);
sprintf(s, "%lu", v->ul);
v->s = s;
v->s = xstrfmt("%lu", v->ul);
}
else if (!strcmp(name, "parent")) {
int num = commit_list_count(commit->parents);
int i;
struct commit_list *parents;
char *s = xmalloc(41 * num + 1);
v->s = s;
for (i = 0, parents = commit->parents;
parents;
parents = parents->next, i = i + 41) {
struct strbuf s = STRBUF_INIT;
for (parents = commit->parents; parents; parents = parents->next) {
struct commit *parent = parents->item;
strcpy(s+i, sha1_to_hex(parent->object.sha1));
if (parents->next)
s[i+40] = ' ';
if (parents != commit->parents)
strbuf_addch(&s, ' ');
strbuf_addstr(&s, sha1_to_hex(parent->object.sha1));
}
if (!i)
*s = '\0';
v->s = strbuf_detach(&s, NULL);
}
}
}
@ -934,7 +917,6 @@ static void populate_value(struct ref_array_item *ref)
else if (!strcmp(formatp, "track") &&
(starts_with(name, "upstream") ||
starts_with(name, "push"))) {
char buf[40];
if (stat_tracking_info(branch, &num_ours,
&num_theirs, NULL))
@ -942,17 +924,13 @@ static void populate_value(struct ref_array_item *ref)
if (!num_ours && !num_theirs)
v->s = "";
else if (!num_ours) {
sprintf(buf, "[behind %d]", num_theirs);
v->s = xstrdup(buf);
} else if (!num_theirs) {
sprintf(buf, "[ahead %d]", num_ours);
v->s = xstrdup(buf);
} else {
sprintf(buf, "[ahead %d, behind %d]",
num_ours, num_theirs);
v->s = xstrdup(buf);
}
else if (!num_ours)
v->s = xstrfmt("[behind %d]", num_theirs);
else if (!num_theirs)
v->s = xstrfmt("[ahead %d]", num_ours);
else
v->s = xstrfmt("[ahead %d, behind %d]",
num_ours, num_theirs);
continue;
} else if (!strcmp(formatp, "trackshort") &&
(starts_with(name, "upstream") ||
@ -979,12 +957,8 @@ static void populate_value(struct ref_array_item *ref)
if (!deref)
v->s = refname;
else {
int len = strlen(refname);
char *s = xmalloc(len + 4);
sprintf(s, "%s^{}", refname);
v->s = s;
}
else
v->s = xstrfmt("%s^{}", refname);
}
for (i = 0; i < used_atom_cnt; i++) {

Просмотреть файл

@ -56,12 +56,11 @@ static struct complete_reflogs *read_complete_reflog(const char *ref)
}
}
if (reflogs->nr == 0) {
int len = strlen(ref);
char *refname = xmalloc(len + 12);
sprintf(refname, "refs/%s", ref);
char *refname = xstrfmt("refs/%s", ref);
for_each_reflog_ent(refname, read_one_reflog, reflogs);
if (reflogs->nr == 0) {
sprintf(refname, "refs/heads/%s", ref);
free(refname);
refname = xstrfmt("refs/heads/%s", ref);
for_each_reflog_ent(refname, read_one_reflog, reflogs);
}
free(refname);

76
refs.c
Просмотреть файл

@ -1602,16 +1602,15 @@ static int resolve_missing_loose_ref(const char *refname,
}
/* This function needs to return a meaningful errno on failure */
static const char *resolve_ref_unsafe_1(const char *refname,
int resolve_flags,
unsigned char *sha1,
int *flags,
struct strbuf *sb_path)
static const char *resolve_ref_1(const char *refname,
int resolve_flags,
unsigned char *sha1,
int *flags,
struct strbuf *sb_refname,
struct strbuf *sb_path,
struct strbuf *sb_contents)
{
int depth = MAXDEPTH;
ssize_t len;
char buffer[256];
static char refname_buffer[256];
int bad_name = 0;
if (flags)
@ -1677,19 +1676,18 @@ static const char *resolve_ref_unsafe_1(const char *refname,
/* Follow "normalized" - ie "refs/.." symlinks by hand */
if (S_ISLNK(st.st_mode)) {
len = readlink(path, buffer, sizeof(buffer)-1);
if (len < 0) {
strbuf_reset(sb_contents);
if (strbuf_readlink(sb_contents, path, 0) < 0) {
if (errno == ENOENT || errno == EINVAL)
/* inconsistent with lstat; retry */
goto stat_ref;
else
return NULL;
}
buffer[len] = 0;
if (starts_with(buffer, "refs/") &&
!check_refname_format(buffer, 0)) {
strcpy(refname_buffer, buffer);
refname = refname_buffer;
if (starts_with(sb_contents->buf, "refs/") &&
!check_refname_format(sb_contents->buf, 0)) {
strbuf_swap(sb_refname, sb_contents);
refname = sb_refname->buf;
if (flags)
*flags |= REF_ISSYMREF;
if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
@ -1718,28 +1716,26 @@ static const char *resolve_ref_unsafe_1(const char *refname,
else
return NULL;
}
len = read_in_full(fd, buffer, sizeof(buffer)-1);
if (len < 0) {
strbuf_reset(sb_contents);
if (strbuf_read(sb_contents, fd, 256) < 0) {
int save_errno = errno;
close(fd);
errno = save_errno;
return NULL;
}
close(fd);
while (len && isspace(buffer[len-1]))
len--;
buffer[len] = '\0';
strbuf_rtrim(sb_contents);
/*
* Is it a symbolic ref?
*/
if (!starts_with(buffer, "ref:")) {
if (!starts_with(sb_contents->buf, "ref:")) {
/*
* Please note that FETCH_HEAD has a second
* line containing other data.
*/
if (get_sha1_hex(buffer, sha1) ||
(buffer[40] != '\0' && !isspace(buffer[40]))) {
if (get_sha1_hex(sb_contents->buf, sha1) ||
(sb_contents->buf[40] != '\0' && !isspace(sb_contents->buf[40]))) {
if (flags)
*flags |= REF_ISBROKEN;
errno = EINVAL;
@ -1754,10 +1750,12 @@ static const char *resolve_ref_unsafe_1(const char *refname,
}
if (flags)
*flags |= REF_ISSYMREF;
buf = buffer + 4;
buf = sb_contents->buf + 4;
while (isspace(*buf))
buf++;
refname = strcpy(refname_buffer, buf);
strbuf_reset(sb_refname);
strbuf_addstr(sb_refname, buf);
refname = sb_refname->buf;
if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
hashclr(sha1);
return refname;
@ -1779,10 +1777,15 @@ static const char *resolve_ref_unsafe_1(const char *refname,
const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
unsigned char *sha1, int *flags)
{
static struct strbuf sb_refname = STRBUF_INIT;
struct strbuf sb_contents = STRBUF_INIT;
struct strbuf sb_path = STRBUF_INIT;
const char *ret = resolve_ref_unsafe_1(refname, resolve_flags,
sha1, flags, &sb_path);
const char *ret;
ret = resolve_ref_1(refname, resolve_flags, sha1, flags,
&sb_refname, &sb_path, &sb_contents);
strbuf_release(&sb_path);
strbuf_release(&sb_contents);
return ret;
}
@ -2222,8 +2225,7 @@ int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
if (!has_glob_specials(pattern)) {
/* Append implied '/' '*' if not present. */
if (real_pattern.buf[real_pattern.len - 1] != '/')
strbuf_addch(&real_pattern, '/');
strbuf_complete(&real_pattern, '/');
/* No need to check for '*', there is none. */
strbuf_addch(&real_pattern, '*');
}
@ -2730,7 +2732,7 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
int namelen = strlen(entry->name) + 1;
struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
hashcpy(n->sha1, entry->u.value.oid.hash);
strcpy(n->name, entry->name);
memcpy(n->name, entry->name, namelen); /* includes NUL */
n->next = cb->ref_to_prune;
cb->ref_to_prune = n;
}
@ -3365,10 +3367,10 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
msglen = msg ? strlen(msg) : 0;
maxlen = strlen(committer) + msglen + 100;
logrec = xmalloc(maxlen);
len = sprintf(logrec, "%s %s %s\n",
sha1_to_hex(old_sha1),
sha1_to_hex(new_sha1),
committer);
len = xsnprintf(logrec, maxlen, "%s %s %s\n",
sha1_to_hex(old_sha1),
sha1_to_hex(new_sha1),
committer);
if (msglen)
len += copy_msg(logrec + len - 1, msg) - 1;
@ -4020,10 +4022,10 @@ void ref_transaction_free(struct ref_transaction *transaction)
static struct ref_update *add_update(struct ref_transaction *transaction,
const char *refname)
{
size_t len = strlen(refname);
struct ref_update *update = xcalloc(1, sizeof(*update) + len + 1);
size_t len = strlen(refname) + 1;
struct ref_update *update = xcalloc(1, sizeof(*update) + len);
strcpy((char *)update->refname, refname);
memcpy((char *)update->refname, refname, len); /* includes NUL */
ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
transaction->updates[transaction->nr++] = update;
return update;

Просмотреть файл

@ -168,10 +168,7 @@ static struct ref *parse_info_refs(struct discovery *heads)
url.buf);
data[i] = 0;
ref_name = mid + 1;
ref = xmalloc(sizeof(struct ref) +
strlen(ref_name) + 1);
memset(ref, 0, sizeof(struct ref));
strcpy(ref->name, ref_name);
ref = alloc_ref(ref_name);
get_sha1_hex(start, ref->old_sha1);
if (!refs)
refs = ref;

142
remote.c
Просмотреть файл

@ -8,6 +8,7 @@
#include "tag.h"
#include "string-list.h"
#include "mergesort.h"
#include "argv-array.h"
enum map_direction { FROM_SRC, FROM_DST };
@ -54,9 +55,6 @@ static const char *pushremote_name;
static struct rewrites rewrites;
static struct rewrites rewrites_push;
#define BUF_SIZE (2048)
static char buffer[BUF_SIZE];
static int valid_remote(const struct remote *remote)
{
return (!!remote->url) || (!!remote->foreign_vcs);
@ -65,7 +63,6 @@ static int valid_remote(const struct remote *remote)
static const char *alias_url(const char *url, struct rewrites *r)
{
int i, j;
char *ret;
struct counted_string *longest;
int longest_i;
@ -86,11 +83,7 @@ static const char *alias_url(const char *url, struct rewrites *r)
if (!longest)
return url;
ret = xmalloc(r->rewrite[longest_i]->baselen +
(strlen(url) - longest->len) + 1);
strcpy(ret, r->rewrite[longest_i]->base);
strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
return ret;
return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
}
static void add_push_refspec(struct remote *remote, const char *ref)
@ -248,106 +241,76 @@ static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
rewrite->instead_of_nr++;
}
static const char *skip_spaces(const char *s)
{
while (isspace(*s))
s++;
return s;
}
static void read_remotes_file(struct remote *remote)
{
struct strbuf buf = STRBUF_INIT;
FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
if (!f)
return;
remote->origin = REMOTE_REMOTES;
while (fgets(buffer, BUF_SIZE, f)) {
int value_list;
char *s, *p;
while (strbuf_getline(&buf, f, '\n') != EOF) {
const char *v;
if (starts_with(buffer, "URL:")) {
value_list = 0;
s = buffer + 4;
} else if (starts_with(buffer, "Push:")) {
value_list = 1;
s = buffer + 5;
} else if (starts_with(buffer, "Pull:")) {
value_list = 2;
s = buffer + 5;
} else
continue;
strbuf_rtrim(&buf);
while (isspace(*s))
s++;
if (!*s)
continue;
p = s + strlen(s);
while (isspace(p[-1]))
*--p = 0;
switch (value_list) {
case 0:
add_url_alias(remote, xstrdup(s));
break;
case 1:
add_push_refspec(remote, xstrdup(s));
break;
case 2:
add_fetch_refspec(remote, xstrdup(s));
break;
}
if (skip_prefix(buf.buf, "URL:", &v))
add_url_alias(remote, xstrdup(skip_spaces(v)));
else if (skip_prefix(buf.buf, "Push:", &v))
add_push_refspec(remote, xstrdup(skip_spaces(v)));
else if (skip_prefix(buf.buf, "Pull:", &v))
add_fetch_refspec(remote, xstrdup(skip_spaces(v)));
}
strbuf_release(&buf);
fclose(f);
}
static void read_branches_file(struct remote *remote)
{
char *frag;
struct strbuf branch = STRBUF_INIT;
int n = 1000;
FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
char *s, *p;
int len;
struct strbuf buf = STRBUF_INIT;
FILE *f = fopen(git_path("branches/%s", remote->name), "r");
if (!f)
return;
s = fgets(buffer, BUF_SIZE, f);
fclose(f);
if (!s)
return;
while (isspace(*s))
s++;
if (!*s)
strbuf_getline(&buf, f, '\n');
strbuf_trim(&buf);
if (!buf.len) {
strbuf_release(&buf);
return;
}
remote->origin = REMOTE_BRANCHES;
p = s + strlen(s);
while (isspace(p[-1]))
*--p = 0;
len = p - s;
p = xmalloc(len + 1);
strcpy(p, s);
/*
* The branches file would have URL and optionally
* #branch specified. The "master" (or specified) branch is
* fetched and stored in the local branch of the same name.
* fetched and stored in the local branch matching the
* remote name.
*/
frag = strchr(p, '#');
if (frag) {
frag = strchr(buf.buf, '#');
if (frag)
*(frag++) = '\0';
strbuf_addf(&branch, "refs/heads/%s", frag);
} else
strbuf_addstr(&branch, "refs/heads/master");
else
frag = "master";
add_url_alias(remote, strbuf_detach(&buf, NULL));
add_fetch_refspec(remote, xstrfmt("refs/heads/%s:refs/heads/%s",
frag, remote->name));
strbuf_addf(&branch, ":refs/heads/%s", remote->name);
add_url_alias(remote, p);
add_fetch_refspec(remote, strbuf_detach(&branch, NULL));
/*
* Cogito compatible push: push current HEAD to remote #branch
* (master if missing)
*/
strbuf_init(&branch, 0);
strbuf_addstr(&branch, "HEAD");
if (frag)
strbuf_addf(&branch, ":refs/heads/%s", frag);
else
strbuf_addstr(&branch, ":refs/heads/master");
add_push_refspec(remote, strbuf_detach(&branch, NULL));
add_push_refspec(remote, xstrfmt("HEAD:refs/heads/%s", frag));
remote->fetch_tags = 1; /* always auto-follow */
}
@ -2035,10 +1998,9 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
{
unsigned char sha1[20];
struct commit *ours, *theirs;
char symmetric[84];
struct rev_info revs;
const char *rev_argv[10], *base;
int rev_argc;
const char *base;
struct argv_array argv = ARGV_ARRAY_INIT;
/* Cannot stat unless we are marked to build on top of somebody else. */
base = branch_get_upstream(branch, NULL);
@ -2067,19 +2029,15 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
}
/* Run "rev-list --left-right ours...theirs" internally... */
rev_argc = 0;
rev_argv[rev_argc++] = NULL;
rev_argv[rev_argc++] = "--left-right";
rev_argv[rev_argc++] = symmetric;
rev_argv[rev_argc++] = "--";
rev_argv[rev_argc] = NULL;
strcpy(symmetric, sha1_to_hex(ours->object.sha1));
strcpy(symmetric + 40, "...");
strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
argv_array_push(&argv, ""); /* ignored */
argv_array_push(&argv, "--left-right");
argv_array_pushf(&argv, "%s...%s",
sha1_to_hex(ours->object.sha1),
sha1_to_hex(theirs->object.sha1));
argv_array_push(&argv, "--");
init_revisions(&revs, NULL);
setup_revisions(rev_argc, rev_argv, &revs, NULL);
setup_revisions(argv.argc, argv.argv, &revs, NULL);
if (prepare_revision_walk(&revs))
die("revision walk setup failed");
@ -2099,6 +2057,8 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
/* clear object flags smudged by the above traversal */
clear_commit_marks(ours, ALL_REV_FLAGS);
clear_commit_marks(theirs, ALL_REV_FLAGS);
argv_array_clear(&argv);
return 0;
}

Просмотреть файл

@ -38,7 +38,7 @@ char *path_name(const struct name_path *path, const char *name)
}
n = xmalloc(len);
m = n + len - (nlen + 1);
strcpy(m, name);
memcpy(m, name, nlen + 1);
for (p = path; p; p = p->up) {
if (p->elem_len) {
m -= p->elem_len + 1;

12
setup.c
Просмотреть файл

@ -99,10 +99,7 @@ char *prefix_path_gently(const char *prefix, int len,
return NULL;
}
} else {
sanitized = xmalloc(len + strlen(path) + 1);
if (len)
memcpy(sanitized, prefix, len);
strcpy(sanitized + len, path);
sanitized = xstrfmt("%.*s%s", len, prefix, path);
if (remaining_prefix)
*remaining_prefix = len;
if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) {
@ -475,11 +472,8 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
size_t pathlen = slash+1 - path;
size_t dirlen = pathlen + len - 8;
dir = xmalloc(dirlen + 1);
strncpy(dir, path, pathlen);
strncpy(dir + pathlen, buf + 8, len - 8);
dir[dirlen] = '\0';
dir = xstrfmt("%.*s%.*s", (int)pathlen, path,
(int)(len - 8), buf + 8);
free(buf);
buf = dir;
}

Просмотреть файл

@ -208,44 +208,25 @@ const char *sha1_file_name(const unsigned char *sha1)
* provided by the caller. which should be "pack" or "idx".
*/
static char *sha1_get_pack_name(const unsigned char *sha1,
char **name, char **base, const char *which)
struct strbuf *buf,
const char *which)
{
static const char hex[] = "0123456789abcdef";
char *buf;
int i;
if (!*base) {
const char *sha1_file_directory = get_object_directory();
int len = strlen(sha1_file_directory);
*base = xmalloc(len + 60);
sprintf(*base, "%s/pack/pack-1234567890123456789012345678901234567890.%s",
sha1_file_directory, which);
*name = *base + len + 11;
}
buf = *name;
for (i = 0; i < 20; i++) {
unsigned int val = *sha1++;
*buf++ = hex[val >> 4];
*buf++ = hex[val & 0xf];
}
return *base;
strbuf_reset(buf);
strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
sha1_to_hex(sha1), which);
return buf->buf;
}
char *sha1_pack_name(const unsigned char *sha1)
{
static char *name, *base;
return sha1_get_pack_name(sha1, &name, &base, "pack");
static struct strbuf buf = STRBUF_INIT;
return sha1_get_pack_name(sha1, &buf, "pack");
}
char *sha1_pack_index_name(const unsigned char *sha1)
{
static char *name, *base;
return sha1_get_pack_name(sha1, &name, &base, "idx");
static struct strbuf buf = STRBUF_INIT;
return sha1_get_pack_name(sha1, &buf, "idx");
}
struct alternate_object_database *alt_odb_list;
@ -671,13 +652,15 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
int open_pack_index(struct packed_git *p)
{
char *idx_name;
size_t len;
int ret;
if (p->index_data)
return 0;
idx_name = xstrdup(p->pack_name);
strcpy(idx_name + strlen(idx_name) - strlen(".pack"), ".idx");
if (!strip_suffix(p->pack_name, ".pack", &len))
die("BUG: pack_name does not end in .pack");
idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
ret = check_packed_git_idx(idx_name, p);
free(idx_name);
return ret;
@ -1161,11 +1144,12 @@ static void try_to_free_pack_memory(size_t size)
release_pack_memory(size);
}
struct packed_git *add_packed_git(const char *path, int path_len, int local)
struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
{
static int have_set_try_to_free_routine;
struct stat st;
struct packed_git *p = alloc_packed_git(path_len + 2);
size_t alloc;
struct packed_git *p;
if (!have_set_try_to_free_routine) {
have_set_try_to_free_routine = 1;
@ -1176,18 +1160,22 @@ struct packed_git *add_packed_git(const char *path, int path_len, int local)
* Make sure a corresponding .pack file exists and that
* the index looks sane.
*/
path_len -= strlen(".idx");
if (path_len < 1) {
free(p);
if (!strip_suffix_mem(path, &path_len, ".idx"))
return NULL;
}
/*
* ".pack" is long enough to hold any suffix we're adding (and
* the use xsnprintf double-checks that)
*/
alloc = path_len + strlen(".pack") + 1;
p = alloc_packed_git(alloc);
memcpy(p->pack_name, path, path_len);
strcpy(p->pack_name + path_len, ".keep");
xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
if (!access(p->pack_name, F_OK))
p->pack_keep = 1;
strcpy(p->pack_name + path_len, ".pack");
xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
free(p);
return NULL;
@ -1207,9 +1195,10 @@ struct packed_git *add_packed_git(const char *path, int path_len, int local)
struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
{
const char *path = sha1_pack_name(sha1);
struct packed_git *p = alloc_packed_git(strlen(path) + 1);
int alloc = strlen(path) + 1;
struct packed_git *p = alloc_packed_git(alloc);
strcpy(p->pack_name, path);
memcpy(p->pack_name, path, alloc); /* includes NUL */
hashcpy(p->sha1, sha1);
if (check_packed_git_idx(idx_path, p)) {
free(p);
@ -1479,7 +1468,7 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
return -1;
/* Generate the header */
hdrlen = sprintf(hdr, "%s %lu", typename(obj_type), size) + 1;
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(obj_type), size) + 1;
/* Sha1.. */
git_SHA1_Init(&c);
@ -2945,7 +2934,7 @@ static void write_sha1_file_prepare(const void *buf, unsigned long len,
git_SHA_CTX c;
/* Generate the header */
*hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
*hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
/* Sha1.. */
git_SHA1_Init(&c);
@ -3008,7 +2997,7 @@ int hash_sha1_file(const void *buf, unsigned long len, const char *type,
unsigned char *sha1)
{
char hdr[32];
int hdrlen;
int hdrlen = sizeof(hdr);
write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
return 0;
}
@ -3038,29 +3027,31 @@ static inline int directory_size(const char *filename)
* We want to avoid cross-directory filename renames, because those
* can have problems on various filesystems (FAT, NFS, Coda).
*/
static int create_tmpfile(char *buffer, size_t bufsiz, const char *filename)
static int create_tmpfile(struct strbuf *tmp, const char *filename)
{
int fd, dirlen = directory_size(filename);
if (dirlen + 20 > bufsiz) {
errno = ENAMETOOLONG;
return -1;
}
memcpy(buffer, filename, dirlen);
strcpy(buffer + dirlen, "tmp_obj_XXXXXX");
fd = git_mkstemp_mode(buffer, 0444);
strbuf_reset(tmp);
strbuf_add(tmp, filename, dirlen);
strbuf_addstr(tmp, "tmp_obj_XXXXXX");
fd = git_mkstemp_mode(tmp->buf, 0444);
if (fd < 0 && dirlen && errno == ENOENT) {
/* Make sure the directory exists */
memcpy(buffer, filename, dirlen);
buffer[dirlen-1] = 0;
if (mkdir(buffer, 0777) && errno != EEXIST)
/*
* Make sure the directory exists; note that the contents
* of the buffer are undefined after mkstemp returns an
* error, so we have to rewrite the whole buffer from
* scratch.
*/
strbuf_reset(tmp);
strbuf_add(tmp, filename, dirlen - 1);
if (mkdir(tmp->buf, 0777) && errno != EEXIST)
return -1;
if (adjust_shared_perm(buffer))
if (adjust_shared_perm(tmp->buf))
return -1;
/* Try again */
strcpy(buffer + dirlen - 1, "/tmp_obj_XXXXXX");
fd = git_mkstemp_mode(buffer, 0444);
strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
fd = git_mkstemp_mode(tmp->buf, 0444);
}
return fd;
}
@ -3073,10 +3064,10 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
git_zstream stream;
git_SHA_CTX c;
unsigned char parano_sha1[20];
static char tmp_file[PATH_MAX];
static struct strbuf tmp_file = STRBUF_INIT;
const char *filename = sha1_file_name(sha1);
fd = create_tmpfile(tmp_file, sizeof(tmp_file), filename);
fd = create_tmpfile(&tmp_file, filename);
if (fd < 0) {
if (errno == EACCES)
return error("insufficient permission for adding an object to repository database %s", get_object_directory());
@ -3125,12 +3116,12 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
struct utimbuf utb;
utb.actime = mtime;
utb.modtime = mtime;
if (utime(tmp_file, &utb) < 0)
if (utime(tmp_file.buf, &utb) < 0)
warning("failed utime() on %s: %s",
tmp_file, strerror(errno));
tmp_file.buf, strerror(errno));
}
return finalize_object_file(tmp_file, filename);
return finalize_object_file(tmp_file.buf, filename);
}
static int freshen_loose_object(const unsigned char *sha1)
@ -3154,7 +3145,7 @@ static int freshen_packed_object(const unsigned char *sha1)
int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1)
{
char hdr[32];
int hdrlen;
int hdrlen = sizeof(hdr);
/* Normally if we have it in the pack then we do not bother writing
* it out into .git/objects/??/?{38} file.
@ -3172,7 +3163,8 @@ int hash_sha1_file_literally(const void *buf, unsigned long len, const char *typ
int hdrlen, status = 0;
/* type string, SP, %lu of the length plus NUL must fit this */
header = xmalloc(strlen(type) + 32);
hdrlen = strlen(type) + 32;
header = xmalloc(hdrlen);
write_sha1_file_prepare(buf, len, type, sha1, header, &hdrlen);
if (!(flags & HASH_WRITE_OBJECT))
@ -3200,7 +3192,7 @@ int force_object_loose(const unsigned char *sha1, time_t mtime)
buf = read_packed_sha1(sha1, &type, &len);
if (!buf)
return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
ret = write_loose_object(sha1, hdr, hdrlen, buf, len, mtime);
free(buf);

Просмотреть файл

@ -96,11 +96,15 @@ static void find_short_object_filename(int len, const char *hex_pfx, struct disa
}
fakeent->next = alt_odb_list;
sprintf(hex, "%.2s", hex_pfx);
xsnprintf(hex, sizeof(hex), "%.2s", hex_pfx);
for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) {
struct dirent *de;
DIR *dir;
sprintf(alt->name, "%.2s/", hex_pfx);
/*
* every alt_odb struct has 42 extra bytes after the base
* for exactly this purpose
*/
xsnprintf(alt->name, 42, "%.2s/", hex_pfx);
dir = opendir(alt->base);
if (!dir)
continue;
@ -368,14 +372,13 @@ int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data)
return ds.ambiguous;
}
const char *find_unique_abbrev(const unsigned char *sha1, int len)
int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
{
int status, exists;
static char hex[41];
memcpy(hex, sha1_to_hex(sha1), 40);
sha1_to_hex_r(hex, sha1);
if (len == 40 || !len)
return hex;
return 40;
exists = has_sha1_file(sha1);
while (len < 40) {
unsigned char sha1_ret[20];
@ -384,10 +387,17 @@ const char *find_unique_abbrev(const unsigned char *sha1, int len)
? !status
: status == SHORT_NAME_NOT_FOUND) {
hex[len] = 0;
return hex;
return len;
}
len++;
}
return len;
}
const char *find_unique_abbrev(const unsigned char *sha1, int len)
{
static char hex[GIT_SHA1_HEXSZ + 1];
find_unique_abbrev_r(hex, sha1, len);
return hex;
}
@ -1283,8 +1293,7 @@ static void diagnose_invalid_index_path(int stage,
const struct cache_entry *ce;
int pos;
unsigned namelen = strlen(filename);
unsigned fullnamelen;
char *fullname;
struct strbuf fullname = STRBUF_INIT;
if (!prefix)
prefix = "";
@ -1304,21 +1313,19 @@ static void diagnose_invalid_index_path(int stage,
}
/* Confusion between relative and absolute filenames? */
fullnamelen = namelen + strlen(prefix);
fullname = xmalloc(fullnamelen + 1);
strcpy(fullname, prefix);
strcat(fullname, filename);
pos = cache_name_pos(fullname, fullnamelen);
strbuf_addstr(&fullname, prefix);
strbuf_addstr(&fullname, filename);
pos = cache_name_pos(fullname.buf, fullname.len);
if (pos < 0)
pos = -pos - 1;
if (pos < active_nr) {
ce = active_cache[pos];
if (ce_namelen(ce) == fullnamelen &&
!memcmp(ce->name, fullname, fullnamelen))
if (ce_namelen(ce) == fullname.len &&
!memcmp(ce->name, fullname.buf, fullname.len))
die("Path '%s' is in the index, but not '%s'.\n"
"Did you mean ':%d:%s' aka ':%d:./%s'?",
fullname, filename,
ce_stage(ce), fullname,
fullname.buf, filename,
ce_stage(ce), fullname.buf,
ce_stage(ce), filename);
}
@ -1328,7 +1335,7 @@ static void diagnose_invalid_index_path(int stage,
die("Path '%s' does not exist (neither on disk nor in the index).",
filename);
free(fullname);
strbuf_release(&fullname);
}

Просмотреть файл

@ -137,11 +137,11 @@ ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet
if (packet_max - 5 < n)
n = packet_max - 5;
if (0 <= band) {
sprintf(hdr, "%04x", n + 5);
xsnprintf(hdr, sizeof(hdr), "%04x", n + 5);
hdr[4] = band;
write_or_die(fd, hdr, 5);
} else {
sprintf(hdr, "%04x", n + 4);
xsnprintf(hdr, sizeof(hdr), "%04x", n + 4);
write_or_die(fd, hdr, 4);
}
write_or_die(fd, p, n);

Просмотреть файл

@ -245,8 +245,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
static char prefix2[2];
if (prefix1[0] != comment_line_char) {
sprintf(prefix1, "%c ", comment_line_char);
sprintf(prefix2, "%c", comment_line_char);
xsnprintf(prefix1, sizeof(prefix1), "%c ", comment_line_char);
xsnprintf(prefix2, sizeof(prefix2), "%c", comment_line_char);
}
add_lines(out, prefix1, prefix2, buf, size);
}
@ -743,3 +743,12 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
}
strbuf_setlen(sb, sb->len + len);
}
void strbuf_add_unique_abbrev(struct strbuf *sb, const unsigned char *sha1,
int abbrev_len)
{
int r;
strbuf_grow(sb, GIT_SHA1_HEXSZ + 1);
r = find_unique_abbrev_r(sb->buf + sb->len, sha1, abbrev_len);
strbuf_setlen(sb, sb->len + r);
}

Просмотреть файл

@ -474,6 +474,14 @@ static inline struct strbuf **strbuf_split(const struct strbuf *sb,
*/
extern void strbuf_list_free(struct strbuf **);
/**
* Add the abbreviation, as generated by find_unique_abbrev, of `sha1` to
* the strbuf `sb`.
*/
extern void strbuf_add_unique_abbrev(struct strbuf *sb,
const unsigned char *sha1,
int abbrev_len);
/**
* Launch the user preferred editor to edit a file and fill the buffer
* with the file's contents upon the user completing their editing. The
@ -491,10 +499,21 @@ extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *
*/
extern void strbuf_addstr_xml_quoted(struct strbuf *sb, const char *s);
/**
* "Complete" the contents of `sb` by ensuring that either it ends with the
* character `term`, or it is empty. This can be used, for example,
* to ensure that text ends with a newline, but without creating an empty
* blank line if there is no content in the first place.
*/
static inline void strbuf_complete(struct strbuf *sb, char term)
{
if (sb->len && sb->buf[sb->len - 1] != term)
strbuf_addch(sb, term);
}
static inline void strbuf_complete_line(struct strbuf *sb)
{
if (sb->len && sb->buf[sb->len - 1] != '\n')
strbuf_addch(sb, '\n');
strbuf_complete(sb, '\n');
}
extern int strbuf_branchname(struct strbuf *sb, const char *name);

Просмотреть файл

@ -122,6 +122,7 @@ static int add_submodule_odb(const char *path)
struct strbuf objects_directory = STRBUF_INIT;
struct alternate_object_database *alt_odb;
int ret = 0;
int alloc;
strbuf_git_path_submodule(&objects_directory, path, "objects/");
if (!is_directory(objects_directory.buf)) {
@ -135,9 +136,10 @@ static int add_submodule_odb(const char *path)
objects_directory.len))
goto done;
alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
alloc = objects_directory.len + 42; /* for "12/345..." sha1 */
alt_odb = xmalloc(sizeof(*alt_odb) + alloc);
alt_odb->next = alt_odb_list;
strcpy(alt_odb->base, objects_directory.buf);
xsnprintf(alt_odb->base, alloc, "%s", objects_directory.buf);
alt_odb->name = alt_odb->base + objects_directory.len;
alt_odb->name[2] = '/';
alt_odb->name[40] = '\0';

Просмотреть файл

@ -202,8 +202,8 @@ test_expect_success 'init honors global core.sharedRepository' '
x$(git config -f shared-honor-global/.git/config core.sharedRepository)
'
test_expect_success 'init rejects insanely long --template' '
test_must_fail git init --template=$(printf "x%09999dx" 1) test
test_expect_success 'init allows insanely long --template' '
git init --template=$(printf "x%09999dx" 1) test
'
test_expect_success 'init creates a new directory' '

Просмотреть файл

@ -63,4 +63,33 @@ test_expect_success 'symbolic-ref fails to delete real ref' '
'
reset_to_sane
test_expect_success 'create large ref name' '
# make 256+ character ref; some systems may not handle that,
# so be gentle
long=0123456789abcdef &&
long=$long/$long/$long/$long &&
long=$long/$long/$long/$long &&
long_ref=refs/heads/$long &&
tree=$(git write-tree) &&
commit=$(echo foo | git commit-tree $tree) &&
if git update-ref $long_ref $commit; then
test_set_prereq LONG_REF
else
echo >&2 "long refs not supported"
fi
'
test_expect_success LONG_REF 'symbolic-ref can point to large ref name' '
git symbolic-ref HEAD $long_ref &&
echo $long_ref >expect &&
git symbolic-ref HEAD >actual &&
test_cmp expect actual
'
test_expect_success LONG_REF 'we can parse long symbolic ref' '
echo $commit >expect &&
git rev-parse --verify HEAD >actual &&
test_cmp expect actual
'
test_done

Просмотреть файл

@ -69,4 +69,32 @@ test_expect_success 'update backfilled tag without primary transfer' '
test_cmp expect actual
'
test_expect_success 'set up fake git-daemon' '
mkdir remote &&
git init --bare remote/one.git &&
mkdir remote/host &&
git init --bare remote/host/two.git &&
write_script fake-daemon <<-\EOF &&
git daemon --inetd \
--informative-errors \
--export-all \
--base-path="$TRASH_DIRECTORY/remote" \
--interpolated-path="$TRASH_DIRECTORY/remote/%H%D" \
"$TRASH_DIRECTORY/remote"
EOF
export TRASH_DIRECTORY &&
PATH=$TRASH_DIRECTORY:$PATH
'
test_expect_success 'ext command can connect to git daemon (no vhost)' '
rm -rf dst &&
git clone "ext::fake-daemon %G/one.git" dst
'
test_expect_success 'ext command can connect to git daemon (vhost)' '
rm -rf dst &&
git clone "ext::fake-daemon %G/two.git %Vhost" dst
'
test_done

2
tag.c
Просмотреть файл

@ -82,7 +82,7 @@ int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
nl = memchr(bufptr, '\n', tail - bufptr);
if (!nl || sizeof(type) <= (nl - bufptr))
return -1;
strncpy(type, bufptr, nl - bufptr);
memcpy(type, bufptr, nl - bufptr);
type[nl - bufptr] = '\0';
bufptr = nl + 1;

Просмотреть файл

@ -47,7 +47,7 @@ static int dump_cache_tree(struct cache_tree *it,
struct cache_tree_sub *rdwn;
rdwn = cache_tree_sub(ref, down->name);
sprintf(path, "%s%.*s/", pfx, down->namelen, down->name);
xsnprintf(path, sizeof(path), "%s%.*s/", pfx, down->namelen, down->name);
if (dump_cache_tree(down->cache_tree, rdwn->cache_tree, path))
errs = 1;
}

23
trace.c
Просмотреть файл

@ -277,25 +277,24 @@ void trace_performance_fl(const char *file, int line, uint64_t nanos,
static const char *quote_crnl(const char *path)
{
static char new_path[PATH_MAX];
const char *p2 = path;
char *p1 = new_path;
static struct strbuf new_path = STRBUF_INIT;
if (!path)
return NULL;
while (*p2) {
switch (*p2) {
case '\\': *p1++ = '\\'; *p1++ = '\\'; break;
case '\n': *p1++ = '\\'; *p1++ = 'n'; break;
case '\r': *p1++ = '\\'; *p1++ = 'r'; break;
strbuf_reset(&new_path);
while (*path) {
switch (*path) {
case '\\': strbuf_addstr(&new_path, "\\\\"); break;
case '\n': strbuf_addstr(&new_path, "\\n"); break;
case '\r': strbuf_addstr(&new_path, "\\r"); break;
default:
*p1++ = *p2;
strbuf_addch(&new_path, *path);
}
p2++;
path++;
}
*p1 = '\0';
return new_path;
return new_path.buf;
}
/* FIXME: move prefix to startup_info struct and get rid of this arg */

Просмотреть файл

@ -654,23 +654,24 @@ static void print_ok_ref_status(struct ref *ref, int porcelain)
"[new branch]"),
ref, ref->peer_ref, NULL, porcelain);
else {
char quickref[84];
struct strbuf quickref = STRBUF_INIT;
char type;
const char *msg;
strcpy(quickref, status_abbrev(ref->old_sha1));
strbuf_addstr(&quickref, status_abbrev(ref->old_sha1));
if (ref->forced_update) {
strcat(quickref, "...");
strbuf_addstr(&quickref, "...");
type = '+';
msg = "forced update";
} else {
strcat(quickref, "..");
strbuf_addstr(&quickref, "..");
type = ' ';
msg = NULL;
}
strcat(quickref, status_abbrev(ref->new_sha1));
strbuf_addstr(&quickref, status_abbrev(ref->new_sha1));
print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
strbuf_release(&quickref);
}
}

Просмотреть файл

@ -1350,9 +1350,7 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
* Then we need to make sure that we do not lose a locally
* present file that is not ignored.
*/
pathbuf = xmalloc(namelen + 2);
memcpy(pathbuf, ce->name, namelen);
strcpy(pathbuf+namelen, "/");
pathbuf = xstrfmt("%.*s/", namelen, ce->name);
memset(&d, 0, sizeof(d));
if (o->dir)

3
url.c
Просмотреть файл

@ -120,8 +120,7 @@ char *url_decode_parameter_value(const char **query)
void end_url_with_slash(struct strbuf *buf, const char *url)
{
strbuf_addstr(buf, url);
if (buf->len && buf->buf[buf->len - 1] != '/')
strbuf_addch(buf, '/');
strbuf_complete(buf, '/');
}
void str_end_url_with_slash(const char *url, char **dest) {

Просмотреть файл

@ -17,10 +17,9 @@ void walker_say(struct walker *walker, const char *fmt, const char *hex)
static void report_missing(const struct object *obj)
{
char missing_hex[41];
strcpy(missing_hex, sha1_to_hex(obj->sha1));
fprintf(stderr, "Cannot obtain needed %s %s\n",
obj->type ? typename(obj->type): "object", missing_hex);
obj->type ? typename(obj->type): "object",
sha1_to_hex(obj->sha1));
if (!is_null_sha1(current_commit_sha1))
fprintf(stderr, "while processing commit %s.\n",
sha1_to_hex(current_commit_sha1));

Просмотреть файл

@ -621,6 +621,22 @@ char *xgetcwd(void)
return strbuf_detach(&sb, NULL);
}
int xsnprintf(char *dst, size_t max, const char *fmt, ...)
{
va_list ap;
int len;
va_start(ap, fmt);
len = vsnprintf(dst, max, fmt, ap);
va_end(ap);
if (len < 0)
die("BUG: your snprintf is broken");
if (len >= max)
die("BUG: attempt to snprintf into too-small buffer");
return len;
}
static int write_file_v(const char *path, int fatal,
const char *fmt, va_list params)
{