2006-09-20 01:31:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2006 Jon Loeliger
|
|
|
|
*/
|
|
|
|
|
2006-09-27 16:16:10 +00:00
|
|
|
#include "git-compat-util.h"
|
2006-09-20 01:31:51 +00:00
|
|
|
#include "interpolate.h"
|
|
|
|
|
|
|
|
|
2006-09-28 19:12:55 +00:00
|
|
|
void interp_set_entry(struct interp *table, int slot, const char *value)
|
2006-09-27 16:16:10 +00:00
|
|
|
{
|
|
|
|
char *oldval = table[slot].value;
|
2006-09-28 19:12:55 +00:00
|
|
|
char *newval = NULL;
|
2006-09-27 16:16:10 +00:00
|
|
|
|
Avoid unnecessary "if-before-free" tests.
This change removes all obvious useless if-before-free tests.
E.g., it replaces code like this:
if (some_expression)
free (some_expression);
with the now-equivalent:
free (some_expression);
It is equivalent not just because POSIX has required free(NULL)
to work for a long time, but simply because it has worked for
so long that no reasonable porting target fails the test.
Here's some evidence from nearly 1.5 years ago:
http://www.winehq.org/pipermail/wine-patches/2006-October/031544.html
FYI, the change below was prepared by running the following:
git ls-files -z | xargs -0 \
perl -0x3b -pi -e \
's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+(free\s*\(\s*\1\s*\))/$2/s'
Note however, that it doesn't handle brace-enclosed blocks like
"if (x) { free (x); }". But that's ok, since there were none like
that in git sources.
Beware: if you do use the above snippet, note that it can
produce syntactically invalid C code. That happens when the
affected "if"-statement has a matching "else".
E.g., it would transform this
if (x)
free (x);
else
foo ();
into this:
free (x);
else
foo ();
There were none of those here, either.
If you're interested in automating detection of the useless
tests, you might like the useless-if-before-free script in gnulib:
[it *does* detect brace-enclosed free statements, and has a --name=S
option to make it detect free-like functions with different names]
http://git.sv.gnu.org/gitweb/?p=gnulib.git;a=blob;f=build-aux/useless-if-before-free
Addendum:
Remove one more (in imap-send.c), spotted by Jean-Luc Herren <jlh@gmx.ch>.
Signed-off-by: Jim Meyering <meyering@redhat.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-01-31 17:26:32 +00:00
|
|
|
free(oldval);
|
2006-09-27 16:16:10 +00:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
newval = xstrdup(value);
|
|
|
|
|
|
|
|
table[slot].value = newval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void interp_clear_table(struct interp *table, int ninterps)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ninterps; i++) {
|
|
|
|
interp_set_entry(table, i, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-20 01:31:51 +00:00
|
|
|
/*
|
|
|
|
* Convert a NUL-terminated string in buffer orig
|
|
|
|
* into the supplied buffer, result, whose length is reslen,
|
|
|
|
* performing substitutions on %-named sub-strings from
|
|
|
|
* the table, interps, with ninterps entries.
|
|
|
|
*
|
|
|
|
* Example interps:
|
|
|
|
* {
|
|
|
|
* { "%H", "example.org"},
|
|
|
|
* { "%port", "123"},
|
|
|
|
* { "%%", "%"},
|
|
|
|
* }
|
|
|
|
*
|
2007-09-10 10:35:05 +00:00
|
|
|
* Returns the length of the substituted string (not including the final \0).
|
|
|
|
* Like with snprintf, if the result is >= reslen, then it overflowed.
|
2006-09-20 01:31:51 +00:00
|
|
|
*/
|
|
|
|
|
2007-06-11 07:34:54 +00:00
|
|
|
unsigned long interpolate(char *result, unsigned long reslen,
|
2006-09-25 21:19:00 +00:00
|
|
|
const char *orig,
|
|
|
|
const struct interp *interps, int ninterps)
|
2006-09-20 01:31:51 +00:00
|
|
|
{
|
2006-09-25 21:19:00 +00:00
|
|
|
const char *src = orig;
|
2006-09-20 01:31:51 +00:00
|
|
|
char *dest = result;
|
2007-06-11 07:34:54 +00:00
|
|
|
unsigned long newlen = 0;
|
2007-03-07 01:44:17 +00:00
|
|
|
const char *name, *value;
|
2007-06-11 07:34:54 +00:00
|
|
|
unsigned long namelen, valuelen;
|
2006-09-20 01:31:51 +00:00
|
|
|
int i;
|
|
|
|
char c;
|
|
|
|
|
2007-06-11 07:34:54 +00:00
|
|
|
while ((c = *src)) {
|
2006-09-20 01:31:51 +00:00
|
|
|
if (c == '%') {
|
|
|
|
/* Try to match an interpolation string. */
|
|
|
|
for (i = 0; i < ninterps; i++) {
|
|
|
|
name = interps[i].name;
|
|
|
|
namelen = strlen(name);
|
2007-06-11 07:34:54 +00:00
|
|
|
if (strncmp(src, name, namelen) == 0)
|
2006-09-20 01:31:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for valid interpolation. */
|
|
|
|
if (i < ninterps) {
|
|
|
|
value = interps[i].value;
|
2007-09-25 14:38:46 +00:00
|
|
|
if (!value) {
|
|
|
|
src += namelen;
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-20 01:31:51 +00:00
|
|
|
|
2007-09-25 14:38:46 +00:00
|
|
|
valuelen = strlen(value);
|
2007-09-10 10:35:05 +00:00
|
|
|
if (newlen + valuelen < reslen) {
|
2006-09-20 01:31:51 +00:00
|
|
|
/* Substitute. */
|
2007-09-10 10:35:05 +00:00
|
|
|
memcpy(dest, value, valuelen);
|
2006-09-20 01:31:51 +00:00
|
|
|
dest += valuelen;
|
|
|
|
}
|
2007-06-11 07:34:54 +00:00
|
|
|
newlen += valuelen;
|
|
|
|
src += namelen;
|
|
|
|
continue;
|
2006-09-20 01:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-11 07:34:54 +00:00
|
|
|
/* Straight copy one non-interpolation character. */
|
|
|
|
if (newlen + 1 < reslen)
|
|
|
|
*dest++ = *src;
|
|
|
|
src++;
|
|
|
|
newlen++;
|
2006-09-20 01:31:51 +00:00
|
|
|
}
|
|
|
|
|
2007-09-10 10:35:05 +00:00
|
|
|
/* 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;
|
2006-09-20 01:31:51 +00:00
|
|
|
}
|