@@ -2026,22 +2026,25 @@ WEAK bool D3D12LoadDXC(void *uc) {
20262026 }
20272027
20282028 // Build full path: <exe_dir>\dxcompiler.dll
2029- char path[512 ];
2030- unsigned long n = GetModuleFileNameA (nullptr , path, (unsigned long )(sizeof (path) - 20 ));
2031- if (n > 0 ) {
2032- // Trim to the directory portion (last backslash).
2033- char *last_sep = path;
2034- for (char *p = path; *p; p++) {
2035- if (*p == ' \\ ' || *p == ' /' ) {
2036- last_sep = p;
2037- }
2029+ constexpr const char dll_name[] = " dxcompiler.dll" ;
2030+ char path[MAX_PATH];
2031+ static_assert (MAX_PATH > sizeof (dll_name), " path buffer too small" );
2032+
2033+ DWORD n = GetModuleFileNameA (nullptr , path, sizeof (path));
2034+ if (n > 0 && n < sizeof (path)) {
2035+ // Trim to the directory portion (last separator).
2036+ char *last_sep = strrchr (path, ' \\ ' );
2037+ if (!last_sep) {
2038+ last_sep = strrchr (path, ' /' );
20382039 }
2039- char *dst = last_sep + 1 ;
2040- for (const char *src = " dxcompiler.dll" ; *src;) {
2041- *dst++ = *src++;
2040+ if (last_sep) {
2041+ // Verify the DLL name fits after the separator.
2042+ size_t dir_len = (size_t )(last_sep + 1 - path);
2043+ if (dir_len + sizeof (dll_name) <= sizeof (path)) {
2044+ memcpy (last_sep + 1 , dll_name, sizeof (dll_name));
2045+ lib_dxcompiler = LoadLibraryExA (path, nullptr , kLoadWithAlteredSearchPath );
2046+ }
20422047 }
2043- *dst = ' \0 ' ;
2044- lib_dxcompiler = LoadLibraryExA (path, nullptr , kLoadWithAlteredSearchPath );
20452048 if (lib_dxcompiler) {
20462049 TRACEPRINT (" D3D12Compute: Loaded DXC from: " << path << " \n " );
20472050 }
0 commit comments