bookmark.c fix bookmark.c fix filename generator #2

strlcpy returns the size of the string it tried to create so we still need strlen

since we know what the sizes are of the strings just check for overflow first and use strmemccpy

fix bufsz on playlist_get_name()

Change-Id: Iaa52f869994ca94487c19b0cf2958330db4fc786
This commit is contained in:
William Wilgus 2022-11-27 00:35:34 -05:00 committed by William Wilgus
parent 65db4acabe
commit 3f4e55a872

View file

@ -43,7 +43,7 @@
#include "file.h" #include "file.h"
#include "pathfuncs.h" #include "pathfuncs.h"
/* #define LOGF_ENABLE */ /*#define LOGF_ENABLE*/
#include "logf.h" #include "logf.h"
#define MAX_BOOKMARKS 10 #define MAX_BOOKMARKS 10
@ -351,11 +351,16 @@ static bool generate_bookmark_file_name(char *filenamebuf,
strmemccpy(filenamebuf, "/root_dir.bmark", filenamebufsz); strmemccpy(filenamebuf, "/root_dir.bmark", filenamebufsz);
else else
{ {
filenamebufsz--; /* strlcpy considers the NULL so bmarknamelen is one off */ size_t buflen, len;
size_t len = strlcpy(filenamebuf, bmarknamein, /* strmemccpy considers the NULL so bmarknamelen is one off */
MIN(filenamebufsz, bmarknamelen) + 1); buflen = MIN(filenamebufsz -1 , bmarknamelen);
if(len >= filenamebufsz) if (buflen >= filenamebufsz)
return false; return false;
strmemccpy(filenamebuf, bmarknamein, buflen + 1);
len = strlen(filenamebuf);
#ifdef HAVE_MULTIVOLUME #ifdef HAVE_MULTIVOLUME
/* The "root" of an extra volume need special handling too. */ /* The "root" of an extra volume need special handling too. */
const char *filename; const char *filename;
@ -404,7 +409,7 @@ static char* create_bookmark(char **name, size_t *namelen)
if(!resume_info.id3) if(!resume_info.id3)
return NULL; return NULL;
size_t bmsz = snprintf(buf, bufsz, size_t bmarksz= snprintf(buf, bufsz,
/* new optional bookmark token descriptors should /* new optional bookmark token descriptors should
be inserted just after ';"' in this line... */ be inserted just after ';"' in this line... */
#if defined(HAVE_PITCHCONTROL) #if defined(HAVE_PITCHCONTROL)
@ -431,20 +436,20 @@ static char* create_bookmark(char **name, size_t *namelen)
#endif #endif
); /*sprintf*/ ); /*sprintf*/
/* mandatory tokens */ /* mandatory tokens */
if (bmsz >= bufsz) /* include NULL*/ if (bmarksz >= bufsz) /* include NULL*/
return NULL; return NULL;
buf += bmsz; buf += bmarksz;
bufsz -= bmsz; bufsz -= bmarksz;
/* create the bookmark */ /* create the bookmark */
playlist_get_name(NULL, buf, bmsz); playlist_get_name(NULL, buf, bufsz);
bmsz = strlen(buf); bmarksz = strlen(buf);
if (bmsz == 0 || (bmsz + 1) >= bufsz) /* include the separator & NULL*/ if (bmarksz == 0 || (bmarksz + 1) >= bufsz) /* include the separator & NULL*/
return NULL; return NULL;
*name = buf; /* return the playlist name through the *pointer */ *name = buf; /* return the playlist name through the *pointer */
*namelen = bmsz; /* return the name length through the pointer */ *namelen = bmarksz; /* return the name length through the pointer */
/* Get the currently playing file minus the path */ /* Get the currently playing file minus the path */
/* This is used when displaying the available bookmarks */ /* This is used when displaying the available bookmarks */
@ -452,12 +457,12 @@ static char* create_bookmark(char **name, size_t *namelen)
if(NULL == file) if(NULL == file)
return NULL; return NULL;
if (buf[bmsz - 1] != '/') if (buf[bmarksz - 1] != '/')
file = resume_info.id3->path; file = resume_info.id3->path;
else file++; else file++;
buf += bmsz; buf += bmarksz;
bufsz -= (bmsz + 1); bufsz -= (bmarksz + 1);
buf[0] = ';'; buf[0] = ';';
buf[1] = '\0'; buf[1] = '\0';