aboutsummaryrefslogtreecommitdiffstats
path: root/src/update.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-10-09 15:48:07 +0200
committerMax Kellermann <max@duempel.org>2008-10-09 15:48:07 +0200
commit4990f04ac09a68db70f8114ebb9d40e684db5d00 (patch)
tree9389046a2784ea07394728d613fc1d06fcc399e9 /src/update.c
parent8536a979202622bf8cda9c3cf4775f3dfb7ae4ac (diff)
downloadmpd-4990f04ac09a68db70f8114ebb9d40e684db5d00.tar.gz
mpd-4990f04ac09a68db70f8114ebb9d40e684db5d00.tar.xz
mpd-4990f04ac09a68db70f8114ebb9d40e684db5d00.zip
update: make addDirectoryPathToDB() non-recursive
This recursive function is very dangerous because it allocates a large buffer on the stack in every iteration. That may be misused to generate a stack overflow.
Diffstat (limited to 'src/update.c')
-rw-r--r--src/update.c26
1 files changed, 15 insertions, 11 deletions
diff --git a/src/update.c b/src/update.c
index 104f73400..d8bbc02d0 100644
--- a/src/update.c
+++ b/src/update.c
@@ -354,21 +354,25 @@ directory_make_child_checked(struct directory *parent, const char *path)
static struct directory *
addDirectoryPathToDB(const char *utf8path)
{
- char path_max_tmp[MPD_PATH_MAX];
- char *parent;
- struct directory *parentDirectory;
+ struct directory *directory = db_get_root();
+ char *duplicated = xstrdup(utf8path);
+ char *slash = duplicated;
- parent = parent_path(path_max_tmp, utf8path);
+ while (true) {
+ slash = strchr(slash, '/');
+ if (slash != NULL)
+ *slash = 0;
- if (strlen(parent) == 0)
- parentDirectory = db_get_root();
- else
- parentDirectory = addDirectoryPathToDB(parent);
+ directory = directory_make_child_checked(directory,
+ duplicated);
+ if (directory == NULL || slash == NULL)
+ break;
- if (!parentDirectory)
- return NULL;
+ *slash++ = '/';
+ }
- return directory_make_child_checked(parentDirectory, utf8path);
+ free(duplicated);
+ return directory;
}
static struct directory *