OPENSSL_SK_NEW(3)
π¦ NAME π¦
okay so this is the OPENSSL_SK_NEW(3) page from the Library Functions Manual, and it's about a whole squad of functions: sk_new_null, sk_new, sk_set_cmp_func, sk_dup, sk_free, sk_pop_free, sk_num, sk_value, sk_find, sk_sort, sk_is_sorted, sk_push, sk_unshift, sk_insert, sk_set, sk_pop, sk_shift, sk_delete, sk_delete_ptr, and sk_zero.
and what are they for? variable-sized arrays of void pointers, called OpenSSL stacks. π
β¨ SYNOPSIS β¨
#include <openssl/stack.h>
_STACK *
sk_new_null(void);
_STACK *
sk_new(int (*compfunc)(const void *, const void *));
old_function_pointer
sk_set_cmp_func(_STACK *stack,
int (*compfunc)(const void *, const void *));
_STACK *
sk_dup(_STACK *stack);
void
sk_free(_STACK *stack);
void
sk_pop_free(_STACK *stack, void (*freefunc)(void *));
int
sk_num(const _STACK *stack);
void *
sk_value(const _STACK *stack, int index);
int
sk_find(_STACK *stack, void *wanted);
void
sk_sort(_STACK *stack);
int
sk_is_sorted(const _STACK *stack);
int
sk_push(_STACK *stack, void *new_item);
int
sk_unshift(_STACK *stack, void *new_item);
int
sk_insert(_STACK *stack, void *new_item, int index);
void *
sk_set(_STACK *stack, int index, void *new_item);
void *
sk_pop(_STACK *stack);
void *
sk_shift(_STACK *stack);
void *
sk_delete(_STACK *stack, int index);
void *
sk_delete_ptr(_STACK *stack, void *wanted);
void
sk_zero(_STACK *stack);
πΈ DESCRIPTION πΈ
okay so here's the lore bestie: OpenSSL introduced this whole idiosyncratic concept of variable sized arrays of pointers, and then called such an array a βstackβ. which is, um. somewhat misleading. π₯΄ and like, as documented right here on this page, OpenSSL stacks are not type safe β they only handle void * function arguments and return values. that's the whole bit.
OpenSSL also gives you a fragile, unusually complicated system of macro-generated wrappers that offers superficial type safety at the expense of extensive obfuscation. it's implemented using large amounts of autogenerated code involving exceedingly ugly, nested cpp(1) macros π β check the STACK_OF(3) manual page for the details, iykyk.
the main character here is the fundamental data type: the _STACK structure. it stores a variable number of void pointers and remembers the number of pointers currently stored. it can optionally hold a pointer to a comparison function. and here's the vibe check: as long as no comparison function is installed, the order of pointers is meaningful; as soon as a comparison function is installed, it becomes ill-defined. π
sk_new_null()β allocates and initializes a new, empty stack.sk_new()β identical, except it also installscompfuncas the comparison function for the new stack object.sk_set_cmp_func()β installscompfuncfor the existing stack. thecompfuncis allowed to beNULL, but the stack is not. π«sk_dup()β creates a shallow copy of the given stack, which must not be aNULLpointer. it neither copies the objects pointed to from the stack nor increases their reference counts, it merely copies the pointers. extreme care must be taken in order to avoid freeing the memory twice β for example by callingsk_free()on one copy and only callingsk_pop_free()on the other. π«Ά careful bestie.sk_free()β frees the given stack. it does not free any of the pointers stored on the stack. unless these pointers are merely copies of pointers owned by other objects, they must be freed before callingsk_free(), in order to avoid leaking memory. ifstackis aNULLpointer, no action occurs.sk_pop_free()β severely misnamed, ngl. it does not at all do what one would expect from a function called βpopβ. instead, it does the same assk_free(), except that it also calls the functionfreefuncon each of the pointers contained in the stack. if the calls tofreefuncare intended to free the memory in use by the objects on the stack, ensure that no other pointers to the same objects remain elsewhere. πsk_find()β searches the stack for thewantedpointer. if the stack contains more than one copy of thewantedpointer, only the first match is found. if a comparison function is installed for the stack, the stack is first sorted withsk_sort(), and instead of comparing pointers, two pointers are considered to match if the comparison function returns 0.sk_sort()β sorts the stack usingqsort(3)and the installed comparison function. ifstackis aNULLpointer or already considered sorted, no action occurs. this function can only be called if a comparison function is installed.sk_is_sorted()β reports whether the stack is considered sorted. callingsk_new_null()orsk_new(), successfully callingsk_push(),sk_unshift(),sk_insert(), orsk_set(), or changing the comparison function sets the state to unsorted. if a comparison function is installed, callingsk_sort(), orsk_find()sets the state to sorted.sk_push()β pushesnew_itemonto the end of the stack, increasing the number of pointers by 1. ifstackis aNULLpointer, no action occurs.sk_unshift()β insertsnew_itemat the beginning of the stack, such that it gets the index 0. the number of pointers increases by 1. ifstackis aNULLpointer, no action occurs.sk_insert()β inserts thenew_iteminto the stack such that it gets the givenindex. ifindexis less than 0 or greater than or equal tosk_num(stack), the effect is the same as forsk_push(). ifstackis aNULLpointer, no action occurs.sk_set()β replaces the pointer with the givenindexon the stack with thenew_item. the old pointer is not freed, which may leak memory if no copy of it exists elsewhere. ifstackis aNULLpointer or ifindexis less than 0 or greater than or equal tosk_num(stack), no action occurs.sk_pop()andsk_shift()β remove the pointer with the highest or lowest index from the stack, respectively, reducing the number of pointers by 1. ifstackis aNULLpointer or if it is empty, no action occurs.sk_delete()β removes the pointer with the givenindexfrom the stack, reducing the number of pointers by 1. ifstackis aNULLpointer or theindexis less than 0 or greater than or equal tosk_num(stack), no action occurs.sk_delete_ptr()β removes thewantedpointer from the stack, reducing the number of pointers by 1 if it is found. it never uses a comparison function, but only compares pointers themselves. the stack pointer must not beNULL. πsk_zero()β removes all pointers from the stack. it does not free any of the pointers. unless these pointers are merely copies of pointers owned by other objects, they must be freed before callingsk_zero(), in order to avoid leaking memory. ifstackis aNULLpointer, no action occurs.
π RETURN VALUES π
sk_new_null(),sk_new(), andsk_dup()return a pointer to the newly allocated stack object, orNULLif insufficient memory is available.sk_set_cmp_func()returns a pointer to the comparison function that was previously installed for the stack, orNULLif none was installed.sk_num()returns the number of pointers currently stored on the stack, or-1ifstackis aNULLpointer.sk_value()returns the pointer with the givenindexfrom the stack, orNULLifstackis aNULLpointer or if theindexis less than 0 or greater than or equal tosk_num(stack).sk_find()returns the lowest index considered to match, or-1ifstackis aNULLpointer or if no match is found.sk_is_sorted()returns 1 if the stack is considered sorted or if it is aNULLpointer, or 0 otherwise.sk_push(),sk_unshift(), andsk_insert()return the new number of pointers on the stack, or 0 ifstackis aNULLpointer or if memory allocation fails.sk_set()returnsnew_item, orNULLifstackis aNULLpointer or if theindexis less than 0 or greater than or equal tosk_num(stack).sk_pop()andsk_shift()return the deleted pointer, orNULLifstackis aNULLpointer or if it is empty.sk_delete()returns the deleted pointer, orNULLifstackis aNULLpointer or if theindexis less than 0 or greater than or equal tosk_num(stack).sk_delete_ptr()returnswanted, orNULLif it is not found.
π SEE ALSO π
STACK_OF(3)β go read it bestie, it's the macro-wrapper saga π§
β HISTORY β
sk_new_null(), sk_new(), sk_free(), sk_pop_free(), sk_num(), sk_value(), sk_find(), sk_push(), sk_unshift(), sk_insert(), sk_pop(), sk_shift(), sk_delete(), and sk_delete_ptr() first appeared in SSLeay 0.5.1. then sk_set_cmp_func(), sk_dup(), and sk_zero() first appeared in SSLeay 0.8.0. these functions have been available since OpenBSD 2.4. π«Ά
sk_set() first appeared in OpenSSL 0.9.3. sk_sort() first appeared in OpenSSL 0.9.4. both functions have been available since OpenBSD 2.6.
sk_is_sorted() first appeared in OpenSSL 0.9.7e and has been available since OpenBSD 3.8. the timeline is giving lore drop fr fr π¦
π BUGS π
- even if a comparison function is installed, empty stacks and stacks containing a single pointer are sometimes considered sorted and sometimes considered unsorted. π
- if a comparison function is installed, the concept of βfirst matchβ in
sk_find()is ill-defined becauseqsort(3)is not a stable sorting function. it is probably best to only assume that they return an arbitrary match.
no cap, that second one is a real vibe check β don't build your whole personality on βfirst matchβ, bestie. π₯
and that's the whole page! β¨ mwah β¨ this cutie came to you from Debian, dated March 4, 2024 π bestie go forth and push pointers responsibly π
π¦ where this page comes from
- opensuse-leap 16.0: libressl-devel-doc 4.1.0-bp160.1.1 Β· OpenSSL
- Copyright (c) 2018 Ingo Schwarze <schwarze@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms