From bcaca54fcc2e482210e4060bb06c8394ec78c0ae Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Tue, 28 Nov 2017 18:49:52 +1100 Subject: [PATCH] 4829. [bug] isc_heap_delete did not zero the index value when the heap was created with a callback to do that. [RT #46709] (cherry picked from commit 65a483106e45704e19781bfe4f4634db4f77562e) --- CHANGES | 4 ++ lib/isc/heap.c | 2 + lib/isc/tests/Atffile | 1 + lib/isc/tests/Makefile.in | 22 ++++++----- lib/isc/tests/heap_test.c | 83 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 9 deletions(-) create mode 100644 lib/isc/tests/heap_test.c diff --git a/CHANGES b/CHANGES index 575f604266..0cecf3d37e 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +4829. [bug] isc_heap_delete did not zero the index value when + the heap was created with a callback to do that. + [RT #46709] + 4827. [misc] Add a precommit check script util/checklibs.sh [RT #46215] diff --git a/lib/isc/heap.c b/lib/isc/heap.c index 13798a5de7..300bf630b0 100644 --- a/lib/isc/heap.c +++ b/lib/isc/heap.c @@ -226,6 +226,8 @@ isc_heap_delete(isc_heap_t *heap, unsigned int idx) { REQUIRE(idx >= 1 && idx <= heap->last); heap_check(heap); + if (heap->index != NULL) + (heap->index)(heap->array[idx], 0); if (idx == heap->last) { heap->array[heap->last] = NULL; heap->last--; diff --git a/lib/isc/tests/Atffile b/lib/isc/tests/Atffile index f7b6d9a7b4..5a074d9564 100644 --- a/lib/isc/tests/Atffile +++ b/lib/isc/tests/Atffile @@ -6,6 +6,7 @@ tp: aes_test tp: counter_test tp: errno_test tp: hash_test +tp: heap_test tp: inet_ntop_test tp: lex_test tp: mem_test diff --git a/lib/isc/tests/Makefile.in b/lib/isc/tests/Makefile.in index 631effc038..ad279ae020 100644 --- a/lib/isc/tests/Makefile.in +++ b/lib/isc/tests/Makefile.in @@ -34,19 +34,19 @@ LIBS = @LIBS@ @ATFLIBS@ OBJS = isctest.@O@ SRCS = isctest.c aes_test.c counter_test.c errno_test.c \ - hash_test.c inet_ntop_test.c lex_test.c mem_test.c \ - netaddr_test.c parse_test.c pool_test.c print_test.c \ - queue_test.c radix_test.c regex_test.c safe_test.c \ - sockaddr_test.c socket_test.c socket_test.c \ + hash_test.c heap_test.c inet_ntop_test.c lex_test.c \ + mem_test.c netaddr_test.c parse_test.c pool_test.c \ + print_test.c queue_test.c radix_test.c regex_test.c \ + safe_test.c sockaddr_test.c socket_test.c socket_test.c \ symtab_test.c task_test.c taskpool_test.c time_test.c SUBDIRS = TARGETS = aes_test@EXEEXT@ counter_test@EXEEXT@ errno_test@EXEEXT@ \ - hash_test@EXEEXT@ inet_ntop_test@EXEEXT@ lex_test@EXEEXT@ \ - mem_test@EXEEXT@ netaddr_test@EXEEXT@ parse_test@EXEEXT@ \ - pool_test@EXEEXT@ print_test@EXEEXT@ queue_test@EXEEXT@ \ - radix_test@EXEEXT@ regex_test@EXEEXT@ safe_test@EXEEXT@ \ - sockaddr_test@EXEEXT@ socket_test@EXEEXT@ \ + hash_test@EXEEXT@ heap_test@EXEEXT@ inet_ntop_test@EXEEXT@ \ + lex_test@EXEEXT@ mem_test@EXEEXT@ netaddr_test@EXEEXT@ \ + parse_test@EXEEXT@ pool_test@EXEEXT@ print_test@EXEEXT@ \ + queue_test@EXEEXT@ radix_test@EXEEXT@ regex_test@EXEEXT@ \ + safe_test@EXEEXT@ sockaddr_test@EXEEXT@ socket_test@EXEEXT@ \ socket_test@EXEEXT@ symtab_test@EXEEXT@ task_test@EXEEXT@ \ taskpool_test@EXEEXT@ time_test@EXEEXT@ @@ -68,6 +68,10 @@ hash_test@EXEEXT@: hash_test.@O@ ${ISCDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ hash_test.@O@ ${ISCLIBS} ${LIBS} +heap_test@EXEEXT@: heap_test.@O@ ${ISCDEPLIBS} + ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ + heap_test.@O@ ${ISCLIBS} ${LIBS} + inet_ntop_test.c.@O@: ${top_srcdir}/lib/isc/ntop_test.c inet_ntop_test@EXEEXT@: inet_ntop_test.@O@ ${ISCDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ diff --git a/lib/isc/tests/heap_test.c b/lib/isc/tests/heap_test.c new file mode 100644 index 0000000000..03c08e34be --- /dev/null +++ b/lib/isc/tests/heap_test.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2011-2017 Internet Systems Consortium, Inc. ("ISC") + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +/* ! \file */ + +#include + +#include + +#include +#include + +#include +#include + +#include + +struct e { + unsigned int value; + unsigned int index; +}; + +static isc_boolean_t +compare(void *p1, void *p2) { + struct e *e1 = p1; + struct e *e2 = p2; + + return (ISC_TF(e1->value < e2->value)); +} + +static void +idx(void *p, unsigned int i) { + struct e *e = p; + + e->index = i; +} + +ATF_TC(isc_heap_delete); +ATF_TC_HEAD(isc_heap_delete, tc) { + atf_tc_set_md_var(tc, "descr", "test isc_heap_delete"); +} +ATF_TC_BODY(isc_heap_delete, tc) { + isc_mem_t *mctx = NULL; + isc_heap_t *heap = NULL; + isc_result_t result; + struct e e1 = { 100, 0 }; + + UNUSED(tc); + + result = isc_mem_create(0, 0, &mctx); + ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + + result = isc_heap_create(mctx, compare, idx, 0, &heap); + ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + ATF_REQUIRE(heap != NULL); + + isc_heap_insert(heap, &e1); + ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + ATF_REQUIRE_EQ(e1.index, 1); + + isc_heap_delete(heap, e1.index); + ATF_CHECK_EQ(e1.index, 0); + + isc_heap_destroy(&heap); + ATF_REQUIRE_EQ(heap, NULL); + + isc_mem_detach(&mctx); + ATF_REQUIRE_EQ(mctx, NULL); +} + +/* + * Main + */ +ATF_TP_ADD_TCS(tp) { + ATF_TP_ADD_TC(tp, isc_heap_delete); + + return (atf_no_error()); +}