Commits

Georg Brandl committed ec97b7a

#1608818: errno can get set by every call to readdir().

Comments (0)

Files changed (2)

 Library
 -------
 
+- Issue #1608818: Fix misbehavior in os.listdir() if readdir() fails.
+
 - Issue #3125: Remove copy_reg in multiprocessing and replace it with
   ForkingPickler.register() to resolve conflict with ctypes.
 

Modules/posixmodule.c

 		return NULL;
 	}
 	for (;;) {
+		errno = 0;
 		Py_BEGIN_ALLOW_THREADS
 		ep = readdir(dirp);
 		Py_END_ALLOW_THREADS
-		if (ep == NULL)
-			break;
+		if (ep == NULL) {
+			if (errno == 0) {
+				break;
+			} else {
+				closedir(dirp);
+				Py_DECREF(d);
+				return posix_error_with_allocated_filename(name);
+			}
+		}
 		if (ep->d_name[0] == '.' &&
 		    (NAMLEN(ep) == 1 ||
 		     (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
 		}
 		Py_DECREF(v);
 	}
-	if (errno != 0 && d != NULL) {
-		/* readdir() returned NULL and set errno */
-		closedir(dirp);
-		Py_DECREF(d);
-		return posix_error_with_allocated_filename(name); 
-	}
 	closedir(dirp);
 	PyMem_Free(name);