aboutsummaryrefslogtreecommitdiffstats
path: root/src/path.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2006-07-30 10:31:41 +0000
committerEric Wong <normalperson@yhbt.net>2006-07-30 10:31:41 +0000
commit263a9d583a74a94fef652bb40f4035b9c0c1612e (patch)
tree52f15e2c2d0e60e87bb1a135c55e4345e2511f2f /src/path.c
parent4144afe551eca63366316c013019bce9ad81bc94 (diff)
downloadmpd-263a9d583a74a94fef652bb40f4035b9c0c1612e.tar.gz
mpd-263a9d583a74a94fef652bb40f4035b9c0c1612e.tar.xz
mpd-263a9d583a74a94fef652bb40f4035b9c0c1612e.zip
remove clumsy strncpy use
strncpy isn't really safe because it doesn't guarantee null termination, and we have had to work around it in several places. strlcpy (from OpenBSD) isn't great, either because it often leaves errors going unchecked (by truncating strings). So we'll add the pathcpy_trunc() function with is basically strlcpy with a hardcoded MAXPATHLEN as the limit, and we'll acknowledge truncation since we only work on paths and MAXPATHLEN should be set correctly by the system headers[1]. file-specific notes: inputStream_http: eyeballing the changes here, it seems to look alright but I haven't actually tested it myself. ls: don't even bother printing a file if the filename is too long (and when is it ever?) since we won't be able to read it anyways. metadataChunk: it's only metadata, and it's only for showin the user, so truncating it here souldn't be a big issue. memset to zero in init is unecessary, so lets not waste cycles [1] - If the system headers are screwed up, then we're majorly screwed regardless of what we do :x git-svn-id: https://svn.musicpd.org/mpd/trunk@4491 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to 'src/path.c')
-rw-r--r--src/path.c61
1 files changed, 40 insertions, 21 deletions
diff --git a/src/path.c b/src/path.c
index 3a6cce23b..69c86391b 100644
--- a/src/path.c
+++ b/src/path.c
@@ -37,10 +37,9 @@
#endif
#endif
-char *musicDir;
-char *playlistDir;
-
-char *fsCharset = NULL;
+const char *musicDir;
+static const char *playlistDir;
+static char *fsCharset = NULL;
static char *pathConvCharset(char *to, char *from, char *str)
{
@@ -205,39 +204,59 @@ void finishPaths(void)
fsCharset = NULL;
}
-char *rmp2amp(char *relativePath)
+static char *pfx_path(const char *path, const char *pfx, const size_t pfx_len)
{
- static char absolutePath[MAXPATHLEN + 1];
-
- memset(absolutePath, 0, MAXPATHLEN + 1);
+ static char ret[MAXPATHLEN+1];
+ size_t rp_len = strlen(path);
+
+ /* check for the likely condition first: */
+ if (mpd_likely((pfx_len + rp_len) < MAXPATHLEN)) {
+ memcpy(ret, pfx, pfx_len);
+ memcpy(ret + pfx_len, path, rp_len + 1);
+ return ret;
+ }
- strncpy(absolutePath, musicDir, MAXPATHLEN);
- strncat(absolutePath, relativePath, MAXPATHLEN - strlen(musicDir));
+ /* unlikely, return an empty string because truncating would
+ * also be wrong... break early and break loudly (the system
+ * headers are likely screwed, not mpd) */
+ ERROR("Cannot prefix '%s' to '%s', max: %d", pfx, path, MAXPATHLEN);
+ ret[0] = '\0';
+ return ret;
+}
- return absolutePath;
+char *rmp2amp(char *relativePath)
+{
+ size_t pfx_len = strlen(musicDir);
+ return pfx_path(relativePath, musicDir, pfx_len);
}
char *rpp2app(char *relativePath)
{
- static char absolutePath[MAXPATHLEN + 1];
-
- memset(absolutePath, 0, MAXPATHLEN + 1);
+ size_t pfx_len = strlen(playlistDir);
+ return pfx_path(relativePath, playlistDir, pfx_len);
+}
- strncpy(absolutePath, playlistDir, MAXPATHLEN);
- strncat(absolutePath, relativePath, MAXPATHLEN - strlen(musicDir));
+/* this is actually like strlcpy (OpenBSD), but we don't actually want to
+ * blindly use it everywhere, only for paths that are OK to truncate (for
+ * error reporting and such */
+void pathcpy_trunc(char *dest, const char *src)
+{
+ size_t len = strlen(src);
- return absolutePath;
+ if (mpd_unlikely(len > MAXPATHLEN))
+ len = MAXPATHLEN;
+ memcpy(dest, src, len);
+ dest[len] = '\0';
}
char *parentPath(char *path)
{
- static char parentPath[MAXPATHLEN + 1];
+ static char parentPath[MAXPATHLEN+1];
char *c;
- memset(parentPath, 0, MAXPATHLEN + 1);
- strncpy(parentPath, path, MAXPATHLEN);
+ pathcpy_trunc(parentPath, path);
+ c = strrchr(parentPath,'/');
- c = strrchr(parentPath, '/');
if (c == NULL)
parentPath[0] = '\0';
else {