Skip to content

Commit

Permalink
[Windows] Fixed CWE-829 issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
punesemu committed Feb 22, 2024
1 parent 97cc13c commit cc7a2fe
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 59 deletions.
42 changes: 16 additions & 26 deletions src/audio/xaudio/snd.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ void (*snd_apu_tick)(void);
void (*snd_end_frame)(void);

BYTE snd_init(void) {
uTCHAR system32[MAX_PATH];

memset(&snd, 0x00, sizeof(_snd));
memset(&xaudio2, 0x00, sizeof(xaudio2));
memset(&ds8, 0x00, sizeof(ds8));
Expand All @@ -116,33 +118,21 @@ BYTE snd_init(void) {
return (EXIT_ERROR);
}

if ((ds8.ds8 = LoadLibrary("DSOUND.DLL")) == NULL) {
log_error(uL("directsound;failed to load DSOUND.DLL"));
ds8.available = FALSE;
} else {
ds8.available = TRUE;
if (GetSystemDirectoryW(system32, MAX_PATH) != 0) {
uTCHAR path[MAX_PATH];

#if defined (__GNUC__)
#if __GNUC__ >= 8
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
#endif
if ((ds8.DirectSoundCreate8_proc =
(HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUND*,LPUNKNOWN))GetProcAddress(ds8.ds8, "DirectSoundCreate8")) == NULL) {
ds8.available = FALSE;
}
if ((ds8.DirectSoundCaptureEnumerateW_proc =
(HRESULT (WINAPI *)(LPDSENUMCALLBACKW, LPVOID))GetProcAddress(ds8.ds8, "DirectSoundCaptureEnumerateW")) == NULL) {
ds8.available = FALSE;
}
#if defined (__GNUC__)
#if __GNUC__ >= 8
#pragma GCC diagnostic pop
#endif
#endif
if (!ds8.available) {
log_error(uL("directsound;system doesn't appear to have DS8"));
usnprintf(path, usizeof(path), uL("" uPs("") "\\DSOUND.DLL"), system32);
ds8.ds8 = LoadLibraryW(path);
ds8.available = FALSE;
if (ds8.ds8 == NULL) {
log_error(uL("directsound;failed to load DSOUND.DLL"));
} else {
ds8.DirectSoundCreate8_proc = (void *)GetProcAddress(ds8.ds8, "DirectSoundCreate8");
ds8.DirectSoundCaptureEnumerateW_proc = (void *)GetProcAddress(ds8.ds8,"DirectSoundCaptureEnumerateW");
ds8.available = (ds8.DirectSoundCreate8_proc != NULL) && (ds8.DirectSoundCaptureEnumerateW_proc != NULL);
if (!ds8.available) {
log_error(uL("directsound;system doesn't appear to have DS8"));
}
}
}

Expand Down
67 changes: 34 additions & 33 deletions src/gui/windows/os_jstick.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,42 +284,43 @@ struct _js_os {

void js_os_init(BYTE first_time) {
if (first_time) {
uTCHAR system32[MAX_PATH];

memset(&js_os, 0x00, sizeof(js_os));

#if defined (__GNUC__)
#if __GNUC__ >= 8
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
#endif
if (((js_os.xinput = LoadLibrary("XInput1_4.dll")) == NULL) &&
((js_os.xinput = LoadLibrary("XInput1_3.dll")) == NULL)) {
log_error(uL("xinput;failed to load xinput dll"));
js_os.xinput_available = FALSE;
} else {
js_os.xinput_available = TRUE;
js_os.XInputGetStateEx_proc =(DWORD (WINAPI *)(DWORD, XINPUT_STATE_EX *))GetProcAddress(js_os.xinput, (LPCSTR) 100);
js_os.XInputGetState_proc = (DWORD (WINAPI *)(DWORD, XINPUT_STATE *))GetProcAddress(js_os.xinput, "XInputGetState");
js_os.XInputGetCapabilities_proc = (DWORD (WINAPI *)(DWORD, DWORD, XINPUT_CAPABILITIES *))GetProcAddress(js_os.xinput, "XInputGetCapabilities");
}
if (GetSystemDirectoryW(system32, MAX_PATH) != 0) {
uTCHAR path[MAX_PATH];

if ((js_os.di8 = LoadLibrary("DINPUT8.dll")) == NULL) {
log_error(uL("directinput;failed to load dinput8.dll"));
} else {
HRESULT (WINAPI *DirectInput8Create_proc)(HINSTANCE, DWORD, REFIID, LPVOID *, LPUNKNOWN);

DirectInput8Create_proc = (HRESULT (WINAPI *)(HINSTANCE, DWORD, REFIID, LPVOID *, LPUNKNOWN))GetProcAddress(js_os.di8, "DirectInput8Create");
DirectInput8Create_proc(GetModuleHandle(NULL),
DIRECTINPUT_VERSION,
&IID_IDirectInput8W,
(void **)&js_os.directInputInterface,
NULL);
usnprintf(path, usizeof(path), uL("" uPs("") "\\XInput1_4.dll"), system32);
js_os.xinput = LoadLibraryW(path);
if (js_os.xinput == NULL) {
usnprintf(path, usizeof(path), uL("" uPs("") "\\XInput1_3.dll"), system32);
js_os.xinput = LoadLibraryW(path);
}
if (js_os.xinput == NULL) {
log_error(uL("xinput;failed to load xinput dll"));
js_os.xinput_available = FALSE;
} else {
js_os.xinput_available = TRUE;
js_os.XInputGetStateEx_proc = (void *)GetProcAddress(js_os.xinput, (LPCSTR) 100);
js_os.XInputGetState_proc = (void *)GetProcAddress(js_os.xinput, "XInputGetState");
js_os.XInputGetCapabilities_proc = (void *)GetProcAddress(js_os.xinput, "XInputGetCapabilities");
}
usnprintf(path, usizeof(path), uL("" uPs("") "\\DINPUT8.dll"), system32);
js_os.di8 = LoadLibraryW(path);
if (js_os.di8 == NULL) {
log_error(uL("directinput;failed to load dinput8.dll"));
} else {
HRESULT (WINAPI *DirectInput8Create_proc)(HINSTANCE, DWORD, REFIID, LPVOID *, LPUNKNOWN);

DirectInput8Create_proc = (void *)GetProcAddress(js_os.di8, "DirectInput8Create");
DirectInput8Create_proc(GetModuleHandle(NULL),
DIRECTINPUT_VERSION,
&IID_IDirectInput8W,
(void **)&js_os.directInputInterface,
NULL);
}
}
#if defined (__GNUC__)
#if __GNUC__ >= 8
#pragma GCC diagnostic pop
#endif
#endif
}
}
void js_os_quit(BYTE last_time) {
Expand Down Expand Up @@ -521,7 +522,7 @@ void js_os_jdev_close(_js_device *jdev) {
if (jdev->present) {
jstick.jdd.count--;
#if defined (DEBUG)
log_warning(uL("jstick disc.;slot%d \"" uPs("") "\" (%d)\n"),
log_warning(uL("jstick disc.;slot%d \"" uPs("") "\" (%d)"),
jdev->index,
jdev->desc,
jstick.jdd.count);
Expand Down

0 comments on commit cc7a2fe

Please sign in to comment.