Fixed problems with free in m3u-scanner

This commit is contained in:
ejurgensen 2013-10-15 23:40:06 +02:00
parent d710e6ee95
commit 3e484f1cae

View File

@ -43,7 +43,7 @@
static int static int
get_extinf(char *string, struct extinf_ctx *extinf) extinf_get(char *string, struct extinf_ctx *extinf)
{ {
regex_t *regex; regex_t *regex;
regmatch_t *regmatch; regmatch_t *regmatch;
@ -74,35 +74,33 @@ get_extinf(char *string, struct extinf_ctx *extinf)
} }
if (regexec(regex, string, 1 + regex->re_nsub, regmatch, 0)) if (regexec(regex, string, 1 + regex->re_nsub, regmatch, 0))
{ goto regmatchfail;
DPRINTF(E_DBG, L_SCAN, "Playlist line has no EXTINF data\n");
goto regmatchfail;
}
/* Found extinf */ /* Found extinf */
if (regmatch[0].rm_so != -1) if (regmatch[0].rm_so != -1)
{ {
DPRINTF(E_DBG, L_SCAN, "Playlist line has EXTINF data\n"); if (extinf->found)
{
free(extinf->artist);
free(extinf->title);
}
/* Artist */ /* Artist */
if (extinf->artist)
free(extinf->artist);
matchlen = regmatch[1].rm_eo - regmatch[1].rm_so; matchlen = regmatch[1].rm_eo - regmatch[1].rm_so;
extinf->artist = (char*)malloc(matchlen + 1); extinf->artist = (char*)malloc(matchlen + 1);
strncpy(extinf->artist, string + regmatch[1].rm_so, matchlen + 1); strncpy(extinf->artist, string + regmatch[1].rm_so, matchlen + 1);
extinf->artist[matchlen]='\0'; extinf->artist[matchlen]='\0';
/* Title */ /* Title */
if (extinf->title) matchlen = regmatch[2].rm_eo - regmatch[2].rm_so;
free(extinf->title);
matchlen = regmatch[1].rm_eo - regmatch[1].rm_so;
extinf->title = (char*)malloc(matchlen + 1); extinf->title = (char*)malloc(matchlen + 1);
strncpy(extinf->title, string + regmatch[1].rm_so, matchlen + 1); strncpy(extinf->title, string + regmatch[2].rm_so, matchlen + 1);
extinf->title[matchlen]='\0'; extinf->title[matchlen]='\0';
free(regmatch); free(regmatch);
regfree(regex); regfree(regex);
extinf->found = 1;
return 1; return 1;
} }
@ -113,6 +111,17 @@ get_extinf(char *string, struct extinf_ctx *extinf)
return 0; return 0;
} }
static void
extinf_reset(struct extinf_ctx *extinf)
{
if (extinf->found)
{
free(extinf->artist);
free(extinf->title);
}
extinf->found = 0;
}
void void
scan_m3u_playlist(char *file, time_t mtime) scan_m3u_playlist(char *file, time_t mtime)
{ {
@ -196,6 +205,7 @@ scan_m3u_playlist(char *file, time_t mtime)
} }
extinf.found = 0; extinf.found = 0;
while (fgets(buf, sizeof(buf), fp) != NULL) while (fgets(buf, sizeof(buf), fp) != NULL)
{ {
len = strlen(buf); len = strlen(buf);
@ -216,11 +226,8 @@ scan_m3u_playlist(char *file, time_t mtime)
continue; continue;
/* Saves metadata in extinf if EXTINF metadata line */ /* Saves metadata in extinf if EXTINF metadata line */
if (get_extinf(buf, &extinf)) if (extinf_get(buf, &extinf))
{ continue;
extinf.found = 1;
continue;
}
/* Check that first char is sane for a path */ /* Check that first char is sane for a path */
if ((!isalnum(buf[0])) && (buf[0] != '/') && (buf[0] != '.')) if ((!isalnum(buf[0])) && (buf[0] != '/') && (buf[0] != '.'))
@ -234,11 +241,14 @@ scan_m3u_playlist(char *file, time_t mtime)
filename = strdup(buf); filename = strdup(buf);
if (!filename) if (!filename)
{ {
DPRINTF(E_LOG, L_SCAN, "Out of memory for playlist filename.\n"); DPRINTF(E_LOG, L_SCAN, "Out of memory for playlist filename\n");
continue; continue;
} }
if (extinf.found)
DPRINTF(E_INFO, L_SCAN, "Playlist has EXTINF, artist is %s, title is %s\n", extinf.artist, extinf.title);
process_media_file(filename, mtime, 0, 0, 1, &extinf); process_media_file(filename, mtime, 0, 0, 1, &extinf);
} }
/* Regular file */ /* Regular file */
@ -298,7 +308,7 @@ scan_m3u_playlist(char *file, time_t mtime)
if (ret < 0) if (ret < 0)
DPRINTF(E_WARN, L_SCAN, "Could not add %s to playlist\n", filename); DPRINTF(E_WARN, L_SCAN, "Could not add %s to playlist\n", filename);
extinf.found = 0; extinf_reset(&extinf);
free(filename); free(filename);
} }
@ -311,10 +321,6 @@ scan_m3u_playlist(char *file, time_t mtime)
} }
fclose(fp); fclose(fp);
if (extinf.artist)
free(extinf.artist);
if (extinf.title)
free(extinf.title);
DPRINTF(E_INFO, L_SCAN, "Done processing playlist\n"); DPRINTF(E_INFO, L_SCAN, "Done processing playlist\n");
} }