[llvm-branch-commits] [libc] [libc] Add Annex K qsort_s definition and tests (PR #198797)

Victor Campos via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Jun 19 09:19:33 PDT 2026


================
@@ -0,0 +1,68 @@
+//===-- Unittests for qsort_s ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#define __STDC_WANT_LIB_EXT1__ 1
+#include "QsortReentrantTest.h"
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/errno_t.h"
+#include "hdr/types/rsize_t.h"
+#include "src/stdlib/qsort_s.h"
+#include "test/UnitTest/ConstraintHandlerCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+QSORTREENTRANT_TEST(QsortS, LIBC_NAMESPACE::qsort_s, rsize_t)
+
+using LlvmLibcQsortSConstraintTest =
+    LIBC_NAMESPACE::testing::ConstraintHandlerCheckingTest;
+
+static int compare(const void *a, const void *b, void *) {
+  const int *a_int = static_cast<const int *>(a);
+  const int *b_int = static_cast<const int *>(b);
+  return *a_int - *b_int;
+}
+
+TEST_F(LlvmLibcQsortSConstraintTest, ArraySizeGreaterThanRsizeMax) {
+  int array[] = {1, 3, 2};
+  errno_t retval =
+      LIBC_NAMESPACE::qsort_s(array, RSIZE_MAX + 1, sizeof(int), compare, 0);
+  EXPECT_NE(retval, 0);
+  EXPECT_STREQ(buffer, "qsort_s: array_size cannot be greater than RSIZE_MAX");
----------------
vhscampos wrote:

Good point. Unfortunately I believe there is no specified way to differentiate among the different error conditions. Both the error message and the errno_t code are implementation-defined.

I plan to change this to a simple boolean "error detected" flag. I don't think we can do better than that.

https://github.com/llvm/llvm-project/pull/198797


More information about the llvm-branch-commits mailing list