From: Junio C Hamano Date: Wed, 15 Aug 2018 22:08:25 +0000 (-0700) Subject: Merge branch 'jk/size-t' X-Git-Tag: v2.19.0-rc0~71 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/7d020f5a78a1ae7ff47d65a2adc79171c0cf9cb7?hp=-c Merge branch 'jk/size-t' Code clean-up to use size_t/ssize_t when they are the right type. * jk/size-t: strbuf_humanise: use unsigned variables pass st.st_size as hint for strbuf_readlink() strbuf_readlink: use ssize_t strbuf: use size_t for length in intermediate variables reencode_string: use size_t for string lengths reencode_string: use st_add/st_mult helpers --- 7d020f5a78a1ae7ff47d65a2adc79171c0cf9cb7 diff --combined convert.c index 3ee4bc274b,e012959b4c..ce7ea0db06 --- a/convert.c +++ b/convert.c @@@ -1,7 -1,6 +1,7 @@@ #define NO_THE_INDEX_COMPATIBILITY_MACROS #include "cache.h" #include "config.h" +#include "object-store.h" #include "attr.h" #include "run-command.h" #include "quote.h" @@@ -191,7 -190,7 +191,7 @@@ static enum eol output_eol(enum crlf_ac /* fall through */ return text_eol_is_crlf() ? EOL_CRLF : EOL_LF; } - warning("Illegal crlf_action %d\n", (int)crlf_action); + warning(_("illegal crlf_action %d"), (int)crlf_action); return core_eol; } @@@ -204,11 -203,11 +204,11 @@@ static void check_global_conv_flags_eol * CRLFs would not be restored by checkout */ if (conv_flags & CONV_EOL_RNDTRP_DIE) - die(_("CRLF would be replaced by LF in %s."), path); + die(_("CRLF would be replaced by LF in %s"), path); else if (conv_flags & CONV_EOL_RNDTRP_WARN) warning(_("CRLF will be replaced by LF in %s.\n" "The file will have its original line" - " endings in your working directory."), path); + " endings in your working directory"), path); } else if (old_stats->lonelf && !new_stats->lonelf ) { /* * CRLFs would be added by checkout @@@ -218,7 -217,7 +218,7 @@@ else if (conv_flags & CONV_EOL_RNDTRP_WARN) warning(_("LF will be replaced by CRLF in %s.\n" "The file will have its original line" - " endings in your working directory."), path); + " endings in your working directory"), path); } } @@@ -335,7 -334,7 +335,7 @@@ static void trace_encoding(const char * strbuf_addf(&trace, "%s (%s, considered %s):\n", context, path, encoding); for (i = 0; i < len && buf; ++i) { strbuf_addf( - &trace,"| \e[2m%2i:\e[0m %2x \e[2m%c\e[0m%c", + &trace, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c", i, (unsigned char) buf[i], (buf[i] > 32 && buf[i] < 127 ? buf[i] : ' '), @@@ -390,7 -389,7 +390,7 @@@ static int encode_to_git(const char *pa struct strbuf *buf, const char *enc, int conv_flags) { char *dst; - int dst_len; + size_t dst_len; int die_on_error = conv_flags & CONV_WRITE_OBJECT; /* @@@ -453,7 -452,7 +453,7 @@@ */ if (die_on_error && check_roundtrip(enc)) { char *re_src; - int re_src_len; + size_t re_src_len; re_src = reencode_string_len(dst, dst_len, enc, default_encoding, @@@ -481,7 -480,7 +481,7 @@@ static int encode_to_worktree(const cha struct strbuf *buf, const char *enc) { char *dst; - int dst_len; + size_t dst_len; /* * No encoding is specified or there is nothing to encode. @@@ -493,8 -492,8 +493,8 @@@ dst = reencode_string_len(src, src_len, enc, default_encoding, &dst_len); if (!dst) { - error("failed to encode '%s' from %s to %s", - path, default_encoding, enc); + error(_("failed to encode '%s' from %s to %s"), + path, default_encoding, enc); return 0; } @@@ -671,8 -670,7 +671,8 @@@ static int filter_buffer_or_fd(int in, if (start_command(&child_process)) { strbuf_release(&cmd); - return error("cannot fork to run external filter '%s'", params->cmd); + return error(_("cannot fork to run external filter '%s'"), + params->cmd); } sigchain_push(SIGPIPE, SIG_IGN); @@@ -691,14 -689,13 +691,14 @@@ if (close(child_process.in)) write_err = 1; if (write_err) - error("cannot feed the input to external filter '%s'", params->cmd); + error(_("cannot feed the input to external filter '%s'"), + params->cmd); sigchain_pop(SIGPIPE); status = finish_command(&child_process); if (status) - error("external filter '%s' failed %d", params->cmd, status); + error(_("external filter '%s' failed %d"), params->cmd, status); strbuf_release(&cmd); return (write_err || status); @@@ -733,13 -730,13 +733,13 @@@ static int apply_single_file_filter(con return 0; /* error was already reported */ if (strbuf_read(&nbuf, async.out, len) < 0) { - err = error("read from external filter '%s' failed", cmd); + err = error(_("read from external filter '%s' failed"), cmd); } if (close(async.out)) { - err = error("read from external filter '%s' failed", cmd); + err = error(_("read from external filter '%s' failed"), cmd); } if (finish_async(&async)) { - err = error("external filter '%s' failed", cmd); + err = error(_("external filter '%s' failed"), cmd); } if (!err) { @@@ -793,7 -790,7 +793,7 @@@ static void handle_filter_error(const s * Something went wrong with the protocol filter. * Force shutdown and restart if another blob requires filtering. */ - error("external filter '%s' failed", entry->subprocess.cmd); + error(_("external filter '%s' failed"), entry->subprocess.cmd); subprocess_stop(&subprocess_map, &entry->subprocess); free(entry); } @@@ -841,7 -838,7 +841,7 @@@ static int apply_multi_file_filter(cons else if (wanted_capability & CAP_SMUDGE) filter_type = "smudge"; else - die("unexpected filter type"); + die(_("unexpected filter type")); sigchain_push(SIGPIPE, SIG_IGN); @@@ -852,7 -849,7 +852,7 @@@ err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n"); if (err) { - error("path name too long for external filter"); + error(_("path name too long for external filter")); goto done; } @@@ -926,8 -923,8 +926,8 @@@ int async_query_available_blobs(const c assert(subprocess_map_initialized); entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd); if (!entry) { - error("external filter '%s' is not available anymore although " - "not all paths have been filtered", cmd); + error(_("external filter '%s' is not available anymore although " + "not all paths have been filtered"), cmd); return 0; } process = &entry->subprocess.process; @@@ -1398,7 -1395,7 +1398,7 @@@ int convert_to_git(const struct index_s ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN, NULL); if (!ret && ca.drv && ca.drv->required) - die("%s: clean filter '%s' failed", path, ca.drv->name); + die(_("%s: clean filter '%s' failed"), path, ca.drv->name); if (ret && dst) { src = dst->buf; @@@ -1432,7 -1429,7 +1432,7 @@@ void convert_to_git_filter_fd(const str assert(ca.drv->clean || ca.drv->process); if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL)) - die("%s: clean filter '%s' failed", path, ca.drv->name); + die(_("%s: clean filter '%s' failed"), path, ca.drv->name); encode_to_git(path, dst->buf, dst->len, dst, ca.working_tree_encoding, conv_flags); crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, conv_flags); @@@ -1475,7 -1472,7 +1475,7 @@@ static int convert_to_working_tree_inte ret_filter = apply_filter( path, src, len, -1, dst, ca.drv, CAP_SMUDGE, dco); if (!ret_filter && ca.drv && ca.drv->required) - die("%s: smudge filter %s failed", path, ca.drv->name); + die(_("%s: smudge filter %s failed"), path, ca.drv->name); return ret | ret_filter; } diff --combined pretty.c index 2b12da324b,e1e4060243..98cf5228f9 --- a/pretty.c +++ b/pretty.c @@@ -630,7 -630,7 +630,7 @@@ const char *logmsg_reencode(const struc * the cached copy from get_commit_buffer, we need to duplicate it * to avoid munging the cached copy. */ - if (msg == get_cached_commit_buffer(commit, NULL)) + if (msg == get_cached_commit_buffer(the_repository, commit, NULL)) out = xstrdup(msg); else out = (char *)msg; @@@ -1146,7 -1146,7 +1146,7 @@@ static size_t format_commit_one(struct /* these depend on the commit */ if (!commit->object.parsed) - parse_object(&commit->object.oid); + parse_object(the_repository, &commit->object.oid); switch (placeholder[0]) { case 'H': /* commit hash */ @@@ -1538,7 -1538,7 +1538,7 @@@ void format_commit_message(const struc } if (output_enc) { - int outsz; + size_t outsz; char *out = reencode_string_len(sb->buf, sb->len, output_enc, utf8, &outsz); if (out) @@@ -1575,7 -1575,7 +1575,7 @@@ static void pp_header(struct pretty_pri } if (starts_with(line, "parent ")) { - if (linelen != 48) + if (linelen != the_hash_algo->hexsz + 8) die("bad parent line in commit"); continue; } @@@ -1583,7 -1583,7 +1583,7 @@@ if (!parents_shown) { unsigned num = commit_list_count(commit->parents); /* with enough slop */ - strbuf_grow(sb, num * 50 + 20); + strbuf_grow(sb, num * (GIT_MAX_HEXSZ + 10) + 20); add_merge_info(pp, sb, commit); parents_shown = 1; } diff --combined refs/files-backend.c index b9eb3aabe6,c110c2520c..1f1a98e4cb --- a/refs/files-backend.c +++ b/refs/files-backend.c @@@ -363,7 -363,7 +363,7 @@@ stat_ref /* Follow "normalized" - ie "refs/.." symlinks by hand */ if (S_ISLNK(st.st_mode)) { strbuf_reset(&sb_contents); - if (strbuf_readlink(&sb_contents, path, 0) < 0) { + if (strbuf_readlink(&sb_contents, path, st.st_size) < 0) { if (errno == ENOENT || errno == EINVAL) /* inconsistent with lstat; retry */ goto stat_ref; @@@ -1582,17 -1582,26 +1582,17 @@@ static int log_ref_write_fd(int fd, con const struct object_id *new_oid, const char *committer, const char *msg) { - int msglen, written; - unsigned maxlen, len; - char *logrec; - - msglen = msg ? strlen(msg) : 0; - maxlen = strlen(committer) + msglen + 100; - logrec = xmalloc(maxlen); - len = xsnprintf(logrec, maxlen, "%s %s %s\n", - oid_to_hex(old_oid), - oid_to_hex(new_oid), - committer); - if (msglen) - len += copy_reflog_msg(logrec + len - 1, msg) - 1; - - written = len <= maxlen ? write_in_full(fd, logrec, len) : -1; - free(logrec); - if (written < 0) - return -1; + struct strbuf sb = STRBUF_INIT; + int ret = 0; - return 0; + strbuf_addf(&sb, "%s %s %s", oid_to_hex(old_oid), oid_to_hex(new_oid), committer); + if (msg && *msg) + copy_reflog_msg(&sb, msg); + strbuf_addch(&sb, '\n'); + if (write_in_full(fd, sb.buf, sb.len) < 0) + ret = -1; + strbuf_release(&sb); + return ret; } static int files_log_ref_write(struct files_ref_store *refs, @@@ -1651,7 -1660,7 +1651,7 @@@ static int write_ref_to_lockfile(struc struct object *o; int fd; - o = parse_object(oid); + o = parse_object(the_repository, oid); if (!o) { strbuf_addf(err, "trying to write ref '%s' with nonexistent object %s", @@@ -1667,7 -1676,7 +1667,7 @@@ return -1; } fd = get_lock_file_fd(&lock->lk); - if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) < 0 || + if (write_in_full(fd, oid_to_hex(oid), the_hash_algo->hexsz) < 0 || write_in_full(fd, &term, 1) < 0 || close_ref_gently(lock) < 0) { strbuf_addf(err, @@@ -3061,7 -3070,7 +3061,7 @@@ static int files_reflog_expire(struct r rollback_lock_file(&reflog_lock); } else if (update && (write_in_full(get_lock_file_fd(&lock->lk), - oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) < 0 || + oid_to_hex(&cb.last_kept_oid), the_hash_algo->hexsz) < 0 || write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 || close_ref_gently(lock) < 0)) { status |= error("couldn't write %s", diff --combined strbuf.c index 030556111d,54f29bbb23..64041c3c24 --- a/strbuf.c +++ b/strbuf.c @@@ -134,7 -134,7 +134,7 @@@ void strbuf_ltrim(struct strbuf *sb int strbuf_reencode(struct strbuf *sb, const char *from, const char *to) { char *out; - int len; + size_t len; if (same_encoding(from, to)) return 0; @@@ -209,7 -209,7 +209,7 @@@ void strbuf_list_free(struct strbuf **s int strbuf_cmp(const struct strbuf *a, const struct strbuf *b) { - int len = a->len < b->len ? a->len: b->len; + size_t len = a->len < b->len ? a->len: b->len; int cmp = memcmp(a->buf, b->buf, len); if (cmp) return cmp; @@@ -389,7 -389,7 +389,7 @@@ size_t strbuf_expand_dict_cb(struct str void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src) { - int i, len = src->len; + size_t i, len = src->len; for (i = 0; i < len; i++) { if (src->buf[i] == '%') @@@ -469,7 -469,7 +469,7 @@@ int strbuf_readlink(struct strbuf *sb, hint = 32; while (hint < STRBUF_MAXLINK) { - int len; + ssize_t len; strbuf_grow(sb, hint); len = readlink(path, sb->buf, hint); @@@ -734,18 -734,18 +734,18 @@@ void strbuf_humanise_bytes(struct strbu { if (bytes > 1 << 30) { strbuf_addf(buf, "%u.%2.2u GiB", - (int)(bytes >> 30), - (int)(bytes & ((1 << 30) - 1)) / 10737419); + (unsigned)(bytes >> 30), + (unsigned)(bytes & ((1 << 30) - 1)) / 10737419); } else if (bytes > 1 << 20) { - int x = bytes + 5243; /* for rounding */ + unsigned x = bytes + 5243; /* for rounding */ strbuf_addf(buf, "%u.%2.2u MiB", x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20); } else if (bytes > 1 << 10) { - int x = bytes + 5; /* for rounding */ + unsigned x = bytes + 5; /* for rounding */ strbuf_addf(buf, "%u.%2.2u KiB", x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10); } else { - strbuf_addf(buf, "%u bytes", (int)bytes); + strbuf_addf(buf, "%u bytes", (unsigned)bytes); } } @@@ -921,7 -921,7 +921,7 @@@ void strbuf_add_unique_abbrev(struct st int abbrev_len) { int r; - strbuf_grow(sb, GIT_SHA1_HEXSZ + 1); + strbuf_grow(sb, GIT_MAX_HEXSZ + 1); r = find_unique_abbrev_r(sb->buf + sb->len, oid, abbrev_len); strbuf_setlen(sb, sb->len + r); } @@@ -960,7 -960,7 +960,7 @@@ static size_t cleanup(char *line, size_ */ void strbuf_stripspace(struct strbuf *sb, int skip_comments) { - int empties = 0; + size_t empties = 0; size_t i, j, len, newlen; char *eol; diff --combined utf8.c index 982217eec9,edcd1e835a..eb78587504 --- a/utf8.c +++ b/utf8.c @@@ -470,14 -470,14 +470,14 @@@ int utf8_fprintf(FILE *stream, const ch #else typedef char * iconv_ibp; #endif - char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv, int *outsz_p) + char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv, size_t *outsz_p) { size_t outsz, outalloc; char *out, *outpos; iconv_ibp cp; outsz = insz; - outalloc = outsz + 1; /* for terminating NUL */ + outalloc = st_add(outsz, 1); /* for terminating NUL */ out = xmalloc(outalloc); outpos = out; cp = (iconv_ibp)in; @@@ -497,7 -497,7 +497,7 @@@ * converting the rest. */ sofar = outpos - out; - outalloc = sofar + insz * 2 + 32; + outalloc = st_add3(sofar, st_mult(insz, 2), 32); out = xrealloc(out, outalloc); outpos = out + sofar; outsz = outalloc - sofar - 1; @@@ -534,9 -534,9 +534,9 @@@ static const char *fallback_encoding(co return name; } - char *reencode_string_len(const char *in, int insz, + char *reencode_string_len(const char *in, size_t insz, const char *out_encoding, const char *in_encoding, - int *outsz) + size_t *outsz) { iconv_t conv; char *out; @@@ -566,10 -566,10 +566,10 @@@ static int has_bom_prefix(const char *d return data && bom && (len >= bom_len) && !memcmp(data, bom, bom_len); } -static const char utf16_be_bom[] = {0xFE, 0xFF}; -static const char utf16_le_bom[] = {0xFF, 0xFE}; -static const char utf32_be_bom[] = {0x00, 0x00, 0xFE, 0xFF}; -static const char utf32_le_bom[] = {0xFF, 0xFE, 0x00, 0x00}; +static const char utf16_be_bom[] = {'\xFE', '\xFF'}; +static const char utf16_le_bom[] = {'\xFF', '\xFE'}; +static const char utf32_be_bom[] = {'\0', '\0', '\xFE', '\xFF'}; +static const char utf32_le_bom[] = {'\xFF', '\xFE', '\0', '\0'}; int has_prohibited_utf_bom(const char *enc, const char *data, size_t len) {