]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add optional code to catch out of order terminal inputs
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 7 Jun 2022 16:46:01 +0000 (12:46 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 8 Jun 2022 16:08:10 +0000 (12:08 -0400)
src/lib/util/sbuff.c

index e62714ebcef5267efed9a837e9f3afaa679074cd..6a5f97c91ea6e529082747f38f847b374b9771de 100644 (file)
@@ -573,6 +573,17 @@ static inline int8_t terminal_cmp(void const *one, void const *two)
        return 0;
 }
 
+#if 0
+static void fr_sbuff_terminal_debug_tmp(fr_sbuff_term_elem_t const *elem[], size_t len)
+{
+       size_t i;
+
+       FR_FAULT_LOG("Terminal count %zu", len);
+
+       for (i = 0; i < len; i++) FR_FAULT_LOG("\t\"%s\" (%zu)", elem[i] ? elem[i]->str : "NULL", elem[i] ? elem[i]->len : 0);
+}
+#endif
+
 /** Merge two sets of terminal strings
  *
  * @param[in] ctx      to allocate the new terminal array in.
@@ -589,6 +600,28 @@ fr_sbuff_term_t *fr_sbuff_terminals_amerge(TALLOC_CTX *ctx, fr_sbuff_term_t cons
        for (i = 0; i < a->len; i++) tmp[i] = &a->elem[i];
        for (j = 0; j < b->len; i++, j++) tmp[i] = &b->elem[j];
 
+       /*
+        *      Check all inputs are pre-sorted.  It doesn't break this
+        *      function, but it's useful in case the terminal arrays
+        *      are used elsewhere without merging.
+        */
+#if !defined(NDEBUG) && defined(SBUFF_CHECK_TERM_ORDER)
+       {
+               size_t l;
+
+               if (a->len) {
+                       fr_quick_sort((void const **)tmp, 0, a->len - 1, terminal_cmp);
+                       for (l = 0; l < a->len; l++) fr_assert(tmp[l] == &a->elem[l]);
+               }
+
+               if (b->len) {
+                       fr_quick_sort((void const **)tmp, a->len, b->len - 1, terminal_cmp);
+                       for (l = 0; l < b->len; l++) fr_assert(tmp[l + a->len] == &a->elem[l + a->len]);
+               }
+       }
+#endif
+
+
        if (likely(i > 1)) {
                fr_quick_sort((void const **)tmp, 0, i - 1, terminal_cmp);