From 8d2f4b993f29bb5c224142a7ef2954f4fef48292 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Sun, 23 Sep 2018 15:11:41 -0500 Subject: [PATCH] Const-correct sk_find and sk_delete_ptr. Change-Id: I7ddc2c4827602ddac2a4aec5f9ccfa21d6c0bc40 Reviewed-on: https://boringssl-review.googlesource.com/32112 Commit-Queue: David Benjamin CQ-Verified: CQ bot account: commit-bot@chromium.org Reviewed-by: Adam Langley --- crypto/stack/stack.c | 8 ++++---- crypto/stack/stack_test.cc | 2 +- include/openssl/stack.h | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/crypto/stack/stack.c b/crypto/stack/stack.c index 7aa32186..6a23722e 100644 --- a/crypto/stack/stack.c +++ b/crypto/stack/stack.c @@ -209,7 +209,7 @@ void *sk_delete(_STACK *sk, size_t where) { return ret; } -void *sk_delete_ptr(_STACK *sk, void *p) { +void *sk_delete_ptr(_STACK *sk, const void *p) { if (sk == NULL) { return NULL; } @@ -223,7 +223,7 @@ void *sk_delete_ptr(_STACK *sk, void *p) { return NULL; } -int sk_find(const _STACK *sk, size_t *out_index, void *p) { +int sk_find(const _STACK *sk, size_t *out_index, const void *p) { if (sk == NULL) { return 0; } @@ -247,7 +247,7 @@ int sk_find(const _STACK *sk, size_t *out_index, void *p) { if (!sk_is_sorted(sk)) { for (size_t i = 0; i < sk->num; i++) { - if (sk->comp((const void **)&p, (const void **)&sk->data[i]) == 0) { + if (sk->comp(&p, (const void **)&sk->data[i]) == 0) { if (out_index) { *out_index = i; } @@ -270,7 +270,7 @@ int sk_find(const _STACK *sk, size_t *out_index, void *p) { size_t idx = ((void **)r) - sk->data; // This function always returns the first result. while (idx > 0 && - sk->comp((const void **)&p, (const void **)&sk->data[idx - 1]) == 0) { + sk->comp(&p, (const void **)&sk->data[idx - 1]) == 0) { idx--; } if (out_index) { diff --git a/crypto/stack/stack_test.cc b/crypto/stack/stack_test.cc index 6eb99486..58b41925 100644 --- a/crypto/stack/stack_test.cc +++ b/crypto/stack/stack_test.cc @@ -324,7 +324,7 @@ TEST(StackTest, FindFirst) { ASSERT_TRUE(bssl::PushToStack(sk.get(), std::move(value))); } - TEST_INT *two = sk_TEST_INT_value(sk.get(), 1); + const TEST_INT *two = sk_TEST_INT_value(sk.get(), 1); // Pointer-based equality. size_t index; ASSERT_TRUE(sk_TEST_INT_find(sk.get(), &index, two)); diff --git a/include/openssl/stack.h b/include/openssl/stack.h index 15b6adf7..74315769 100644 --- a/include/openssl/stack.h +++ b/include/openssl/stack.h @@ -160,7 +160,7 @@ OPENSSL_EXPORT void *sk_delete(_STACK *sk, size_t where); // sk_delete_ptr removes, at most, one instance of |p| from the stack based on // pointer equality. If an instance of |p| is found then |p| is returned, // otherwise it returns NULL. -OPENSSL_EXPORT void *sk_delete_ptr(_STACK *sk, void *p); +OPENSSL_EXPORT void *sk_delete_ptr(_STACK *sk, const void *p); // sk_find returns the first value in the stack equal to |p|. If a comparison // function has been set on the stack, equality is defined by it, otherwise @@ -173,7 +173,7 @@ OPENSSL_EXPORT void *sk_delete_ptr(_STACK *sk, void *p); // Note this differs from OpenSSL. The type signature is slightly different, and // OpenSSL's sk_find will implicitly sort |sk| if it has a comparison function // defined. -OPENSSL_EXPORT int sk_find(const _STACK *sk, size_t *out_index, void *p); +OPENSSL_EXPORT int sk_find(const _STACK *sk, size_t *out_index, const void *p); // sk_shift removes and returns the first element in the stack, or returns NULL // if the stack is empty. @@ -302,13 +302,13 @@ BSSL_NAMESPACE_END } \ \ static inline OPENSSL_UNUSED ptrtype sk_##name##_delete_ptr( \ - STACK_OF(name) *sk, ptrtype p) { \ - return (ptrtype)sk_delete_ptr((_STACK *)sk, (void *)p); \ + STACK_OF(name) *sk, constptrtype p) { \ + return (ptrtype)sk_delete_ptr((_STACK *)sk, (const void *)p); \ } \ \ static inline OPENSSL_UNUSED int sk_##name##_find( \ - const STACK_OF(name) *sk, size_t *out_index, ptrtype p) { \ - return sk_find((const _STACK *)sk, out_index, (void *)p); \ + const STACK_OF(name) *sk, size_t *out_index, constptrtype p) { \ + return sk_find((const _STACK *)sk, out_index, (const void *)p); \ } \ \ static inline OPENSSL_UNUSED ptrtype sk_##name##_shift(STACK_OF(name) *sk) { \