Anonymous avatar Anonymous committed 7c36daa

raise a fatal error when the interpreter is in the early initialization

Comments (0)

Files changed (1)

 #define DEV_URANDOM "/dev/urandom"
 #endif
 
+/* raise a fatal error when the interpreter is in the early initialization
+ * phase
+ * XXX: replace with something better than Py_RndHashSeed[0] == -1
+ * */
+
+#define error_string(exc, msg) do {          \
+	if (Py_RndHashSeed[0] == -1) {           \
+        Py_FatalError(msg);                  \
+    } else {                                 \
+	    PyErr_SetString(exc, msg);           \
+    }                                        \
+    } while(0)
+
+#define error_errno(exc, filename, msg) do { \
+	if (Py_RndHashSeed[0] == -1) {           \
+        Py_FatalError(msg);                  \
+    } else {                                 \
+	    PyErr_SetFromErrnoWithFilename(exc, filename); \
+    }                                        \
+    } while(0)
+
+
 #ifdef MS_WINDOWS
 
-#define win32_error(function, filename) \
-	errno = GetLastError(); \
-	PyErr_SetFromWindowsErr(errno)
+#define win32_error(function, filename) do { \
+	if (Py_RndHashSeed[0] == -1) {           \
+        Py_FatalError(function);             \
+    } else {                                 \
+	    errno = GetLastError();              \
+	    PyErr_SetFromWindowsErr(errno)       \
+    }                                        \
+    } while(0)
 
 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
               LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
 PyOS_URandom(unsigned char *buf, Py_ssize_t len)
 {
     if (len < 0) {
-    	PyErr_SetString(PyExc_ValueError,
-                        "negative argument not allowed");
+    	error_string(PyExc_ValueError,
+    			     "negative argument not allowed");
     	return -1;
     }
 
                                         hAdvAPI32,
                                         "CryptAcquireContextA");
         if (pCryptAcquireContext == NULL) {
-            PyErr_SetString(PyExc_NotImplementedError,
-                            "CryptAcquireContextA not found");
+        	error_string(PyExc_NotImplementedError,
+                        "CryptAcquireContextA not found");
             return -1;
         }
 
         pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
                                         hAdvAPI32, "CryptGenRandom");
         if (pCryptGenRandom == NULL) {
-            PyErr_SetString(PyExc_NotImplementedError,
-                                   "CryptGenRandom not found");
+        	error_string(PyExc_NotImplementedError,
+                         "CryptGenRandom not found");
             return -1;
         }
 
 	ssize_t pos, result = 0;
 
     if (len < 0) {
-    	PyErr_SetString(PyExc_ValueError,
-                        "negative argument not allowed");
+    	error_string(PyExc_ValueError,
+                     "negative argument not allowed");
     	return -1;
     }
 
 	if ((fd = open(DEV_URANDOM, O_RDONLY)) == -1) {
-		PyErr_SetFromErrnoWithFilename(PyExc_OSError, DEV_URANDOM);
+		error_errno(PyExc_OSError, DEV_URANDOM, "Can't open /dev/urandom");
 		return -1;
 	}
 
 	while (pos < len) {
 		if ((result = read(fd, buf+pos, len-pos)) == -1) {
 			close(fd);
-			PyErr_SetFromErrnoWithFilename(PyExc_OSError, DEV_URANDOM);
+			error_errno(PyExc_OSError, DEV_URANDOM, "Error reading from /dev/urandom");
 			return -1;
 		}
 		pos += result;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.