core: rework serialization
Let's be more careful with what we serialize: let's ensure we never serialize strings that are longer than LONG_LINE_MAX, so that we know we can read them back with read_line(…, LONG_LINE_MAX, …) safely. In order to implement this all serialization functions are move to serialize.[ch], and internally will do line size checks. We'd rather skip a serialization line (with a loud warning) than write an overly long line out. Of course, this is just a second level protection, after all the data we serialize shouldn't be this long in the first place. While we are at it also clean up logging: while serializing make sure to always log about errors immediately. Also, (void)ify all calls we don't expect errors in (or catch errors as part of the general fflush_and_check() at the end.
This commit is contained in:
@@ -752,36 +752,3 @@ int getenv_bool_secure(const char *p) {
|
||||
|
||||
return parse_boolean(e);
|
||||
}
|
||||
|
||||
int serialize_environment(FILE *f, char **environment) {
|
||||
char **e;
|
||||
|
||||
STRV_FOREACH(e, environment) {
|
||||
_cleanup_free_ char *ce;
|
||||
|
||||
ce = cescape(*e);
|
||||
if (!ce)
|
||||
return -ENOMEM;
|
||||
|
||||
fprintf(f, "env=%s\n", ce);
|
||||
}
|
||||
|
||||
/* caller should call ferror() */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int deserialize_environment(char ***environment, const char *line) {
|
||||
char *uce;
|
||||
int r;
|
||||
|
||||
assert(line);
|
||||
assert(environment);
|
||||
|
||||
assert(startswith(line, "env="));
|
||||
r = cunescape(line + 4, 0, &uce);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return strv_env_replace(environment, uce);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user