From: Pierre Habouzit <madcoder@debian.org>
To: git@vger.kernel.org
Cc: Pierre Habouzit <madcoder@debian.org>
Subject: [PATCH 2/6] Change semantics of interpolate to work like snprintf.
Date: Sun, 9 Sep 2007 02:04:32 +0200 [thread overview]
Message-ID: <11892962764194-git-send-email-madcoder@debian.org> (raw)
In-Reply-To: <11892962761617-git-send-email-madcoder@debian.org>
Also fix many off-by-ones and a useless memset.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
commit.c | 9 ++++-----
interpolate.c | 20 ++++++++------------
2 files changed, 12 insertions(+), 17 deletions(-)
diff --git a/commit.c b/commit.c
index 99f65ce..25781cc 100644
--- a/commit.c
+++ b/commit.c
@@ -923,15 +923,14 @@ long format_commit_message(const struct commit *commit, const void *format,
do {
char *buf = *buf_p;
- unsigned long space = *space_p;
+ unsigned long len;
- space = interpolate(buf, space, format,
+ len = interpolate(buf, *space_p, format,
table, ARRAY_SIZE(table));
- if (!space)
+ if (len < *space_p)
break;
- buf = xrealloc(buf, space);
+ ALLOC_GROW(buf, len + 1, *space_p);
*buf_p = buf;
- *space_p = space;
} while (1);
interp_clear_table(table, ARRAY_SIZE(table));
diff --git a/interpolate.c b/interpolate.c
index 0082677..ff4fb10 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;
@@ -89,14 +86,13 @@ unsigned long interpolate(char *result, unsigned long reslen,
}
}
/* Straight copy one non-interpolation character. */
- if (newlen + 1 < reslen)
+ if (newlen < reslen)
*dest++ = *src;
src++;
newlen++;
}
- if (newlen + 1 < reslen)
- return 0;
- else
- return newlen + 2;
+ if (reslen > 0)
+ *dest = '\0';
+ return newlen;
}
--
1.5.3.1
next prev parent reply other threads:[~2007-09-09 8:43 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-09 0:04 Use more strbufs series [on top of next] Pierre Habouzit
2007-09-09 0:04 ` [PATCH 1/6] Add strbuf_rtrim and strbuf_insert Pierre Habouzit
2007-09-09 0:04 ` Pierre Habouzit [this message]
2007-09-09 0:04 ` [PATCH 3/6] Rework pretty_print_commit to use strbufs instead of custom buffers Pierre Habouzit
2007-09-09 0:04 ` [PATCH 4/6] Use strbuf_read in builtin-fetch-tool.c Pierre Habouzit
2007-09-09 0:04 ` [PATCH 5/6] Use strbufs to in read_message (imap-send.c), custom buffer-- Pierre Habouzit
2007-09-09 0:04 ` [PATCH 6/6] Replace all read_fd use with strbuf_read, and get rid of it Pierre Habouzit
[not found] ` <7vsl5nflj2.fsf@gitster.siamese.dyndns.org>
[not found] ` <20070910090656.GC3417@artemis.corp>
[not found] ` <7vir6isvu1.fsf@gitster.siamese.dyndns.org>
[not found] ` <7v642iqumt.fsf@gitster.siamese.dyndns.org>
[not found] ` <20070910183256.GD32442@artemis.corp>
2007-09-27 22:56 ` return void expressions in C (Was: [PATCH 3/6] Rework pretty_print_commit ...) Linus Torvalds
2007-09-09 0:12 ` Use more strbufs series [on top of next] Pierre Habouzit
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=11892962764194-git-send-email-madcoder@debian.org \
--to=madcoder@debian.org \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).