Skip to content

Commit e8b08ed

Browse files
committed
tree-wide: use memmem_safe()
Let's be paranoid and do something useful if we operate with empty haystack/needle. This doesn't actually fix anything, as the places as far as I can see check for non-emptyness already beforehand, but I will sleep safer at night, if we don't even allow the trap to be fallen in, ever, even if the code is changed sooner or later.
1 parent d8782cc commit e8b08ed

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/boot/bootctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static int get_file_version(int fd, char **v) {
163163
if (!s)
164164
goto finish;
165165

166-
e = memmem(s, st.st_size - (s - buf), " ####", 5);
166+
e = memmem_safe(s, st.st_size - (s - buf), " ####", 5);
167167
if (!e || e - s < 3) {
168168
r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Malformed version string.");
169169
goto finish;

src/import/pull-common.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "escape.h"
1111
#include "fd-util.h"
1212
#include "io-util.h"
13+
#include "memory-util.h"
1314
#include "path-util.h"
1415
#include "process-util.h"
1516
#include "pull-common.h"
@@ -342,18 +343,18 @@ static int verify_one(PullJob *checksum_job, PullJob *job) {
342343

343344
line = strjoina(job->checksum, " *", fn, "\n");
344345

345-
p = memmem(checksum_job->payload,
346-
checksum_job->payload_size,
347-
line,
348-
strlen(line));
346+
p = memmem_safe(checksum_job->payload,
347+
checksum_job->payload_size,
348+
line,
349+
strlen(line));
349350

350351
if (!p) {
351352
line = strjoina(job->checksum, " ", fn, "\n");
352353

353-
p = memmem(checksum_job->payload,
354-
checksum_job->payload_size,
355-
line,
356-
strlen(line));
354+
p = memmem_safe(checksum_job->payload,
355+
checksum_job->payload_size,
356+
line,
357+
strlen(line));
357358
}
358359

359360
if (!p || (p != (char*) checksum_job->payload && p[-1] != '\n'))

src/libsystemd/sd-bus/bus-socket.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ static int bus_socket_auth_verify_client(sd_bus *b) {
173173
if (!d)
174174
return 0;
175175

176-
e = memmem(d + 2, b->rbuffer_size - (d - (char*) b->rbuffer) - 2, "\r\n", 2);
176+
e = memmem_safe(d + 2, b->rbuffer_size - (d - (char*) b->rbuffer) - 2, "\r\n", 2);
177177
if (!e)
178178
return 0;
179179

180180
if (b->accept_fd) {
181-
f = memmem(e + 2, b->rbuffer_size - (e - (char*) b->rbuffer) - 2, "\r\n", 2);
181+
f = memmem_safe(e + 2, b->rbuffer_size - (e - (char*) b->rbuffer) - 2, "\r\n", 2);
182182
if (!f)
183183
return 0;
184184

@@ -399,7 +399,7 @@ static int bus_socket_auth_verify_server(sd_bus *b) {
399399
for (;;) {
400400
/* Check if line is complete */
401401
line = (char*) b->rbuffer + b->auth_rbegin;
402-
e = memmem(line, b->rbuffer_size - b->auth_rbegin, "\r\n", 2);
402+
e = memmem_safe(line, b->rbuffer_size - b->auth_rbegin, "\r\n", 2);
403403
if (!e)
404404
return processed;
405405

0 commit comments

Comments
 (0)