88
99#include "Python.h"
1010#include "osdefs.h"
11+ #include <assert.h>
1112#include <windows.h>
1213#include "importdl.h"
13- #include "malloc.h" // for alloca
14+ #include "malloc.h" /* for alloca */
1415
15- extern const char * PyWin_DLLVersionString ; // a string loaded from the DLL at startup.
16+ /* a string loaded from the DLL at startup */
17+ extern const char * PyWin_DLLVersionString ;
1618
17- FILE * PyWin_FindRegisteredModule ( const char * moduleName , struct filedescr * * ppFileDesc , char * pathBuf , int pathLen )
19+ FILE * PyWin_FindRegisteredModule (const char * moduleName ,
20+ struct filedescr * * ppFileDesc ,
21+ char * pathBuf ,
22+ int pathLen )
1823{
1924 char * moduleKey ;
2025 const char keyPrefix [] = "Software\\Python\\PythonCore\\" ;
2126 const char keySuffix [] = "\\Modules\\" ;
2227#ifdef _DEBUG
23- // In debugging builds, we _must_ have the debug version registered.
28+ /* In debugging builds, we _must_ have the debug version
29+ * registered.
30+ */
2431 const char debugString [] = "\\Debug" ;
2532#else
2633 const char debugString [] = "" ;
@@ -31,28 +38,40 @@ FILE *PyWin_FindRegisteredModule( const char *moduleName, struct filedescr **ppF
3138 int modNameSize ;
3239 long regStat ;
3340
34- // Calculate the size for the sprintf buffer.
35- // Get the size of the chars only, plus 1 NULL.
36- size_t bufSize = sizeof (keyPrefix )- 1 + strlen (PyWin_DLLVersionString ) + sizeof (keySuffix ) + strlen (moduleName ) + sizeof (debugString ) - 1 ;
37- // alloca == no free required, but memory only local to fn, also no heap fragmentation!
41+ /* Calculate the size for the sprintf buffer.
42+ * Get the size of the chars only, plus 1 NULL.
43+ */
44+ size_t bufSize = sizeof (keyPrefix )- 1 +
45+ strlen (PyWin_DLLVersionString ) +
46+ sizeof (keySuffix ) +
47+ strlen (moduleName ) +
48+ sizeof (debugString ) - 1 ;
49+ /* alloca == no free required, but memory only local to fn,
50+ * also no heap fragmentation!
51+ */
3852 moduleKey = alloca (bufSize );
39- sprintf (moduleKey , "Software\\Python\\PythonCore\\%s\\Modules\\%s%s" , PyWin_DLLVersionString , moduleName , debugString );
53+ sprintf (moduleKey ,
54+ "Software\\Python\\PythonCore\\%s\\Modules\\%s%s" ,
55+ PyWin_DLLVersionString , moduleName , debugString );
4056
4157 modNameSize = pathLen ;
4258 regStat = RegQueryValue (keyBase , moduleKey , pathBuf , & modNameSize );
43- if (regStat != ERROR_SUCCESS )
59+ if (regStat != ERROR_SUCCESS )
4460 return NULL ;
45- // use the file extension to locate the type entry.
61+ /* use the file extension to locate the type entry. */
4662 for (fdp = _PyImport_Filetab ; fdp -> suffix != NULL ; fdp ++ ) {
4763 size_t extLen = strlen (fdp -> suffix );
48- if (modNameSize > extLen && strnicmp (pathBuf + (modNameSize - extLen - 1 ),fdp -> suffix ,extLen )== 0 )
64+ assert (modNameSize >= 0 ); /* else cast to size_t is wrong */
65+ if ((size_t )modNameSize > extLen &&
66+ strnicmp (pathBuf + ((size_t )modNameSize - extLen - 1 ),
67+ fdp -> suffix ,
68+ extLen ) == 0 )
4969 break ;
5070 }
51- if (fdp -> suffix == NULL )
71+ if (fdp -> suffix == NULL )
5272 return NULL ;
5373 fp = fopen (pathBuf , fdp -> mode );
5474 if (fp != NULL )
5575 * ppFileDesc = fdp ;
5676 return fp ;
5777}
58-
0 commit comments