From b1ac6f9a80b9dbae52382f852dc52e517cb13257 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 31 Aug 2026 21:54:36 -0700 Subject: [PATCH] Fix unsafe casts in test_sorting.sp. --- core/logic/smn_sorting.cpp | 1 + plugins/include/sorting.inc | 29 ++++++++++++++++++++++++++ plugins/testsuite/mock/test_sorting.sp | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/core/logic/smn_sorting.cpp b/core/logic/smn_sorting.cpp index 0ad99fd0..948f4c99 100644 --- a/core/logic/smn_sorting.cpp +++ b/core/logic/smn_sorting.cpp @@ -696,6 +696,7 @@ REGISTER_NATIVES(sortNatives) {"SortStrings", sm_SortStrings}, {"SortCustom1D", sm_SortCustom1D}, {"SortCustom2D", sm_SortCustom2D}, + {"SortCustom2DStrings", sm_SortCustom2D}, {"SortADTArray", sm_SortADTArray}, {"SortADTArrayCustom", sm_SortADTArrayCustom}, diff --git a/plugins/include/sorting.inc b/plugins/include/sorting.inc index ee777d86..d80c1e81 100644 --- a/plugins/include/sorting.inc +++ b/plugins/include/sorting.inc @@ -119,11 +119,15 @@ native void SortCustom1D(int[] array, int array_size, SortFunc1D sortfunc, Handl * 0 if first is equal to second * 1 if first should go after second */ +#if defined __sourcepawn2 +typedef SortFunc2D = function int (int[] elem1, int[] elem2, const int[][] array, Handle hndl); +#else typeset SortFunc2D { function int (int[] elem1, int[] elem2, const int[][] array, Handle hndl); function int (char[] elem1, char[] elem2, const char[][] array, Handle hndl); }; +#endif /** * Sorts a custom 2D array. You must pass in a comparison function. @@ -135,6 +139,31 @@ typeset SortFunc2D */ native void SortCustom2D(any[][] array, int array_size, SortFunc2D sortfunc, Handle hndl=INVALID_HANDLE); +#if defined __sourcepawn2 +/** + * Sort comparison function for 2D char-array elements (sub-strings). + * + * @param elem1 First string to compare. + * @param elem2 Second string to compare. + * @param array Array that is being sorted (order is undefined). + * @param hndl Handle optionally passed in while sorting. + * @return -1 if first should go before second + * 0 if first is equal to second + * 1 if first should go after second + */ +typedef SortFunc2DStrings = function int (char[] elem1, char[] elem2, const char[][] array, Handle hndl); + +/** + * Sorts a custom 2D char-array. You must pass in a comparison function. + * + * @param array Array to sort. + * @param array_size Size of the major array to sort (first index, outermost). + * @param sortfunc Sort comparison function to use. + * @param hndl Optional Handle to pass through the comparison calls. + */ +native void SortCustom2DStrings(char[][] array, int array_size, SortFunc2DStrings sortfunc, Handle hndl=INVALID_HANDLE); +#endif + /** * Sort an ADT Array. Specify the type as Integer, Float, or String. * diff --git a/plugins/testsuite/mock/test_sorting.sp b/plugins/testsuite/mock/test_sorting.sp index a875c23c..bcdd0e02 100644 --- a/plugins/testsuite/mock/test_sorting.sp +++ b/plugins/testsuite/mock/test_sorting.sp @@ -163,7 +163,7 @@ void Test_Sort2D() { "sidluke", "sniperbeamer" }; - SortCustom2D(view_as(strarray), sizeof(strarray), Custom2DSortString); + SortCustom2DStrings(strarray, sizeof(strarray), Custom2DSortString); AssertStrArrayEq("SortCustom2D char[][] Ascending", strarray, expected_ascending, sizeof(strarray)); }