From 006d2da96d326fbf9fe90428b9545032bed7f3cb Mon Sep 17 00:00:00 2001 From: Eric Caruso Date: Wed, 5 Jun 2013 18:52:33 -0700 Subject: [PATCH] Added warning for bad return values from user comparators Alerts users that comparators should be in the style of functions passed to e.g. C qsort (i.e. returning -1, 0, or 1) rather than STL-style boolean comparators (i.e. true for less-than). --- hphp/runtime/base/array/sort_helpers.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/hphp/runtime/base/array/sort_helpers.h b/hphp/runtime/base/array/sort_helpers.h index 83afad007..36b1cbc5f 100644 --- a/hphp/runtime/base/array/sort_helpers.h +++ b/hphp/runtime/base/array/sort_helpers.h @@ -239,6 +239,7 @@ struct ElmUCompare { typedef typename AccessorT::ElmT ElmT; AccessorT acc; const CallCtx* ctx; + ElmUCompare() : warned(false) {} bool operator()(ElmT left, ElmT right) const { Variant ret; TypedValue args[2]; @@ -260,9 +261,11 @@ struct ElmUCompare { default: /* fall through */ break; } } - // Task #1839416: Raise a warning indicating that the comparator - // returned something other than an integer, double, or numeric - // string + if (!warned) { + raise_warning("Sort comparators should return an integer, double, " + "or numeric string with value -1, 0, or 1"); + warned = true; + } if (ret.isBoolean()) { // Match the behavior of Zend PHP for comparators that return // boolean values @@ -283,6 +286,8 @@ struct ElmUCompare { } return ret.toInt64() < 0; } +private: + mutable bool warned; }; ///////////////////////////////////////////////////////////////////////////////