1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-21 18:03:41 +03:00

journal: add some careful overflow checking

(cherry picked from commit d8671b1c6f036ce270b9631973314e7de24e74b1)
(cherry picked from commit 84e1819ec104a168f7904134b6212669133c955f)
(cherry picked from commit 03af9b1476ff56c67cb84d14927f1ac7b1a534e3)
This commit is contained in:
Lennart Poettering 2021-06-08 22:14:40 +02:00 committed by Zbigniew Jędrzejewski-Szmek
parent 31f6ae00f3
commit 12fa360daa

View File

@ -2856,25 +2856,33 @@ void journal_print_header(sd_journal *j) {
}
}
_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *ret) {
Iterator i;
JournalFile *f;
uint64_t sum = 0;
assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD);
assert_return(bytes, -EINVAL);
assert_return(ret, -EINVAL);
ORDERED_HASHMAP_FOREACH(f, j->files, i) {
struct stat st;
uint64_t b;
if (fstat(f->fd, &st) < 0)
return -errno;
sum += (uint64_t) st.st_blocks * 512ULL;
b = (uint64_t) st.st_blocks;
if (b > UINT64_MAX / 512)
return -EOVERFLOW;
b *= 512;
if (sum > UINT64_MAX - b)
return -EOVERFLOW;
sum += b;
}
*bytes = sum;
*ret = sum;
return 0;
}