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

libmbbootimg/src/format/loki.cpp

Lines changed: 1 addition & 3 deletions
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);

libmbbootimg/tests/format/test_android_reader.cpp

Lines changed: 1 addition & 1 deletion
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

libmbcommon/src/file/memory.cpp

Lines changed: 4 additions & 4 deletions
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;

libmbcommon/tests/test_file_util.cpp

Lines changed: 7 additions & 4 deletions
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());

libmbpatcher/src/patchers/odinpatcher.cpp

Lines changed: 6 additions & 5 deletions
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

libmbsign/src/mbsign.cpp

Lines changed: 1 addition & 2 deletions
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

libmbsparse/src/sparse.cpp

Lines changed: 1 addition & 1 deletion
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:

libmbutil/src/archive.cpp

Lines changed: 1 addition & 3 deletions
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) {

libmbutil/src/loopdev.cpp

Lines changed: 2 additions & 3 deletions
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;

libmbutil/src/selinux.cpp

Lines changed: 1 addition & 2 deletions
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) {

0 commit comments

Comments
 (0)