Skip to content

Commit 8499427

Browse files
committed
*: Avoid memset where possible
Signed-off-by: Andrew Gunnerson <[email protected]>
1 parent bde5edb commit 8499427

File tree

15 files changed

+38
-53
lines changed

15 files changed

+38
-53
lines changed

Diff for: libmbbootimg/src/format/loki.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ oc::result<void> _loki_patch_file(Writer &writer, File &file,
296296
uint64_t aboot_offset;
297297
LokiTarget *tgt = nullptr;
298298
android::AndroidHeader ahdr;
299-
LokiHeader lhdr;
299+
LokiHeader lhdr = {};
300300

301301
memcpy(patch, LOKI_SHELLCODE, LOKI_SHELLCODE_SIZE);
302302

@@ -354,8 +354,6 @@ oc::result<void> _loki_patch_file(Writer &writer, File &file,
354354
OUTCOME_TRYV(_loki_read_android_header(writer, file, ahdr));
355355

356356
// Set up Loki header
357-
memset(&lhdr, 0, sizeof(lhdr));
358-
359357
memcpy(lhdr.magic, LOKI_MAGIC, LOKI_MAGIC_SIZE);
360358
lhdr.recovery = 0;
361359
strncpy(lhdr.build, tgt->build, sizeof(lhdr.build) - 1);

Diff for: libmbbootimg/tests/format/test_android_reader.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ TEST(AndroidSetHeaderTest, ValuesShouldMatch)
231231
"Test board name");
232232
snprintf(reinterpret_cast<char *>(ahdr.cmdline), sizeof(ahdr.cmdline),
233233
"Test cmdline");
234-
memset(ahdr.id, 0xff, sizeof(ahdr.id));
234+
std::fill(std::begin(ahdr.id), std::end(ahdr.id), 0xff);
235235

236236
ASSERT_TRUE(AndroidFormatReader::convert_header(ahdr, header));
237237

Diff for: libmbcommon/src/file/memory.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ oc::result<size_t> MemoryFile::on_write(const void *buf, size_t size)
212212
}
213213

214214
// Zero-initialize new space
215-
memset(static_cast<char *>(new_data) + m_size, 0,
216-
desired_size - m_size);
215+
std::fill_n(static_cast<char *>(new_data) + m_size,
216+
desired_size - m_size, 0);
217217

218218
m_data = new_data;
219219
m_size = desired_size;
@@ -272,8 +272,8 @@ oc::result<void> MemoryFile::on_truncate(uint64_t size)
272272

273273
// Zero-initialize new space
274274
if (size > m_size) {
275-
memset(static_cast<char *>(new_data) + m_size, 0,
276-
static_cast<size_t>(size) - m_size);
275+
std::fill_n(static_cast<char *>(new_data) + m_size,
276+
static_cast<size_t>(size) - m_size, 0);
277277
}
278278

279279
m_data = new_data;

Diff for: libmbcommon/tests/test_file_util.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <gmock/gmock.h>
2121

22+
#include <algorithm>
2223
#include <memory>
2324
#include <vector>
2425

@@ -456,8 +457,9 @@ TEST(FileMoveTest, LargeForwardsCopyShouldSucceed)
456457
{
457458
std::vector<unsigned char> buf(100000);
458459

459-
memset(buf.data(), 'a', buf.size() / 2);
460-
memset(buf.data() + buf.size() / 2, 'b', buf.size() / 2);
460+
auto middle = buf.begin() + buf.size() / 2;
461+
std::fill(buf.begin(), middle, 'a');
462+
std::fill(middle, buf.end(), 'b');
461463

462464
MemoryFile file(buf.data(), buf.size());
463465
ASSERT_TRUE(file.is_open());
@@ -475,8 +477,9 @@ TEST(FileMoveTest, LargeBackwardsCopyShouldSucceed)
475477
{
476478
std::vector<unsigned char> buf(100000);
477479

478-
memset(buf.data(), 'a', buf.size() / 2);
479-
memset(buf.data() + buf.size() / 2, 'b', buf.size() / 2);
480+
auto middle = buf.begin() + buf.size() / 2;
481+
std::fill(buf.begin(), middle, 'a');
482+
std::fill(middle, buf.end(), 'b');
480483

481484
MemoryFile file(buf.data(), buf.size());
482485
ASSERT_TRUE(file.is_open());

Diff for: libmbpatcher/src/patchers/odinpatcher.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "mbpatcher/patchers/odinpatcher.h"
2121

2222
#include <algorithm>
23+
#include <array>
2324
#include <thread>
2425
#include <unordered_set>
2526

@@ -372,16 +373,16 @@ bool OdinPatcher::process_file(archive *a, archive_entry *entry, bool sparse)
372373

373374
static const char * indent(unsigned int depth)
374375
{
375-
static char buf[16];
376-
memset(buf, ' ', sizeof(buf));
376+
static std::array<char, 16> buf;
377+
buf.fill(' ');
377378

378-
if (depth * 2 < sizeof(buf) - 1) {
379+
if (depth * 2 < buf.size() - 1) {
379380
buf[depth * 2] = '\0';
380381
} else {
381-
buf[sizeof(buf) - 1] = '\0';
382+
buf[buf.size() - 1] = '\0';
382383
}
383384

384-
return buf;
385+
return buf.data();
385386
}
386387

387388
struct NestedCtx

Diff for: libmbsign/src/mbsign.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ bool sign_data(BIO *bio_data_in, BIO *bio_sig_out, EVP_PKEY *pkey)
367367
unsigned char *buf = nullptr;
368368
size_t len;
369369
int n;
370+
SigHeader hdr = {};
370371

371372
if (version == VERSION_1_SHA512_DGST) {
372373
md_type = EVP_sha512();
@@ -438,8 +439,6 @@ bool sign_data(BIO *bio_data_in, BIO *bio_sig_out, EVP_PKEY *pkey)
438439
}
439440

440441
// Write header
441-
SigHeader hdr;
442-
memset(&hdr, 0, sizeof(hdr));
443442
memcpy(hdr.magic, MAGIC, MAGIC_SIZE);
444443
hdr.version = version;
445444

Diff for: libmbsparse/src/sparse.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ oc::result<size_t> SparseFile::on_read(void *buf, size_t size)
439439
break;
440440
}
441441
case CHUNK_TYPE_DONT_CARE:
442-
memset(buf, 0, static_cast<size_t>(to_read));
442+
std::fill_n(reinterpret_cast<unsigned char *>(buf), to_read, 0);
443443
n_read = to_read;
444444
break;
445445
default:

Diff for: libmbutil/src/archive.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,10 @@ bool libarchive_copy_data_disk_to_archive(archive *in, archive *out,
9090
ssize_t bytes_written;
9191
int64_t offset;
9292
int64_t progress = 0;
93-
char null_buf[64 * 1024];
93+
char null_buf[64 * 1024] = {};
9494
const void *buf;
9595
int ret;
9696

97-
memset(null_buf, 0, sizeof(null_buf));
98-
9997
while ((ret = archive_read_data_block(
10098
in, &buf, &bytes_read, &offset)) == ARCHIVE_OK) {
10199
if (offset > progress) {

Diff for: libmbutil/src/loopdev.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static int find_loopdev_by_scanning(void)
9999
// Avoid /dev/block/loop0 since some installers (ahem, SuperSU) are
100100
// hardcoded to use it
101101
for (int n = 1; n < MAX_LOOPDEVS; ++n) {
102-
struct loop_info64 loopinfo;
102+
loop_info64 loopinfo;
103103
struct stat sb;
104104

105105
sprintf(loopdev, LOOP_FMT, n);
@@ -168,7 +168,7 @@ bool loopdev_set_up_device(const std::string &loopdev, const std::string &file,
168168
int ffd = -1;
169169
int lfd = -1;
170170

171-
struct loop_info64 loopinfo;
171+
loop_info64 loopinfo = {};
172172

173173
if ((ffd = open(file.c_str(), ro ? O_RDONLY : O_RDWR)) < 0) {
174174
return false;
@@ -186,7 +186,6 @@ bool loopdev_set_up_device(const std::string &loopdev, const std::string &file,
186186
close(lfd);
187187
});
188188

189-
memset(&loopinfo, 0, sizeof(struct loop_info64));
190189
strlcpy(reinterpret_cast<char *>(loopinfo.lo_file_name), file.c_str(),
191190
LO_NAME_SIZE);
192191
loopinfo.lo_offset = offset;

Diff for: libmbutil/src/selinux.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ bool selinux_get_enforcing(int &value)
307307
return false;
308308
}
309309

310-
char buf[20];
311-
memset(buf, 0, sizeof(buf));
310+
char buf[20] = {};
312311
ssize_t ret = read(fd, buf, sizeof(buf) - 1);
313312
close(fd);
314313
if (ret < 0) {

Diff for: libmbutil/src/string.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,10 @@ char ** dup_cstring_list(const char * const *list)
112112
for (items = 0; list[items]; ++items);
113113
size_t size = (items + 1) * sizeof(list[0]);
114114

115-
copy = static_cast<char **>(malloc(size));
115+
copy = static_cast<char **>(calloc(1, size));
116116
if (!copy) {
117117
return nullptr;
118118
}
119-
memset(copy, 0, size);
120119

121120
for (size_t i = 0; i < items; ++i) {
122121
copy[i] = strdup(list[i]);

Diff for: mbbootui/daemon_connection.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ bool MbtoolConnection::connect()
341341
}
342342

343343
int fd;
344-
struct sockaddr_un addr;
344+
sockaddr_un addr = {};
345345

346346
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
347347
if (fd < 0) {
@@ -356,15 +356,14 @@ bool MbtoolConnection::connect()
356356
char abs_name[] = "\0" SOCKET_ADDRESS;
357357
size_t abs_name_len = sizeof(abs_name) - 1;
358358

359-
memset(&addr, 0, sizeof(addr));
360359
addr.sun_family = AF_LOCAL;
361360
memcpy(addr.sun_path, abs_name, abs_name_len);
362361

363362
// Calculate correct length so the trailing junk is not included in the
364363
// abstract socket name
365-
socklen_t addr_len = offsetof(struct sockaddr_un, sun_path) + abs_name_len;
364+
socklen_t addr_len = offsetof(sockaddr_un, sun_path) + abs_name_len;
366365

367-
if (::connect(fd, (struct sockaddr *) &addr, addr_len) < 0) {
366+
if (::connect(fd, reinterpret_cast<sockaddr *>(&addr), addr_len) < 0) {
368367
LOGE("Failed to connect to socket: %s", strerror(errno));
369368
return false;
370369
}

Diff for: mbtool/appsync.cpp

+6-12
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,13 @@ static void put_socket_to_env(const char *name, int fd)
257257
*/
258258
static int create_new_socket()
259259
{
260-
struct sockaddr_un addr;
261-
int fd;
262-
263-
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
260+
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
264261
if (fd < 0) {
265262
LOGE("Failed to create socket: %s", strerror(errno));
266263
return -1;
267264
}
268265

269-
memset(&addr, 0, sizeof(addr));
266+
sockaddr_un addr = {};
270267
addr.sun_family = AF_LOCAL;
271268
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s",
272269
INSTALLD_SOCKET_PATH);
@@ -374,9 +371,7 @@ static bool send_message(int fd, const char *command,
374371
*/
375372
static int connect_to_installd()
376373
{
377-
struct sockaddr_un addr;
378-
379-
memset(&addr, 0, sizeof(addr));
374+
sockaddr_un addr = {};
380375
addr.sun_family = AF_LOCAL;
381376
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s",
382377
INSTALLD_SOCKET_PATH);
@@ -502,7 +497,8 @@ static bool do_remove(const std::vector<std::string> &args)
502497
#undef TAG
503498
}
504499

505-
struct CommandInfo {
500+
struct CommandInfo
501+
{
506502
const char *name;
507503
unsigned int nargs;
508504
bool (*func)(const std::vector<std::string> &args);
@@ -753,9 +749,7 @@ static bool proxy_process(int fd, bool can_appsync)
753749

754750
LOGD("---");
755751

756-
struct pollfd fds[2];
757-
memset(fds, 0, sizeof(fds));
758-
752+
pollfd fds[2] = {};
759753
fds[0].fd = client_fd;
760754
fds[0].events = POLLIN;
761755
fds[1].fd = installd_fd;

Diff for: mbtool/daemon.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,7 @@ static bool client_connection(int fd)
190190

191191
static bool run_daemon()
192192
{
193-
int fd;
194-
struct sockaddr_un addr;
195-
196-
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
193+
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
197194
if (fd < 0) {
198195
LOGE("Failed to create socket: %s", strerror(errno));
199196
return false;
@@ -206,7 +203,7 @@ static bool run_daemon()
206203
char abs_name[] = "\0mbtool.daemon";
207204
size_t abs_name_len = sizeof(abs_name) - 1;
208205

209-
memset(&addr, 0, sizeof(addr));
206+
sockaddr_un addr = {};
210207
addr.sun_family = AF_LOCAL;
211208
memcpy(addr.sun_path, abs_name, abs_name_len);
212209

Diff for: odinupdater/fuse-sparse.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ int main(int argc, char *argv[])
308308
}
309309
}
310310

311-
fuse_operations fuse_oper;
312-
memset(&fuse_oper, 0, sizeof(fuse_oper));
311+
fuse_operations fuse_oper = {};
313312
fuse_oper.getattr = fuse_getattr;
314313
fuse_oper.open = fuse_open;
315314
fuse_oper.read = fuse_read;

0 commit comments

Comments
 (0)