The strbuf functionality is always used to create C strings, so already
reserve an extra byte for NUL whenever size allocations occur.
Together with the fact that strbuf_str already returns a const char *,
an empty strbuf may return an unmodifiable empty string.
This renders all strbuf_str return value checks obsolete.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/296
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
if (node->values) {
const char *s = strbuf_str(buf);
- if (s != NULL && fnmatch(s, subkey, 0) == 0)
+ if (fnmatch(s, subkey, 0) == 0)
index_searchwild__allvalues(node, out);
else
index_close(node);
if (node->value_count > 0) {
const char *s = strbuf_str(buf);
- if (s != NULL && fnmatch(s, subkey, 0) == 0)
+ if (fnmatch(s, subkey, 0) == 0)
index_mm_searchwild_allvalues(node, out);
}
if (!kmod_module_strbuf_pushhex(&sbuf, value, valuelen))
goto list_error;
hex = strbuf_str(&sbuf);
- if (hex == NULL)
- goto list_error;
n = kmod_module_info_append(list, key, keylen, hex, strlen(hex));
if (n == NULL)
bool strbuf_reserve_extra(struct strbuf *buf, size_t n)
{
- if (uaddsz_overflow(buf->used, n, &n) || n > SIZE_MAX - BUF_STEP)
+ if (uaddsz_overflow(buf->used, n, &n) || n >= SIZE_MAX - BUF_STEP)
return false;
- if (n <= buf->size)
+ if (n < buf->size)
return true;
- if (n % BUF_STEP)
+ if (++n % BUF_STEP)
n = ((n / BUF_STEP) + 1) * BUF_STEP;
return buf_realloc(buf, n);
const char *strbuf_str(struct strbuf *buf)
{
- if (!buf->used || buf->bytes[buf->used - 1]) {
- if (!strbuf_reserve_extra(buf, 1))
- return NULL;
+ if (!buf->used)
+ return "";
+
+ if (buf->bytes[buf->used - 1])
buf->bytes[buf->used] = '\0';
- }
return buf->bytes;
}
namelen = strlen(name);
if (!strbuf_pushchars(path, name) ||
- /* Ensure space for (possible) '/' or '\0' */
+ /* Ensure space for (possible) '/' */
!strbuf_reserve_extra(path, 1)) {
err = -ENOMEM;
ERR("No memory\n");
strbuf_shrink_to(&salias, baselen);
- if (!strbuf_pushchars(&salias, sym->name) ||
- !strbuf_reserve_extra(&salias, 1)) {
+ if (!strbuf_pushchars(&salias, sym->name)) {
ret = -ENOMEM;
goto err_alloc;
}