Skip to content

Commit 0eeba7a

Browse files
authored
Merge pull request systemd#10168 from keszybz/coverity-fixes
Coverity fixes
2 parents dd5ab7d + 6f48815 commit 0eeba7a

File tree

5 files changed

+41
-64
lines changed

5 files changed

+41
-64
lines changed

src/journal-remote/journal-upload.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ static size_t fd_input_callback(void *buf, size_t size, size_t nmemb, void *user
284284
ssize_t r;
285285

286286
assert(u);
287-
assert(nmemb <= SSIZE_MAX / size);
287+
assert(nmemb < SSIZE_MAX / size);
288288

289289
if (u->input < 0)
290290
return 0;

src/journal/test-journal-syslog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static void test_syslog_parse_identifier(const char *str,
2222

2323
static void test_syslog_parse_priority(const char *str, int priority, int ret) {
2424
const char *buf = str;
25-
int priority2, ret2;
25+
int priority2 = 0, ret2;
2626

2727
ret2 = syslog_parse_priority(&buf, &priority2, false);
2828

src/libsystemd-network/sd-dhcp6-client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct sd_dhcp6_client {
4949
DHCP6IA ia_pd;
5050
sd_event_source *timeout_t1;
5151
sd_event_source *timeout_t2;
52-
int request;
52+
unsigned request;
5353
be32_t transaction_id;
5454
usec_t transaction_start;
5555
struct sd_dhcp6_lease *lease;

src/libsystemd/sd-event/sd-event.c

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ static void source_free(sd_event_source *s) {
999999
free(s->description);
10001000
free(s);
10011001
}
1002+
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_event_source*, source_free);
10021003

10031004
static int source_set_pending(sd_event_source *s, bool b) {
10041005
int r;
@@ -1092,7 +1093,7 @@ _public_ int sd_event_add_io(
10921093
sd_event_io_handler_t callback,
10931094
void *userdata) {
10941095

1095-
sd_event_source *s;
1096+
_cleanup_(source_freep) sd_event_source *s = NULL;
10961097
int r;
10971098

10981099
assert_return(e, -EINVAL);
@@ -1115,13 +1116,12 @@ _public_ int sd_event_add_io(
11151116
s->enabled = SD_EVENT_ON;
11161117

11171118
r = source_io_register(s, s->enabled, events);
1118-
if (r < 0) {
1119-
source_free(s);
1119+
if (r < 0)
11201120
return r;
1121-
}
11221121

11231122
if (ret)
11241123
*ret = s;
1124+
TAKE_PTR(s);
11251125

11261126
return 0;
11271127
}
@@ -1196,7 +1196,7 @@ _public_ int sd_event_add_time(
11961196
void *userdata) {
11971197

11981198
EventSourceType type;
1199-
sd_event_source *s;
1199+
_cleanup_(source_freep) sd_event_source *s = NULL;
12001200
struct clock_data *d;
12011201
int r;
12021202

@@ -1248,20 +1248,17 @@ _public_ int sd_event_add_time(
12481248

12491249
r = prioq_put(d->earliest, s, &s->time.earliest_index);
12501250
if (r < 0)
1251-
goto fail;
1251+
return r;
12521252

12531253
r = prioq_put(d->latest, s, &s->time.latest_index);
12541254
if (r < 0)
1255-
goto fail;
1255+
return r;
12561256

12571257
if (ret)
12581258
*ret = s;
1259+
TAKE_PTR(s);
12591260

12601261
return 0;
1261-
1262-
fail:
1263-
source_free(s);
1264-
return r;
12651262
}
12661263

12671264
static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
@@ -1277,7 +1274,7 @@ _public_ int sd_event_add_signal(
12771274
sd_event_signal_handler_t callback,
12781275
void *userdata) {
12791276

1280-
sd_event_source *s;
1277+
_cleanup_(source_freep) sd_event_source *s = NULL;
12811278
struct signal_data *d;
12821279
sigset_t ss;
12831280
int r;
@@ -1317,16 +1314,15 @@ _public_ int sd_event_add_signal(
13171314
e->signal_sources[sig] = s;
13181315

13191316
r = event_make_signal_data(e, sig, &d);
1320-
if (r < 0) {
1321-
source_free(s);
1317+
if (r < 0)
13221318
return r;
1323-
}
13241319

13251320
/* Use the signal name as description for the event source by default */
13261321
(void) sd_event_source_set_description(s, signal_to_string(sig));
13271322

13281323
if (ret)
13291324
*ret = s;
1325+
TAKE_PTR(s);
13301326

13311327
return 0;
13321328
}
@@ -1339,7 +1335,7 @@ _public_ int sd_event_add_child(
13391335
sd_event_child_handler_t callback,
13401336
void *userdata) {
13411337

1342-
sd_event_source *s;
1338+
_cleanup_(source_freep) sd_event_source *s = NULL;
13431339
int r;
13441340

13451341
assert_return(e, -EINVAL);
@@ -1369,24 +1365,22 @@ _public_ int sd_event_add_child(
13691365
s->enabled = SD_EVENT_ONESHOT;
13701366

13711367
r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s);
1372-
if (r < 0) {
1373-
source_free(s);
1368+
if (r < 0)
13741369
return r;
1375-
}
13761370

13771371
e->n_enabled_child_sources++;
13781372

13791373
r = event_make_signal_data(e, SIGCHLD, NULL);
13801374
if (r < 0) {
13811375
e->n_enabled_child_sources--;
1382-
source_free(s);
13831376
return r;
13841377
}
13851378

13861379
e->need_process_child = true;
13871380

13881381
if (ret)
13891382
*ret = s;
1383+
TAKE_PTR(s);
13901384

13911385
return 0;
13921386
}
@@ -1397,7 +1391,7 @@ _public_ int sd_event_add_defer(
13971391
sd_event_handler_t callback,
13981392
void *userdata) {
13991393

1400-
sd_event_source *s;
1394+
_cleanup_(source_freep) sd_event_source *s = NULL;
14011395
int r;
14021396

14031397
assert_return(e, -EINVAL);
@@ -1415,13 +1409,12 @@ _public_ int sd_event_add_defer(
14151409
s->enabled = SD_EVENT_ONESHOT;
14161410

14171411
r = source_set_pending(s, true);
1418-
if (r < 0) {
1419-
source_free(s);
1412+
if (r < 0)
14201413
return r;
1421-
}
14221414

14231415
if (ret)
14241416
*ret = s;
1417+
TAKE_PTR(s);
14251418

14261419
return 0;
14271420
}
@@ -1432,7 +1425,7 @@ _public_ int sd_event_add_post(
14321425
sd_event_handler_t callback,
14331426
void *userdata) {
14341427

1435-
sd_event_source *s;
1428+
_cleanup_(source_freep) sd_event_source *s = NULL;
14361429
int r;
14371430

14381431
assert_return(e, -EINVAL);
@@ -1454,13 +1447,12 @@ _public_ int sd_event_add_post(
14541447
s->enabled = SD_EVENT_ON;
14551448

14561449
r = set_put(e->post_sources, s);
1457-
if (r < 0) {
1458-
source_free(s);
1450+
if (r < 0)
14591451
return r;
1460-
}
14611452

14621453
if (ret)
14631454
*ret = s;
1455+
TAKE_PTR(s);
14641456

14651457
return 0;
14661458
}
@@ -1471,7 +1463,7 @@ _public_ int sd_event_add_exit(
14711463
sd_event_handler_t callback,
14721464
void *userdata) {
14731465

1474-
sd_event_source *s;
1466+
_cleanup_(source_freep) sd_event_source *s = NULL;
14751467
int r;
14761468

14771469
assert_return(e, -EINVAL);
@@ -1494,13 +1486,12 @@ _public_ int sd_event_add_exit(
14941486
s->enabled = SD_EVENT_ONESHOT;
14951487

14961488
r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1497-
if (r < 0) {
1498-
source_free(s);
1489+
if (r < 0)
14991490
return r;
1500-
}
15011491

15021492
if (ret)
15031493
*ret = s;
1494+
TAKE_PTR(s);
15041495

15051496
return 0;
15061497
}
@@ -1817,11 +1808,10 @@ _public_ int sd_event_add_inotify(
18171808
sd_event_inotify_handler_t callback,
18181809
void *userdata) {
18191810

1820-
bool rm_inotify = false, rm_inode = false;
18211811
struct inotify_data *inotify_data = NULL;
18221812
struct inode_data *inode_data = NULL;
18231813
_cleanup_close_ int fd = -1;
1824-
sd_event_source *s;
1814+
_cleanup_(source_freep) sd_event_source *s = NULL;
18251815
struct stat st;
18261816
int r;
18271817

@@ -1859,13 +1849,13 @@ _public_ int sd_event_add_inotify(
18591849
/* Allocate an inotify object for this priority, and an inode object within it */
18601850
r = event_make_inotify_data(e, SD_EVENT_PRIORITY_NORMAL, &inotify_data);
18611851
if (r < 0)
1862-
goto fail;
1863-
rm_inotify = r > 0;
1852+
return r;
18641853

18651854
r = event_make_inode_data(e, inotify_data, st.st_dev, st.st_ino, &inode_data);
1866-
if (r < 0)
1867-
goto fail;
1868-
rm_inode = r > 0;
1855+
if (r < 0) {
1856+
event_free_inotify_data(e, inotify_data);
1857+
return r;
1858+
}
18691859

18701860
/* Keep the O_PATH fd around until the first iteration of the loop, so that we can still change the priority of
18711861
* the event source, until then, for which we need the original inode. */
@@ -1878,30 +1868,18 @@ _public_ int sd_event_add_inotify(
18781868
LIST_PREPEND(inotify.by_inode_data, inode_data->event_sources, s);
18791869
s->inotify.inode_data = inode_data;
18801870

1881-
rm_inode = rm_inotify = false;
1882-
18831871
/* Actually realize the watch now */
18841872
r = inode_data_realize_watch(e, inode_data);
18851873
if (r < 0)
1886-
goto fail;
1874+
return r;
18871875

18881876
(void) sd_event_source_set_description(s, path);
18891877

18901878
if (ret)
18911879
*ret = s;
1880+
TAKE_PTR(s);
18921881

18931882
return 0;
1894-
1895-
fail:
1896-
source_free(s);
1897-
1898-
if (rm_inode)
1899-
event_free_inode_data(e, inode_data);
1900-
1901-
if (rm_inotify)
1902-
event_free_inotify_data(e, inotify_data);
1903-
1904-
return r;
19051883
}
19061884

19071885
static sd_event_source* event_source_free(sd_event_source *s) {

src/udev/udev-builtin-net_id.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,14 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
317317
* provide the port number in the 'dev_id' sysfs attribute instead of 'dev_port',
318318
* which thus stays initialized as 0. */
319319
if (dev_port == 0) {
320-
unsigned long type;
321-
322320
attr = udev_device_get_sysattr_value(dev, "type");
323-
/* The 'type' attribute always exists. */
324-
type = strtoul(attr, NULL, 10);
325-
if (type == ARPHRD_INFINIBAND) {
326-
attr = udev_device_get_sysattr_value(dev, "dev_id");
327-
if (attr)
328-
dev_port = strtoul(attr, NULL, 16);
321+
if (attr) {
322+
unsigned long type = strtoul(attr, NULL, 10);
323+
if (type == ARPHRD_INFINIBAND) {
324+
attr = udev_device_get_sysattr_value(dev, "dev_id");
325+
if (attr)
326+
dev_port = strtoul(attr, NULL, 16);
327+
}
329328
}
330329
}
331330
}

0 commit comments

Comments
 (0)