diff options
author | Max Kellermann <max@duempel.org> | 2008-10-09 15:48:07 +0200 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2008-10-11 19:21:50 -0700 |
commit | f2fd7be1d7b80da32611c57360e71fb777b95c7d (patch) | |
tree | f2a1693a44e6d1a491c88b29d066383fe5f2a0db /src | |
parent | 50f1074c1b0b156e484401c6455e881cd830c2f7 (diff) | |
download | mpd-f2fd7be1d7b80da32611c57360e71fb777b95c7d.tar.gz mpd-f2fd7be1d7b80da32611c57360e71fb777b95c7d.tar.xz mpd-f2fd7be1d7b80da32611c57360e71fb777b95c7d.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 '')
-rw-r--r-- | src/update.c | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/src/update.c b/src/update.c index dd48bfe78..1415852e0 100644 --- a/src/update.c +++ b/src/update.c @@ -340,21 +340,23 @@ 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 (1) { + if ((slash = strchr(slash, '/'))) + *slash = 0; - if (strlen(parent) == 0) - parentDirectory = db_get_root(); - else - parentDirectory = addDirectoryPathToDB(parent); + directory = directory_make_child_checked(directory, duplicated); + if (!directory || !slash) + break; - if (!parentDirectory) - return NULL; + *slash++ = '/'; + } - return directory_make_child_checked(parentDirectory, utf8path); + free(duplicated); + return directory; } static struct directory * addParentPathToDB(const char *utf8path) |