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:
		@@ -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)) )
 | 
			
		||||
				{
 | 
			
		||||
 
 | 
			
		||||
@@ -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, '/')) )
 | 
			
		||||
 
 | 
			
		||||
@@ -118,11 +118,11 @@ SendFormats(struct upnphttp *h, const char *sformat)
 | 
			
		||||
static char *
 | 
			
		||||
tivo_unescape_tag(char *tag)
 | 
			
		||||
{
 | 
			
		||||
	modifyString(tag, "&", "&");
 | 
			
		||||
	modifyString(tag, "&amp;lt;", "<");
 | 
			
		||||
	modifyString(tag, "&lt;", "<");
 | 
			
		||||
	modifyString(tag, "&amp;gt;", ">");
 | 
			
		||||
	modifyString(tag, "&gt;", ">");
 | 
			
		||||
	modifyString(tag, "&amp;", "&", 1);
 | 
			
		||||
	modifyString(tag, "&amp;lt;", "<", 1);
 | 
			
		||||
	modifyString(tag, "&lt;", "<", 1);
 | 
			
		||||
	modifyString(tag, "&amp;gt;", ">", 1);
 | 
			
		||||
	modifyString(tag, "&gt;", ">", 1);
 | 
			
		||||
	return tag;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										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);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								utils.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								utils.h
									
									
									
									
									
								
							@@ -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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user