utils: add noalloc flag to modifyString()

This should make it more straightforward for code analyzers to
tell if the function is going to allocate memory.
This commit is contained in:
Justin Maggard 2014-04-18 15:18:44 -07:00
parent b0550495e8
commit 331d484555
5 changed files with 32 additions and 24 deletions

View File

@ -704,7 +704,7 @@ start_inotify()
i += EVENT_SIZE + event->len;
continue;
}
esc_name = modifyString(strdup(event->name), "&", "&");
esc_name = modifyString(strdup(event->name), "&", "&", 0);
snprintf(path_buf, sizeof(path_buf), "%s/%s", get_path_from_wd(event->wd), event->name);
if ( event->mask & IN_ISDIR && (event->mask & (IN_CREATE|IN_MOVED_TO)) )
{

View File

@ -236,7 +236,7 @@ found:
DPRINTF(E_DEBUG, L_SCANNER, "- %s not found in db\n", fname);
if( strchr(fname, '\\') )
{
fname = modifyString(fname, "\\", "/");
fname = modifyString(fname, "\\", "/", 1);
goto retry;
}
else if( (fname = strchr(fname, '/')) )

View File

@ -118,11 +118,11 @@ SendFormats(struct upnphttp *h, const char *sformat)
static char *
tivo_unescape_tag(char *tag)
{
modifyString(tag, "&", "&");
modifyString(tag, "<", "<");
modifyString(tag, "<", "<");
modifyString(tag, ">", ">");
modifyString(tag, ">", ">");
modifyString(tag, "&", "&", 1);
modifyString(tag, "<", "<", 1);
modifyString(tag, "<", "<", 1);
modifyString(tag, ">", ">", 1);
modifyString(tag, ">", ">", 1);
return tag;
}

40
utils.c
View File

@ -31,6 +31,7 @@
#include "minidlnatypes.h"
#include "upnpglobalvars.h"
#include "utils.h"
#include "log.h"
int
@ -143,20 +144,27 @@ strcasestrc(const char *s, const char *p, const char t)
}
char *
modifyString(char * string, const char * before, const char * after)
modifyString(char *string, const char *before, const char *after, int noalloc)
{
int oldlen, newlen, chgcnt = 0;
char *s, *p;
/* If there is no match, just return */
s = strstr(string, before);
if (!s)
return string;
oldlen = strlen(before);
newlen = strlen(after);
if( newlen > oldlen )
if (newlen > oldlen)
{
s = string;
while( (p = strstr(s, before)) )
if (noalloc)
return string;
while ((p = strstr(s, before)))
{
chgcnt++;
s = p+oldlen;
s = p + oldlen;
}
s = realloc(string, strlen(string)+((newlen-oldlen)*chgcnt)+1);
/* If we failed to realloc, return the original alloc'd string */
@ -167,10 +175,10 @@ modifyString(char * string, const char * before, const char * after)
}
s = string;
while( s )
while (s)
{
p = strcasestr(s, before);
if( !p )
p = strstr(s, before);
if (!p)
return string;
memmove(p + newlen, p + oldlen, strlen(p + oldlen) + 1);
memcpy(p, after, newlen);
@ -189,10 +197,10 @@ unescape_tag(const char *tag, int force_alloc)
|| strstr(tag, """) )
{
esc_tag = strdup(tag);
esc_tag = modifyString(esc_tag, "&", "&");
esc_tag = modifyString(esc_tag, "&lt;", "<");
esc_tag = modifyString(esc_tag, "&gt;", ">");
esc_tag = modifyString(esc_tag, "&quot;", "\"");
esc_tag = modifyString(esc_tag, "&amp;", "&", 1);
esc_tag = modifyString(esc_tag, "&lt;", "<", 1);
esc_tag = modifyString(esc_tag, "&gt;", ">", 1);
esc_tag = modifyString(esc_tag, "&quot;", "\"", 1);
}
else if( force_alloc )
esc_tag = strdup(tag);
@ -208,10 +216,10 @@ escape_tag(const char *tag, int force_alloc)
if( strchr(tag, '&') || strchr(tag, '<') || strchr(tag, '>') || strchr(tag, '"') )
{
esc_tag = strdup(tag);
esc_tag = modifyString(esc_tag, "&", "&amp;amp;");
esc_tag = modifyString(esc_tag, "<", "&amp;lt;");
esc_tag = modifyString(esc_tag, ">", "&amp;gt;");
esc_tag = modifyString(esc_tag, "\"", "&amp;quot;");
esc_tag = modifyString(esc_tag, "&", "&amp;amp;", 0);
esc_tag = modifyString(esc_tag, "<", "&amp;lt;", 0);
esc_tag = modifyString(esc_tag, ">", "&amp;gt;", 0);
esc_tag = modifyString(esc_tag, "\"", "&amp;quot;", 0);
}
else if( force_alloc )
esc_tag = strdup(tag);

View File

@ -60,7 +60,7 @@ int ends_with(const char * haystack, const char * needle);
char *trim(char *str);
char *strstrc(const char *s, const char *p, const char t);
char *strcasestrc(const char *s, const char *p, const char t);
char *modifyString(char * string, const char * before, const char * after);
char *modifyString(char *string, const char *before, const char *after, int noalloc);
char *escape_tag(const char *tag, int force_alloc);
char *unescape_tag(const char *tag, int force_alloc);
void strip_ext(char * name);