aboutsummaryrefslogtreecommitdiff
path: root/interpolate.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2007-10-03 03:06:02 -0700
committerJunio C Hamano <gitster@pobox.com>2007-10-03 03:06:02 -0700
commit66d4035e1099477aa488c24c0c081ee764f85868 (patch)
treed89d0c1d61e5af45c19c0903d75aed970ffa2220 /interpolate.c
parente66273a6abb8e9cd0967d52113e29c8014a255f8 (diff)
parent9a76adebd6dfe144c3643e7e877fff2b41be5c8e (diff)
downloadgit-66d4035e1099477aa488c24c0c081ee764f85868.tar.gz
git-66d4035e1099477aa488c24c0c081ee764f85868.tar.xz
Merge branch 'ph/strbuf'
* ph/strbuf: (44 commits) Make read_patch_file work on a strbuf. strbuf_read_file enhancement, and use it. strbuf change: be sure ->buf is never ever NULL. double free in builtin-update-index.c Clean up stripspace a bit, use strbuf even more. Add strbuf_read_file(). rerere: Fix use of an empty strbuf.buf Small cache_tree_write refactor. Make builtin-rerere use of strbuf nicer and more efficient. Add strbuf_cmp. strbuf_setlen(): do not barf on setting length of an empty buffer to 0 sq_quote_argv and add_to_string rework with strbuf's. Full rework of quote_c_style and write_name_quoted. Rework unquote_c_style to work on a strbuf. strbuf API additions and enhancements. nfv?asprintf are broken without va_copy, workaround them. Fix the expansion pattern of the pseudo-static path buffer. builtin-for-each-ref.c::copy_name() - do not overstep the buffer. builtin-apply.c: fix a tiny leak introduced during xmemdupz() conversion. Use xmemdupz() in many places. ...
Diffstat (limited to 'interpolate.c')
-rw-r--r--interpolate.c20
1 files changed, 9 insertions, 11 deletions
diff --git a/interpolate.c b/interpolate.c
index 00826778f..3de583238 100644
--- a/interpolate.c
+++ b/interpolate.c
@@ -44,9 +44,8 @@ void interp_clear_table(struct interp *table, int ninterps)
* { "%%", "%"},
* }
*
- * Returns 0 on a successful substitution pass that fits in result,
- * Returns a number of bytes needed to hold the full substituted
- * string otherwise.
+ * Returns the length of the substituted string (not including the final \0).
+ * Like with snprintf, if the result is >= reslen, then it overflowed.
*/
unsigned long interpolate(char *result, unsigned long reslen,
@@ -61,8 +60,6 @@ unsigned long interpolate(char *result, unsigned long reslen,
int i;
char c;
- memset(result, 0, reslen);
-
while ((c = *src)) {
if (c == '%') {
/* Try to match an interpolation string. */
@@ -78,9 +75,9 @@ unsigned long interpolate(char *result, unsigned long reslen,
value = interps[i].value;
valuelen = strlen(value);
- if (newlen + valuelen + 1 < reslen) {
+ if (newlen + valuelen < reslen) {
/* Substitute. */
- strncpy(dest, value, valuelen);
+ memcpy(dest, value, valuelen);
dest += valuelen;
}
newlen += valuelen;
@@ -95,8 +92,9 @@ unsigned long interpolate(char *result, unsigned long reslen,
newlen++;
}
- if (newlen + 1 < reslen)
- return 0;
- else
- return newlen + 2;
+ /* XXX: the previous loop always keep room for the ending NUL,
+ we just need to check if there was room for a NUL in the first place */
+ if (reslen > 0)
+ *dest = '\0';
+ return newlen;
}