diff --git a/src/dir_scan.c b/src/dir_scan.c
index 9483d016d906fe8567c8a969ff8c89b3f7c397a8..442c4a278de0a6be117cc773e9967bb27ae16228 100644
--- a/src/dir_scan.c
+++ b/src/dir_scan.c
@@ -312,6 +312,7 @@ void dir_scan_init(const char *path) {
   dir_setlasterr(NULL);
   dir_seterr(NULL);
   dir_process = process;
-  buf_dir = malloc(dir_memsize(""));
+  if (!buf_dir)
+    buf_dir = malloc(dir_memsize(""));
   pstate = ST_CALC;
 }