iovec-util: introduce iovec_equal(), and add overflow check (#41700)

This commit is contained in:
Lennart Poettering
2026-04-23 17:13:33 +02:00
committed by GitHub
23 changed files with 157 additions and 73 deletions
+12 -8
View File
@@ -21,35 +21,39 @@ size_t iovec_total_size(const struct iovec *iovec, size_t n) {
assert(iovec || n == 0);
FOREACH_ARRAY(j, iovec, n)
FOREACH_ARRAY(j, iovec, n) {
if (j->iov_len > SIZE_MAX - sum)
return SIZE_MAX; /* Indicate overflow. */
sum += j->iov_len;
}
return sum;
}
bool iovec_increment(struct iovec *iovec, size_t n, size_t k) {
bool iovec_inc_many(struct iovec *iovec, size_t n, size_t k) {
assert(iovec || n == 0);
/* Returns true if there is nothing else to send (bytes written cover all of the iovec),
* false if there's still work to do. */
bool have = false;
FOREACH_ARRAY(j, iovec, n) {
size_t sub;
if (j->iov_len == 0)
continue;
if (k == 0)
return false;
sub = MIN(j->iov_len, k);
j->iov_len -= sub;
j->iov_base = (uint8_t*) j->iov_base + sub;
size_t sub = MIN(j->iov_len, k);
iovec_inc(j, sub);
k -= sub;
have = have || iovec_is_set(j);
}
assert(k == 0); /* Anything else would mean that we wrote more bytes than available,
* or the kernel reported writing more bytes than sent. */
return true;
return !have;
}
struct iovec* iovec_make_string(struct iovec *iovec, const char *s) {
+4 -1
View File
@@ -11,7 +11,7 @@ extern const struct iovec iovec_empty; /* Points to an empty, but valid (i.e.
size_t iovec_total_size(const struct iovec *iovec, size_t n) _nonnull_if_nonzero_(1, 2);
bool iovec_increment(struct iovec *iovec, size_t n, size_t k) _nonnull_if_nonzero_(1, 2);
bool iovec_inc_many(struct iovec *iovec, size_t n, size_t k) _nonnull_if_nonzero_(1, 2);
struct iovec* iovec_make_string(struct iovec *iovec, const char *s);
@@ -38,6 +38,9 @@ char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const ch
void iovec_array_free(struct iovec *iovec, size_t n_iovec) _nonnull_if_nonzero_(1, 2);
int iovec_memcmp(const struct iovec *a, const struct iovec *b) _pure_;
static inline bool iovec_equal(const struct iovec *a, const struct iovec *b) {
return iovec_memcmp(a, b) == 0;
}
struct iovec* iovec_memdup(const struct iovec *source, struct iovec *ret);
+1 -1
View File
@@ -557,7 +557,7 @@ static int write_to_syslog(
if (!syslog_is_stream)
break;
if (iovec_increment(iovec, ELEMENTSOF(iovec), n))
if (iovec_inc_many(iovec, ELEMENTSOF(iovec), n))
break;
}