Skip to content

Commit f5e6182

Browse files
committed
[sanitizer][NFC] Remove InternalScopedString::size()
size() is inconsistent with length(). In most size() use cases we can replace InternalScopedString with InternalMmapVector. Remove non-constant data() to avoid direct manipulations of internal buffer. append() should be enought to modify InternalScopedString.
1 parent b605cfb commit f5e6182

12 files changed

+42
-36
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,17 +598,13 @@ class InternalScopedString {
598598
: buffer_(max_length), length_(0) {
599599
buffer_[0] = '\0';
600600
}
601-
uptr size() const { return buffer_.size(); }
602601
uptr length() const { return length_; }
603602
void clear() {
604-
(*this)[0] = '\0';
603+
buffer_[0] = '\0';
605604
length_ = 0;
606605
}
607606
void append(const char *format, ...);
608-
char *data() { return buffer_.data(); }
609607
const char *data() const { return buffer_.data(); }
610-
char &operator[](uptr i) { return buffer_[i]; }
611-
const char &operator[](uptr i) const { return buffer_[i]; }
612608

613609
private:
614610
InternalMmapVector<char> buffer_;

compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,11 @@ void *BackgroundThread(void *arg) {
9494
void WriteToSyslog(const char *msg) {
9595
InternalScopedString msg_copy(kErrorMessageBufferSize);
9696
msg_copy.append("%s", msg);
97-
char *p = msg_copy.data();
98-
char *q;
97+
const char *p = msg_copy.data();
9998

10099
// Print one line at a time.
101100
// syslog, at least on Android, has an implicit message length limit.
102-
while ((q = internal_strchr(p, '\n'))) {
101+
while (char* q = internal_strchr(p, '\n')) {
103102
*q = '\0';
104103
WriteOneLineToSyslog(p);
105104
p = q + 1;

compiler-rt/lib/sanitizer_common/sanitizer_libignore.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void LibIgnore::AddIgnoredLibrary(const char *name_templ) {
3838
void LibIgnore::OnLibraryLoaded(const char *name) {
3939
BlockingMutexLock lock(&mutex_);
4040
// Try to match suppressions with symlink target.
41-
InternalScopedString buf(kMaxPathLength);
41+
InternalMmapVector<char> buf(kMaxPathLength);
4242
if (name && internal_readlink(name, buf.data(), buf.size() - 1) > 0 &&
4343
buf[0]) {
4444
for (uptr i = 0; i < count_; i++) {

compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -574,20 +574,12 @@ struct DlIteratePhdrData {
574574
bool first;
575575
};
576576

577-
static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
578-
DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
579-
InternalScopedString module_name(kMaxPathLength);
580-
if (data->first) {
581-
data->first = false;
582-
// First module is the binary itself.
583-
ReadBinaryNameCached(module_name.data(), module_name.size());
584-
} else if (info->dlpi_name) {
585-
module_name.append("%s", info->dlpi_name);
586-
}
577+
static int AddModuleSegments(const char *module_name, dl_phdr_info *info,
578+
InternalMmapVectorNoCtor<LoadedModule> *modules) {
587579
if (module_name[0] == '\0')
588580
return 0;
589581
LoadedModule cur_module;
590-
cur_module.set(module_name.data(), info->dlpi_addr);
582+
cur_module.set(module_name, info->dlpi_addr);
591583
for (int i = 0; i < (int)info->dlpi_phnum; i++) {
592584
const Elf_Phdr *phdr = &info->dlpi_phdr[i];
593585
if (phdr->p_type == PT_LOAD) {
@@ -599,7 +591,26 @@ static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
599591
writable);
600592
}
601593
}
602-
data->modules->push_back(cur_module);
594+
modules->push_back(cur_module);
595+
return 0;
596+
}
597+
598+
static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
599+
DlIteratePhdrData *data = (DlIteratePhdrData *)arg;
600+
if (data->first) {
601+
InternalMmapVector<char> module_name(kMaxPathLength);
602+
data->first = false;
603+
// First module is the binary itself.
604+
ReadBinaryNameCached(module_name.data(), module_name.size());
605+
return AddModuleSegments(module_name.data(), info, data->modules);
606+
}
607+
608+
if (info->dlpi_name) {
609+
InternalScopedString module_name(kMaxPathLength);
610+
module_name.append("%s", info->dlpi_name);
611+
return AddModuleSegments(module_name.data(), info, data->modules);
612+
}
613+
603614
return 0;
604615
}
605616

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
453453
// On OS X the executable path is saved to the stack by dyld. Reading it
454454
// from there is much faster than calling dladdr, especially for large
455455
// binaries with symbols.
456-
InternalScopedString exe_path(kMaxPathLength);
456+
InternalMmapVector<char> exe_path(kMaxPathLength);
457457
uint32_t size = exe_path.size();
458458
if (_NSGetExecutablePath(exe_path.data(), &size) == 0 &&
459459
realpath(exe_path.data(), buf) != 0) {
@@ -1019,7 +1019,7 @@ void MaybeReexec() {
10191019
if (DyldNeedsEnvVariable() && !lib_is_in_env) {
10201020
// DYLD_INSERT_LIBRARIES is not set or does not contain the runtime
10211021
// library.
1022-
InternalScopedString program_name(1024);
1022+
InternalMmapVector<char> program_name(1024);
10231023
uint32_t buf_size = program_name.size();
10241024
_NSGetExecutablePath(program_name.data(), &buf_size);
10251025
char *new_env = const_cast<char*>(info.dli_fname);

compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ void ReportFile::Write(const char *buffer, uptr length) {
275275

276276
bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) {
277277
MemoryMappingLayout proc_maps(/*cache_enabled*/false);
278-
InternalScopedString buff(kMaxPathLength);
279-
MemoryMappedSegment segment(buff.data(), kMaxPathLength);
278+
InternalMmapVector<char> buff(kMaxPathLength);
279+
MemoryMappedSegment segment(buff.data(), buff.size());
280280
while (proc_maps.Next(&segment)) {
281281
if (segment.IsExecutable() &&
282282
internal_strcmp(module, segment.filename) == 0) {

compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,13 @@ int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
346346

347347
FORMAT(2, 3)
348348
void InternalScopedString::append(const char *format, ...) {
349-
CHECK_LT(length_, size());
349+
CHECK_LT(length_, buffer_.size());
350350
va_list args;
351351
va_start(args, format);
352-
VSNPrintf(data() + length_, size() - length_, format, args);
352+
VSNPrintf(buffer_.data() + length_, buffer_.size() - length_, format, args);
353353
va_end(args);
354354
length_ += internal_strlen(data() + length_);
355-
CHECK_LT(length_, size());
355+
CHECK_LT(length_, buffer_.size());
356356
}
357357

358358
} // namespace __sanitizer

compiler-rt/lib/sanitizer_common/sanitizer_procmaps_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void MemoryMappingLayout::LoadFromCache() {
120120
void MemoryMappingLayout::DumpListOfModules(
121121
InternalMmapVectorNoCtor<LoadedModule> *modules) {
122122
Reset();
123-
InternalScopedString module_name(kMaxPathLength);
123+
InternalMmapVector<char> module_name(kMaxPathLength);
124124
MemoryMappedSegment segment(module_name.data(), module_name.size());
125125
for (uptr i = 0; Next(&segment); i++) {
126126
const char *cur_name = segment.filename;

compiler-rt/lib/sanitizer_common/sanitizer_procmaps_mac.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
354354
void MemoryMappingLayout::DumpListOfModules(
355355
InternalMmapVectorNoCtor<LoadedModule> *modules) {
356356
Reset();
357-
InternalScopedString module_name(kMaxPathLength);
358-
MemoryMappedSegment segment(module_name.data(), kMaxPathLength);
357+
InternalMmapVector<char> module_name(kMaxPathLength);
358+
MemoryMappedSegment segment(module_name.data(), module_name.size());
359359
MemoryMappedSegmentData data;
360360
segment.data_ = &data;
361361
while (Next(&segment)) {

compiler-rt/lib/sanitizer_common/sanitizer_suppressions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ SuppressionContext::SuppressionContext(const char *suppression_types[],
3434
static bool GetPathAssumingFileIsRelativeToExec(const char *file_path,
3535
/*out*/char *new_file_path,
3636
uptr new_file_path_size) {
37-
InternalScopedString exec(kMaxPathLength);
37+
InternalMmapVector<char> exec(kMaxPathLength);
3838
if (ReadBinaryNameCached(exec.data(), exec.size())) {
3939
const char *file_name_pos = StripModuleName(exec.data());
4040
uptr path_to_exec_len = file_name_pos - exec.data();
@@ -69,7 +69,7 @@ void SuppressionContext::ParseFromFile(const char *filename) {
6969
if (filename[0] == '\0')
7070
return;
7171

72-
InternalScopedString new_file_path(kMaxPathLength);
72+
InternalMmapVector<char> new_file_path(kMaxPathLength);
7373
filename = FindFile(filename, new_file_path.data(), new_file_path.size());
7474

7575
// Read the file.

compiler-rt/lib/tsan/rtl/tsan_rtl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static void *BackgroundThread(void *arg) {
172172
fd_t fd = OpenFile(filename.data(), WrOnly);
173173
if (fd == kInvalidFd) {
174174
Printf("ThreadSanitizer: failed to open memory profile file '%s'\n",
175-
&filename[0]);
175+
filename.data());
176176
} else {
177177
mprof_fd = fd;
178178
}

compiler-rt/lib/ubsan/ubsan_monitor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ void __ubsan::__ubsan_get_current_report_data(const char **OutIssueKind,
5252

5353
// Ensure that the first character of the diagnostic text can't start with a
5454
// lowercase letter.
55-
char FirstChar = Buf.data()[0];
55+
char FirstChar = *Buf.data();
5656
if (FirstChar >= 'a' && FirstChar <= 'z')
57-
Buf.data()[0] = FirstChar - 'a' + 'A';
57+
*const_cast<char *>(Buf.data()) += 'A' - 'a';
5858

5959
*OutIssueKind = CurrentUBR->IssueKind;
6060
*OutMessage = Buf.data();

0 commit comments

Comments
 (0)