X Tutup
Skip to content

Commit e437538

Browse files
committed
tree-wide: make cunescape*() functions return ssize_t
Strictly speaking, we are returning the size of a memory chunk of arbitrary size, so ssize_t is more appropriate than int.
1 parent ddedf7c commit e437538

File tree

21 files changed

+112
-109
lines changed

21 files changed

+112
-109
lines changed

src/basic/escape.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit,
288288
return r;
289289
}
290290

291-
int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) {
291+
ssize_t cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) {
292292
_cleanup_free_ char *ans = NULL;
293293
char *t;
294294
const char *f;

src/basic/escape.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ char* cescape(const char *s);
4545
char* cescape_length(const char *s, size_t n);
4646
int cescape_char(char c, char *buf);
4747

48-
int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret);
49-
static inline int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) {
48+
int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit, bool accept_nul);
49+
50+
ssize_t cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret);
51+
static inline ssize_t cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) {
5052
return cunescape_length_with_prefix(s, length, NULL, flags, ret);
5153
}
52-
static inline int cunescape(const char *s, UnescapeFlags flags, char **ret) {
54+
static inline ssize_t cunescape(const char *s, UnescapeFlags flags, char **ret) {
5355
return cunescape_length(s, strlen(s), flags, ret);
5456
}
55-
int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit, bool accept_nul);
5657

5758
typedef enum XEscapeFlags {
5859
XESCAPE_8_BIT = 1 << 0,

src/core/load-fragment.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,6 @@ int config_parse_exec_input_text(
10101010
_cleanup_free_ char *unescaped = NULL, *resolved = NULL;
10111011
ExecContext *c = data;
10121012
const Unit *u = userdata;
1013-
size_t sz;
1014-
void *p;
10151013
int r;
10161014

10171015
assert(data);
@@ -1026,9 +1024,9 @@ int config_parse_exec_input_text(
10261024
return 0;
10271025
}
10281026

1029-
r = cunescape(rvalue, 0, &unescaped);
1030-
if (r < 0) {
1031-
log_syntax(unit, LOG_WARNING, filename, line, r,
1027+
ssize_t l = cunescape(rvalue, 0, &unescaped);
1028+
if (l < 0) {
1029+
log_syntax(unit, LOG_WARNING, filename, line, l,
10321030
"Failed to decode C escaped text '%s', ignoring: %m", rvalue);
10331031
return 0;
10341032
}
@@ -1040,7 +1038,7 @@ int config_parse_exec_input_text(
10401038
return 0;
10411039
}
10421040

1043-
sz = strlen(resolved);
1041+
size_t sz = strlen(resolved);
10441042
if (c->stdin_data_size + sz + 1 < c->stdin_data_size || /* check for overflow */
10451043
c->stdin_data_size + sz + 1 > EXEC_STDIN_DATA_MAX) {
10461044
log_syntax(unit, LOG_WARNING, filename, line, 0,
@@ -1049,7 +1047,7 @@ int config_parse_exec_input_text(
10491047
return 0;
10501048
}
10511049

1052-
p = realloc(c->stdin_data, c->stdin_data_size + sz + 1);
1050+
void *p = realloc(c->stdin_data, c->stdin_data_size + sz + 1);
10531051
if (!p)
10541052
return log_oom();
10551053

@@ -4516,7 +4514,7 @@ int config_parse_set_credential(
45164514
}
45174515
} else {
45184516
char *unescaped = NULL;
4519-
int l;
4517+
ssize_t l;
45204518

45214519
/* We support escape codes here, so that users can insert trailing \n if they like */
45224520
l = cunescape(p, UNESCAPE_ACCEPT_NUL, &unescaped);

src/core/path.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,13 +674,14 @@ static int path_deserialize_item(Unit *u, const char *key, const char *value, FD
674674
p->result = f;
675675

676676
} else if (streq(key, "path-spec")) {
677-
int previous_exists, skip = 0, r;
677+
int previous_exists, skip = 0;
678678
_cleanup_free_ char *type_str = NULL;
679679

680680
if (sscanf(value, "%ms %i %n", &type_str, &previous_exists, &skip) < 2)
681681
log_unit_debug(u, "Failed to parse path-spec value: %s", value);
682682
else {
683683
_cleanup_free_ char *unescaped = NULL;
684+
ssize_t l;
684685
PathType type;
685686
PathSpec *s;
686687

@@ -690,9 +691,9 @@ static int path_deserialize_item(Unit *u, const char *key, const char *value, FD
690691
return 0;
691692
}
692693

693-
r = cunescape(value+skip, 0, &unescaped);
694-
if (r < 0) {
695-
log_unit_warning_errno(u, r, "Failed to unescape serialize path: %m");
694+
l = cunescape(value+skip, 0, &unescaped);
695+
if (l < 0) {
696+
log_unit_warning_errno(u, l, "Failed to unescape serialize path: %m");
696697
return 0;
697698
}
698699

src/core/service.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,10 +2890,11 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value,
28902890
log_unit_error_errno(u, r, "Unable to deserialize current bus owner %s: %m", value);
28912891
} else if (streq(key, "status-text")) {
28922892
char *t;
2893+
ssize_t l;
28932894

2894-
r = cunescape(value, 0, &t);
2895-
if (r < 0)
2896-
log_unit_debug_errno(u, r, "Failed to unescape status text '%s': %m", value);
2895+
l = cunescape(value, 0, &t);
2896+
if (l < 0)
2897+
log_unit_debug_errno(u, l, "Failed to unescape status text '%s': %m", value);
28972898
else
28982899
free_and_replace(s->status_text, t);
28992900

src/core/swap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,8 +1202,9 @@ static int swap_load_proc_swaps(Manager *m, bool set_flags) {
12021202
continue;
12031203
}
12041204

1205-
if (cunescape(dev, UNESCAPE_RELAX, &d) < 0)
1206-
return log_oom();
1205+
ssize_t l = cunescape(dev, UNESCAPE_RELAX, &d);
1206+
if (l < 0)
1207+
return log_error_errno(l, "Failed to unescape device path: %m");
12071208

12081209
device_found_node(m, d, DEVICE_FOUND_SWAP, DEVICE_FOUND_SWAP);
12091210

src/cryptsetup/cryptsetup.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ static char* disk_description(const char *path) {
453453
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
454454
const char *i, *name;
455455
struct stat st;
456-
int r;
457456

458457
assert(path);
459458

@@ -468,14 +467,15 @@ static char* disk_description(const char *path) {
468467

469468
if (sd_device_get_property_value(device, "ID_PART_ENTRY_NAME", &name) >= 0) {
470469
_cleanup_free_ char *unescaped = NULL;
470+
ssize_t l;
471471

472472
/* ID_PART_ENTRY_NAME uses \x style escaping, using libblkid's blkid_encode_string(). Let's
473473
* reverse this here to make the string more human friendly in case people embed spaces or
474474
* other weird stuff. */
475475

476-
r = cunescape(name, UNESCAPE_RELAX, &unescaped);
477-
if (r < 0) {
478-
log_debug_errno(r, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m");
476+
l = cunescape(name, UNESCAPE_RELAX, &unescaped);
477+
if (l < 0) {
478+
log_debug_errno(l, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m");
479479
return NULL;
480480
}
481481

src/import/pull-common.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ int pull_find_old_etags(
3434
const char *suffix,
3535
char ***etags) {
3636

37-
_cleanup_free_ char *escaped_url = NULL;
38-
_cleanup_closedir_ DIR *d = NULL;
39-
_cleanup_strv_free_ char **l = NULL;
40-
struct dirent *de;
4137
int r;
4238

4339
assert(url);
@@ -46,11 +42,11 @@ int pull_find_old_etags(
4642
if (!image_root)
4743
image_root = "/var/lib/machines";
4844

49-
escaped_url = xescape(url, FILENAME_ESCAPE);
45+
_cleanup_free_ char *escaped_url = xescape(url, FILENAME_ESCAPE);
5046
if (!escaped_url)
5147
return -ENOMEM;
5248

53-
d = opendir(image_root);
49+
_cleanup_closedir_ DIR *d = opendir(image_root);
5450
if (!d) {
5551
if (errno == ENOENT) {
5652
*etags = NULL;
@@ -60,6 +56,9 @@ int pull_find_old_etags(
6056
return -errno;
6157
}
6258

59+
_cleanup_strv_free_ char **ans = NULL;
60+
struct dirent *de;
61+
6362
FOREACH_DIRENT_ALL(de, d, return -errno) {
6463
_cleanup_free_ char *u = NULL;
6564
const char *a, *b;
@@ -93,19 +92,21 @@ int pull_find_old_etags(
9392
if (a >= b)
9493
continue;
9594

96-
r = cunescape_length(a, b - a, 0, &u);
97-
if (r < 0)
98-
return r;
95+
ssize_t l = cunescape_length(a, b - a, 0, &u);
96+
if (l < 0) {
97+
assert(l >= INT8_MIN);
98+
return l;
99+
}
99100

100101
if (!http_etag_is_valid(u))
101102
continue;
102103

103-
r = strv_consume(&l, TAKE_PTR(u));
104+
r = strv_consume(&ans, TAKE_PTR(u));
104105
if (r < 0)
105106
return r;
106107
}
107108

108-
*etags = TAKE_PTR(l);
109+
*etags = TAKE_PTR(ans);
109110

110111
return 0;
111112
}

src/libsystemd/sd-login/sd-login.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -585,20 +585,18 @@ _public_ int sd_session_get_class(const char *session, char **class) {
585585

586586
_public_ int sd_session_get_desktop(const char *session, char **desktop) {
587587
_cleanup_free_ char *escaped = NULL;
588-
char *t;
589588
int r;
589+
ssize_t l;
590590

591591
assert_return(desktop, -EINVAL);
592592

593593
r = session_get_string(session, "DESKTOP", &escaped);
594594
if (r < 0)
595595
return r;
596596

597-
r = cunescape(escaped, 0, &t);
598-
if (r < 0)
599-
return r;
600-
601-
*desktop = t;
597+
l = cunescape(escaped, 0, desktop);
598+
if (l < 0)
599+
return l;
602600
return 0;
603601
}
604602

src/login/logind-inhibit.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,11 @@ void inhibitor_stop(Inhibitor *i) {
209209
}
210210

211211
int inhibitor_load(Inhibitor *i) {
212-
213-
_cleanup_free_ char
214-
*what = NULL,
215-
*uid = NULL,
216-
*pid = NULL,
217-
*who = NULL,
218-
*why = NULL,
219-
*mode = NULL;
220-
212+
_cleanup_free_ char *what = NULL, *uid = NULL, *pid = NULL, *who = NULL, *why = NULL, *mode = NULL;
221213
InhibitWhat w;
222214
InhibitMode mm;
223215
char *cc;
216+
ssize_t l;
224217
int r;
225218

226219
r = parse_env_file(NULL, i->state_file,
@@ -255,17 +248,17 @@ int inhibitor_load(Inhibitor *i) {
255248
}
256249

257250
if (who) {
258-
r = cunescape(who, 0, &cc);
259-
if (r < 0)
260-
return log_oom();
251+
l = cunescape(who, 0, &cc);
252+
if (l < 0)
253+
return log_debug_errno(l, "Failed to unescape \"who\" of inhibitor: %m");
261254

262255
free_and_replace(i->who, cc);
263256
}
264257

265258
if (why) {
266-
r = cunescape(why, 0, &cc);
267-
if (r < 0)
268-
return log_oom();
259+
l = cunescape(why, 0, &cc);
260+
if (l < 0)
261+
return log_debug_errno(l, "Failed to unescape \"why\" of inhibitor: %m");
269262

270263
free_and_replace(i->why, cc);
271264
}

0 commit comments

Comments
 (0)
X Tutup