Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
godot
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
community
godot
Commits
9d27bd3c
Commit
9d27bd3c
authored
Jan 09, 2018
by
poke1024
Committed by
Bernhard Liebl
Aug 04, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix SortArray crashing with bad comparison functions
parent
4e4702e3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
5 deletions
+31
-5
array.cpp
core/array.cpp
+1
-1
sort.h
core/sort.h
+30
-4
No files found.
core/array.cpp
View file @
9d27bd3c
...
...
@@ -259,7 +259,7 @@ Array &Array::sort_custom(Object *p_obj, const StringName &p_function) {
ERR_FAIL_NULL_V
(
p_obj
,
*
this
);
SortArray
<
Variant
,
_ArrayVariantSortCustom
>
avs
;
SortArray
<
Variant
,
_ArrayVariantSortCustom
,
true
>
avs
;
avs
.
compare
.
obj
=
p_obj
;
avs
.
compare
.
func
=
p_function
;
avs
.
sort
(
_p
->
array
.
ptrw
(),
_p
->
array
.
size
());
...
...
core/sort.h
View file @
9d27bd3c
...
...
@@ -36,13 +36,25 @@
@author ,,, <red@lunatea>
*/
#define ERR_BAD_COMPARE(cond) \
if (unlikely(cond)) { \
ERR_PRINT("bad comparison function; sorting will be broken"); \
break; \
}
template
<
class
T
>
struct
_DefaultComparator
{
inline
bool
operator
()(
const
T
&
a
,
const
T
&
b
)
const
{
return
(
a
<
b
);
}
_FORCE_INLINE_
bool
operator
()(
const
T
&
a
,
const
T
&
b
)
const
{
return
(
a
<
b
);
}
};
template
<
class
T
,
class
Comparator
=
_DefaultComparator
<
T
>
>
#ifdef DEBUG_ENABLED
#define SORT_ARRAY_VALIDATE_ENABLED true
#else
#define SORT_ARRAY_VALIDATE_ENABLED false
#endif
template
<
class
T
,
class
Comparator
=
_DefaultComparator
<
T
>
,
bool
Validate
=
SORT_ARRAY_VALIDATE_ENABLED
>
class
SortArray
{
enum
{
...
...
@@ -164,12 +176,23 @@ public:
inline
int
partitioner
(
int
p_first
,
int
p_last
,
T
p_pivot
,
T
*
p_array
)
const
{
const
int
unmodified_first
=
p_first
;
const
int
unmodified_last
=
p_last
;
while
(
true
)
{
while
(
compare
(
p_array
[
p_first
],
p_pivot
))
while
(
compare
(
p_array
[
p_first
],
p_pivot
))
{
if
(
Validate
)
{
ERR_BAD_COMPARE
(
p_first
==
unmodified_last
-
1
)
}
p_first
++
;
}
p_last
--
;
while
(
compare
(
p_pivot
,
p_array
[
p_last
]))
while
(
compare
(
p_pivot
,
p_array
[
p_last
]))
{
if
(
Validate
)
{
ERR_BAD_COMPARE
(
p_last
==
unmodified_first
)
}
p_last
--
;
}
if
(
!
(
p_first
<
p_last
))
return
p_first
;
...
...
@@ -238,6 +261,9 @@ public:
int
next
=
p_last
-
1
;
while
(
compare
(
p_value
,
p_array
[
next
]))
{
if
(
Validate
)
{
ERR_BAD_COMPARE
(
next
==
0
)
}
p_array
[
p_last
]
=
p_array
[
next
];
p_last
=
next
;
next
--
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment