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:
40
utils.c
40
utils.c
@ -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, "<", "<");
|
||||
esc_tag = modifyString(esc_tag, ">", ">");
|
||||
esc_tag = modifyString(esc_tag, """, "\"");
|
||||
esc_tag = modifyString(esc_tag, "&", "&", 1);
|
||||
esc_tag = modifyString(esc_tag, "<", "<", 1);
|
||||
esc_tag = modifyString(esc_tag, ">", ">", 1);
|
||||
esc_tag = modifyString(esc_tag, """, "\"", 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;");
|
||||
esc_tag = modifyString(esc_tag, "<", "&lt;");
|
||||
esc_tag = modifyString(esc_tag, ">", "&gt;");
|
||||
esc_tag = modifyString(esc_tag, "\"", "&quot;");
|
||||
esc_tag = modifyString(esc_tag, "&", "&amp;", 0);
|
||||
esc_tag = modifyString(esc_tag, "<", "&lt;", 0);
|
||||
esc_tag = modifyString(esc_tag, ">", "&gt;", 0);
|
||||
esc_tag = modifyString(esc_tag, "\"", "&quot;", 0);
|
||||
}
|
||||
else if( force_alloc )
|
||||
esc_tag = strdup(tag);
|
||||
|
Reference in New Issue
Block a user