From: Gerhard Sittig Date: Thu, 21 Dec 2023 20:20:55 +0000 (+0100) Subject: strutil: support special case in power of two calculation X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c3ada48afce7626a980c89e66d592a4242362c57;p=libsigrok.git strutil: support special case in power of two calculation The sr_next_power_of_two() helper routine rejected input value 0 (an index), considered this case invalid. It is not, requires 1 bit to hold the value. Return a "power of two" value of 1 for that input, callers expect that result. --- diff --git a/src/strutil.c b/src/strutil.c index 841a682a..2136bd49 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -1844,8 +1844,18 @@ SR_API int sr_next_power_of_two(size_t value, size_t *bits, size_t *power) if (power) *power = 0; - if (!value) - return SR_ERR_ARG; + /* + * Handle the special case of input value 0 (needs 1 bit + * and results in "power of two" value 1) here. It is not + * covered by the generic logic below. + */ + if (!value) { + if (bits) + *bits = 1; + if (power) + *power = 1; + return SR_OK; + } need_bits = 0; check_mask = 0;