Merge pull request #27770 from mrc0mmand/more-nallocfuzz-shenanigans

A couple of fixes for potential issues during OOM situations
This commit is contained in:
Yu Watanabe
2023-05-25 17:15:37 +09:00
committed by GitHub
21 changed files with 154 additions and 85 deletions
+3
View File
@@ -628,6 +628,9 @@ static int method_dump_memory_state_by_fd(sd_bus_message *message, void *userdat
dump_file = safe_fclose(dump_file);
if (!dump)
return -ENOMEM;
fd = acquire_data_fd(dump, dump_size, 0);
if (fd < 0)
return fd;
+10 -8
View File
@@ -12,6 +12,7 @@
#include "alloc-util.h"
#include "calendarspec.h"
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "macro.h"
#include "parse-util.h"
@@ -336,9 +337,9 @@ static void format_chain(FILE *f, int space, const CalendarComponent *c, bool us
}
int calendar_spec_to_string(const CalendarSpec *c, char **p) {
char *buf = NULL;
_cleanup_free_ char *buf = NULL;
_cleanup_fclose_ FILE *f = NULL;
size_t sz = 0;
FILE *f;
int r;
assert(c);
@@ -383,14 +384,15 @@ int calendar_spec_to_string(const CalendarSpec *c, char **p) {
}
r = fflush_and_check(f);
fclose(f);
if (r < 0) {
free(buf);
if (r < 0)
return r;
}
*p = buf;
f = safe_fclose(f);
if (!buf)
return -ENOMEM;
*p = TAKE_PTR(buf);
return 0;
}
+10
View File
@@ -621,8 +621,13 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant **
return log_warning_errno(r, "Could not parse core file, flushing file buffer failed: %m");
c.f = safe_fclose(c.f);
if (!buf)
return log_oom();
*ret = TAKE_PTR(buf);
}
if (ret_package_metadata)
*ret_package_metadata = TAKE_PTR(package_metadata);
@@ -735,8 +740,13 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r
return log_warning_errno(r, "Could not parse ELF file, flushing file buffer failed: %m");
c.f = safe_fclose(c.f);
if (!buf)
return log_oom();
*ret = TAKE_PTR(buf);
}
if (ret_package_metadata)
*ret_package_metadata = TAKE_PTR(elf_metadata);
+3
View File
@@ -2537,6 +2537,9 @@ int table_format(Table *t, char **ret) {
f = safe_fclose(f);
if (!buf)
return -ENOMEM;
*ret = TAKE_PTR(buf);
return 0;
+17 -16
View File
@@ -1769,6 +1769,7 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha
int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) {
_cleanup_free_ char *s = NULL;
_cleanup_fclose_ FILE *f = NULL;
size_t sz = 0;
int r;
@@ -1781,26 +1782,26 @@ int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) {
if (flags & JSON_FORMAT_OFF)
return -ENOEXEC;
{
_cleanup_fclose_ FILE *f = NULL;
f = open_memstream_unlocked(&s, &sz);
if (!f)
return -ENOMEM;
f = open_memstream_unlocked(&s, &sz);
if (!f)
return -ENOMEM;
r = json_variant_dump(v, flags, f, NULL);
if (r < 0)
return r;
/* Add terminating 0, so that the output buffer is a valid string. */
fputc('\0', f);
r = fflush_and_check(f);
}
r = json_variant_dump(v, flags, f, NULL);
if (r < 0)
return r;
assert(s);
/* Add terminating 0, so that the output buffer is a valid string. */
fputc('\0', f);
r = fflush_and_check(f);
if (r < 0)
return r;
f = safe_fclose(f);
if (!s)
return -ENOMEM;
*ret = TAKE_PTR(s);
assert(sz > 0);
return (int) sz - 1;
+2 -2
View File
@@ -284,7 +284,7 @@ int specifier_architecture(char specifier, const void *data, const char *root, c
* installation. */
static int parse_os_release_specifier(const char *root, const char *id, char **ret) {
char *v = NULL;
_cleanup_free_ char *v = NULL;
int r;
assert(ret);
@@ -293,7 +293,7 @@ static int parse_os_release_specifier(const char *root, const char *id, char **r
if (r >= 0)
/* parse_os_release() calls parse_env_file() which only sets the return value for
* entries found. Let's make sure we set the return value in all cases. */
*ret = v;
*ret = TAKE_PTR(v);
/* Translate error for missing os-release file to EUNATCH. */
return r == -ENOENT ? -EUNATCH : r;