shithub: vdir

Download patch

ref: 8ffa802e29ca42eb54cc4b10f8c756ba6275e801
parent: 663d1a8c4432f3cef70466c04699fb6ebaafbc22
author: phil9 <telephil9@gmail.com>
date: Mon Sep 12 00:26:49 EDT 2022

fix crash caused by dirreadall() failing (thanks igor)

--- a/vdir.c
+++ b/vdir.c
@@ -134,7 +134,12 @@
 	if(dirs!=nil)
 		free(dirs);
 	ndirs = dirreadall(fd, &dirs);
-	qsort(dirs, ndirs, sizeof *dirs, (int(*)(void*,void*))dircmp);
+	if(ndirs > 0)
+		qsort(dirs, ndirs, sizeof *dirs, (int(*)(void*,void*))dircmp);
+	else{
+		ndirs = 0;
+		showerrstr("Unable to read directory");
+	}
 	offset = 0;
 	close(fd);
 	m = 1;
@@ -142,7 +147,7 @@
 		if(dirs[i].length>m)
 			m=dirs[i].length;
 	}
-	sizew = 1+1+log(m)/log(10);
+	sizew = m == 0 ? 3 : 1+1+log(m)/log(10);
 }
 
 void