From: john at keeping.me.uk (John Keeping)
Subject: [PATCH 2/2] shared: use strbuf for expanding macros
Date: Sat, 16 Jun 2018 14:07:29 +0100 [thread overview]
Message-ID: <557cc424e8607a045d2ffdbca94c35c11caaa1d7.1529154091.git.john@keeping.me.uk> (raw)
In-Reply-To: <b4f6dd090a86f0216666e3dd4e2e0aa11fa7ce28.1529154091.git.john@keeping.me.uk>
Avoid a fixed size buffer by using struct strbuf to build the expanded
value.
Signed-off-by: John Keeping <john at keeping.me.uk>
---
shared.c | 73 +++++++++++++++++++++-----------------------------------
1 file changed, 27 insertions(+), 46 deletions(-)
diff --git a/shared.c b/shared.c
index 32d0f46..6ce3990 100644
--- a/shared.c
+++ b/shared.c
@@ -467,69 +467,50 @@ static int is_token_char(char c)
return isalnum(c) || c == '_';
}
-/* Replace name with getenv(name), return pointer to zero-terminating char
+/* Replace name with getenv(name).
*/
-static char *expand_macro(char *name, int maxlength)
+static void expand_macro(struct strbuf *buf, ssize_t start)
{
- char *value;
- int len;
+ const char *name = buf->buf + start;
+ const char *value = getenv(name);
- len = 0;
- value = getenv(name);
- if (value) {
- len = strlen(value);
- if (len > maxlength)
- len = maxlength;
- strncpy(name, value, len);
- }
- return name + len;
+ /* Truncate output to remove variable name. */
+ strbuf_setlen(buf, start);
+
+ if (value)
+ strbuf_addstr(buf, value);
}
-#define EXPBUFSIZE (1024 * 8)
-
/* Replace all tokens prefixed by '$' in the specified text with the
* value of the named environment variable.
* NB: the return value is allocated, it must be freed by the caller.
*/
char *expand_macros(const char *txt)
{
- static char result[EXPBUFSIZE];
- char *p, *start;
- int len;
+ struct strbuf result = STRBUF_INIT;
+ ssize_t start = -1;
- p = result;
- start = NULL;
- while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
- *p = *txt;
- if (start) {
+ while (txt && *txt) {
+ if (start >= 0) {
if (!is_token_char(*txt)) {
- if (p - start > 0) {
- *p = '\0';
- len = result + EXPBUFSIZE - start - 1;
- p = expand_macro(start, len) - 1;
- }
- start = NULL;
- txt--;
+ if (result.len - start >= 0)
+ expand_macro(&result, start);
+ start = -1;
}
- p++;
- txt++;
- continue;
}
- if (*txt == '$') {
- start = p;
- txt++;
- continue;
- }
- p++;
+
+ if (*txt == '$')
+ start = result.len;
+ else
+ strbuf_addch(&result, *txt);
+
txt++;
}
- *p = '\0';
- if (start && p - start > 0) {
- len = result + EXPBUFSIZE - start - 1;
- p = expand_macro(start, len);
- *p = '\0';
- }
- return xstrdup(result);
+
+ if (start >= 0 && result.len - start > 0)
+ expand_macro(&result, start);
+
+ return strbuf_detach(&result, NULL);
}
char *get_mimetype_for_filename(const char *filename)
--
2.17.1
next prev parent reply other threads:[~2018-06-16 13:07 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-12 23:33 [PATCH 1/2] gcc8.1: fix strncpy bounds warnings andy
2018-06-12 23:34 ` [PATCH 2/2] gcc8.1: fix strcat warning andy
2018-06-16 13:11 ` john
2018-06-16 21:52 ` list
2018-06-17 2:28 ` andy
2018-06-16 13:04 ` [PATCH 1/2] gcc8.1: fix strncpy bounds warnings john
2018-06-16 13:07 ` [PATCH 1/2] shared: allocate return value from expand_macros() john
2018-06-16 13:07 ` john [this message]
2018-06-16 13:12 ` [PATCH 1/2] gcc8.1: fix strncpy bounds warnings andy
2018-06-16 14:14 ` john
2018-06-26 11:36 ` [PATCH v2 0/2] " andy
2018-06-26 11:36 ` [PATCH v2 1/2] " andy
2018-06-26 11:36 ` [PATCH v2 2/2] cgit_repobasename: convert to allocated result andy
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=557cc424e8607a045d2ffdbca94c35c11caaa1d7.1529154091.git.john@keeping.me.uk \
--to=cgit@lists.zx2c4.com \
/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.
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 NNTP newsgroup(s).