changeset 9206:b04689148abf draft

(svn r13072) -Fix (r12995): possible out-of-bounds access
author smatz <smatz@openttd.org>
date Tue, 13 May 2008 18:39:15 +0000
parents a8b1f6fa5f79
children 839a532bf0f6
files src/engine.cpp
diffstat 1 files changed, 5 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -120,7 +120,11 @@
  */
 void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
 {
-	qsort(&((*el)[0]), el->size(), sizeof(EngineID), compare);
+	size_t size = el->size();
+	/* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
+	 * generally, do not sort if there are less than 2 items */
+	if (size < 2) return;
+	qsort(&(el->at(0)), size, sizeof(EngineID), compare);
 }
 
 /** Sort selected range of items (on indices @ <begin, begin+num_items-1>)