chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Properly check for overflow in offsets
[elogind.git]
/
src
/
shared
/
fileio.c
diff --git
a/src/shared/fileio.c
b/src/shared/fileio.c
index 4390726a9388b19f40d0353b7f5c6687540a0c9d..ad068bf30da6c4687b80393a6c387ca388ee74ea 100644
(file)
--- a/
src/shared/fileio.c
+++ b/
src/shared/fileio.c
@@
-24,16
+24,8
@@
#include "util.h"
#include "strv.h"
#include "util.h"
#include "strv.h"
-int write_string_file(const char *fn, const char *line) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(fn);
- assert(line);
-
- f = fopen(fn, "we");
- if (!f)
- return -errno;
+int write_string_to_file(FILE *f, const char *line) {
errno = 0;
fputs(line, f);
if (!endswith(line, "\n"))
errno = 0;
fputs(line, f);
if (!endswith(line, "\n"))
@@
-47,6
+39,19
@@
int write_string_file(const char *fn, const char *line) {
return 0;
}
return 0;
}
+int write_string_file(const char *fn, const char *line) {
+ _cleanup_fclose_ FILE *f = NULL;
+
+ assert(fn);
+ assert(line);
+
+ f = fopen(fn, "we");
+ if (!f)
+ return -errno;
+
+ return write_string_to_file(f, line);
+}
+
int write_string_file_atomic(const char *fn, const char *line) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL;
int write_string_file_atomic(const char *fn, const char *line) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL;
@@
-177,7
+182,7
@@
static int parse_env_file_internal(
void *userdata) {
_cleanup_free_ char *contents = NULL, *key = NULL;
void *userdata) {
_cleanup_free_ char *contents = NULL, *key = NULL;
- size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_whitespace = (size_t) -1;
+ size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_
value_whitespace = (size_t) -1, last_key_
whitespace = (size_t) -1;
char *p, *value = NULL;
int r;
char *p, *value = NULL;
int r;
@@
-212,6
+217,8
@@
static int parse_env_file_internal(
state = COMMENT;
else if (!strchr(WHITESPACE, c)) {
state = KEY;
state = COMMENT;
else if (!strchr(WHITESPACE, c)) {
state = KEY;
+ last_key_whitespace = (size_t) -1;
+
if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) {
r = -ENOMEM;
goto fail;
if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) {
r = -ENOMEM;
goto fail;
@@
-225,9
+232,15
@@
static int parse_env_file_internal(
if (strchr(newline, c)) {
state = PRE_KEY;
n_key = 0;
if (strchr(newline, c)) {
state = PRE_KEY;
n_key = 0;
- } else if (c == '=')
+ } else if (c == '=')
{
state = PRE_VALUE;
state = PRE_VALUE;
- else {
+ last_value_whitespace = (size_t) -1;
+ } else {
+ if (!strchr(WHITESPACE, c))
+ last_key_whitespace = (size_t) -1;
+ else if (last_key_whitespace == (size_t) -1)
+ last_key_whitespace = n_key;
+
if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) {
r = -ENOMEM;
goto fail;
if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) {
r = -ENOMEM;
goto fail;
@@
-239,7
+252,7
@@
static int parse_env_file_internal(
break;
case PRE_VALUE:
break;
case PRE_VALUE:
- if (strchr(newline, c)
|| strchr(COMMENTS, c)
) {
+ if (strchr(newline, c)) {
state = PRE_KEY;
key[n_key] = 0;
state = PRE_KEY;
key[n_key] = 0;
@@
-247,8
+260,8
@@
static int parse_env_file_internal(
value[n_value] = 0;
/* strip trailing whitespace from key */
value[n_value] = 0;
/* strip trailing whitespace from key */
-
while(strchr(WHITESPACE, key[--n_key])
)
- key[
n_key]=
0;
+
if (last_key_whitespace != (size_t) -1
)
+ key[
last_key_whitespace] =
0;
r = push(key, value, userdata);
if (r < 0)
r = push(key, value, userdata);
if (r < 0)
@@
-257,6
+270,7
@@
static int parse_env_file_internal(
n_key = 0;
value = NULL;
value_alloc = n_value = 0;
n_key = 0;
value = NULL;
value_alloc = n_value = 0;
+
} else if (c == '\'')
state = SINGLE_QUOTE_VALUE;
else if (c == '\"')
} else if (c == '\'')
state = SINGLE_QUOTE_VALUE;
else if (c == '\"')
@@
-279,18
+293,19
@@
static int parse_env_file_internal(
case VALUE:
if (strchr(newline, c)) {
state = PRE_KEY;
case VALUE:
if (strchr(newline, c)) {
state = PRE_KEY;
+
key[n_key] = 0;
if (value)
value[n_value] = 0;
key[n_key] = 0;
if (value)
value[n_value] = 0;
- /* Chomp off trailing whitespace */
- if (last_whitespace != (size_t) -1)
- value[last_whitespace] = 0;
+ /* Chomp off trailing whitespace
from value
*/
+ if (last_
value_
whitespace != (size_t) -1)
+ value[last_
value_
whitespace] = 0;
/* strip trailing whitespace from key */
/* strip trailing whitespace from key */
-
while(strchr(WHITESPACE, key[--n_key])
)
- key[
n_key]=
0;
+
if (last_key_whitespace != (size_t) -1
)
+ key[
last_key_whitespace] =
0;
r = push(key, value, userdata);
if (r < 0)
r = push(key, value, userdata);
if (r < 0)
@@
-299,14
+314,15
@@
static int parse_env_file_internal(
n_key = 0;
value = NULL;
value_alloc = n_value = 0;
n_key = 0;
value = NULL;
value_alloc = n_value = 0;
+
} else if (c == '\\') {
state = VALUE_ESCAPE;
} else if (c == '\\') {
state = VALUE_ESCAPE;
- last_whitespace = (size_t) -1;
+ last_
value_
whitespace = (size_t) -1;
} else {
if (!strchr(WHITESPACE, c))
} else {
if (!strchr(WHITESPACE, c))
- last_whitespace = (size_t) -1;
- else if (last_whitespace == (size_t) -1)
- last_whitespace = n_value;
+ last_
value_
whitespace = (size_t) -1;
+ else if (last_
value_
whitespace == (size_t) -1)
+ last_
value_
whitespace = n_value;
if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
r = -ENOMEM;
if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
r = -ENOMEM;
@@
-416,9
+432,13
@@
static int parse_env_file_internal(
if (value)
value[n_value] = 0;
if (value)
value[n_value] = 0;
+ if (state == VALUE)
+ if (last_value_whitespace != (size_t) -1)
+ value[last_value_whitespace] = 0;
+
/* strip trailing whitespace from key */
/* strip trailing whitespace from key */
-
while(strchr(WHITESPACE, key[--n_key])
)
- key[
n_key]=
0;
+
if (last_key_whitespace != (size_t) -1
)
+ key[
last_key_whitespace] =
0;
r = push(key, value, userdata);
if (r < 0)
r = push(key, value, userdata);
if (r < 0)
@@
-544,8
+564,8
@@
static void write_env_var(FILE *f, const char *v) {
int write_env_file(const char *fname, char **l) {
char **i;
int write_env_file(const char *fname, char **l) {
char **i;
-
char _cleanup_free_
*p = NULL;
-
FILE _cleanup_fclose_
*f = NULL;
+
_cleanup_free_ char
*p = NULL;
+
_cleanup_fclose_ FILE
*f = NULL;
int r;
r = fopen_temporary(fname, &f, &p);
int r;
r = fopen_temporary(fname, &f, &p);