--- a/src/include/lib/sort_template.h
+++ b/src/include/lib/sort_template.h
@@ -307,6 +307,9 @@ ST_SORT(ST_ELEMENT_TYPE * data, size_t n
      int         r,
                  presorted;

+if (!data && n == 0)
+    return;
+
  loop:

