From 3f4f6dba44a1aa3f11d8deaf487e80cbe3eeaf2c Mon Sep 17 00:00:00 2001 From: yellows8 Date: Tue, 17 Nov 2020 16:53:04 -0500 Subject: [PATCH] hid: Handle NULL with hidGetNpadStates*() total_out. Use const for hidSetSupportedNpadIdType(). --- nx/include/switch/services/hid.h | 2 +- nx/source/services/hid.c | 55 +++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/nx/include/switch/services/hid.h b/nx/include/switch/services/hid.h index 99ae5db4..530cfdb4 100644 --- a/nx/include/switch/services/hid.h +++ b/nx/include/switch/services/hid.h @@ -934,7 +934,7 @@ Result hidGetSupportedNpadStyleSet(u32 *style_set); /// This is automatically called with CONTROLLER_PLAYER_{1-8} and CONTROLLER_HANDHELD in \ref hidInitialize. /// count must be <=10. Each entry in buf must be CONTROLLER_PLAYER_{1-8} or CONTROLLER_HANDHELD. -Result hidSetSupportedNpadIdType(HidControllerID *buf, size_t count); +Result hidSetSupportedNpadIdType(const HidControllerID *buf, size_t count); /// Gets an event with the specified autoclear for the input controller. /// The user *must* close the event when finished with it / before the app exits. diff --git a/nx/source/services/hid.c b/nx/source/services/hid.c index 35a3ce96..0f7f3b76 100644 --- a/nx/source/services/hid.c +++ b/nx/source/services/hid.c @@ -582,8 +582,10 @@ void hidGetNpadStatesGc(u32 id, HidNpadGcState *states, size_t count, size_t *to HidNpadStateEntry tmp_entries[17]={0}; HidNpadGcTriggerState tmp_entries_trigger[17]={0}; + if (total_out) *total_out = 0; if (count > 17) count = 17; - Result rc = _hidGetNpadStates(id, 0, tmp_entries, count, total_out); + size_t tmp_out=0; + Result rc = _hidGetNpadStates(id, 0, tmp_entries, count, &tmp_out); if (R_FAILED(rc)) diagAbortWithResult(rc); rc = _hidVerifyNpadIdType(id); @@ -593,14 +595,15 @@ void hidGetNpadStatesGc(u32 id, HidNpadGcState *states, size_t count, size_t *to if (npad == NULL) diagAbortWithResult(MAKERESULT(Module_Libnx, LibnxError_NotInitialized)); - size_t tmp_out=0; - rc = _hidGetStates(&npad->npad_gc_trigger_header, npad->npad_gc_trigger_state, tmp_entries_trigger, sizeof(HidNpadGcTriggerState), count, &tmp_out); + size_t tmp_out2=0; + rc = _hidGetStates(&npad->npad_gc_trigger_header, npad->npad_gc_trigger_state, tmp_entries_trigger, sizeof(HidNpadGcTriggerState), count, &tmp_out2); if (R_FAILED(rc)) diagAbortWithResult(rc); - if (tmp_out < *total_out) *total_out = tmp_out; + if (tmp_out2 < tmp_out) tmp_out = tmp_out2; + if (total_out) *total_out = tmp_out; - memset(states, 0, sizeof(HidNpadGcState) * (*total_out)); + memset(states, 0, sizeof(HidNpadGcState) * tmp_out); - for (size_t i=0; i<*total_out; i++) { + for (size_t i=0; i 17) count = 17; - Result rc = _hidGetNpadStates(id, 0, tmp_entries, count, total_out); + size_t tmp_out=0; + Result rc = _hidGetNpadStates(id, 0, tmp_entries, count, &tmp_out); if (R_FAILED(rc)) diagAbortWithResult(rc); - memset(states, 0, sizeof(HidNpadLarkState) * (*total_out)); + memset(states, 0, sizeof(HidNpadLarkState) * tmp_out); HidNpad *npad = _hidNpadSharedmemGetInternalState(id); if (npad == NULL) @@ -638,7 +643,9 @@ void hidGetNpadStatesLark(u32 id, HidNpadLarkState *states, size_t count, size_t u32 unk = atomic_load_explicit(&npad->unk_x43E0, memory_order_acquire); if (!(unk>=1 && unk<=4)) unk = 0; - for (size_t i=0; i<*total_out; i++) { + if (total_out) *total_out = tmp_out; + + for (size_t i=0; i 17) count = 17; - Result rc = _hidGetNpadStates(id, 1, tmp_entries, count, total_out); + size_t tmp_out=0; + Result rc = _hidGetNpadStates(id, 1, tmp_entries, count, &tmp_out); if (R_FAILED(rc)) diagAbortWithResult(rc); - memset(states, 0, sizeof(HidNpadHandheldLarkState) * (*total_out)); + memset(states, 0, sizeof(HidNpadHandheldLarkState) * tmp_out); HidNpad *npad = _hidNpadSharedmemGetInternalState(id); if (npad == NULL) @@ -671,7 +680,9 @@ void hidGetNpadStatesHandheldLark(u32 id, HidNpadHandheldLarkState *states, size u32 unk1 = atomic_load_explicit(&npad->unk_x43E4, memory_order_acquire); if (!(unk1>=1 && unk1<=4)) unk1 = 0; - for (size_t i=0; i<*total_out; i++) { + if (total_out) *total_out = tmp_out; + + for (size_t i=0; i 17) count = 17; - Result rc = _hidGetNpadStates(id, 0, tmp_entries, count, total_out); + size_t tmp_out=0; + Result rc = _hidGetNpadStates(id, 0, tmp_entries, count, &tmp_out); if (R_FAILED(rc)) diagAbortWithResult(rc); - memset(states, 0, sizeof(HidNpadLuciaState) * (*total_out)); + memset(states, 0, sizeof(HidNpadLuciaState) * tmp_out); HidNpad *npad = _hidNpadSharedmemGetInternalState(id); if (npad == NULL) @@ -701,7 +714,9 @@ void hidGetNpadStatesLucia(u32 id, HidNpadLuciaState *states, size_t count, size u32 unk = atomic_load_explicit(&npad->unk_x43E8, memory_order_acquire); if (!(unk>=1 && unk<=3)) unk = 0; - for (size_t i=0; i<*total_out; i++) { + if (total_out) *total_out = tmp_out; + + for (size_t i=0; i