From 70d9fb63e60392d1e3f59cc2967f3247a0645e6c Mon Sep 17 00:00:00 2001 From: Greg Kaiser Date: Thu, 15 Jul 2021 13:33:10 +0000 Subject: [PATCH] Revert "Remove unused String8::setPathName." This reverts commit c214426e0ce5f38aed925d24b1f3ec43aab2fd55. Reason for revert: This is not unused; broke build internally Change-Id: I18e8b7954256ae015ad32743fa6a75850b00913e --- libutils/String8.cpp | 21 ++++++++++++++------- libutils/String8_fuzz.cpp | 4 ++++ libutils/include/utils/String8.h | 8 ++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/libutils/String8.cpp b/libutils/String8.cpp index 8511da9ce..195e122c6 100644 --- a/libutils/String8.cpp +++ b/libutils/String8.cpp @@ -429,17 +429,24 @@ void String8::toLower() // --------------------------------------------------------------------------- // Path functions -static void setPathName(String8& s, const char* name) { - size_t len = strlen(name); - char* buf = s.lockBuffer(len); +void String8::setPathName(const char* name) +{ + setPathName(name, strlen(name)); +} + +void String8::setPathName(const char* name, size_t len) +{ + char* buf = lockBuffer(len); memcpy(buf, name, len); // remove trailing path separator, if present - if (len > 0 && buf[len - 1] == OS_PATH_SEPARATOR) len--; + if (len > 0 && buf[len-1] == OS_PATH_SEPARATOR) + len--; + buf[len] = '\0'; - s.unlockBuffer(len); + unlockBuffer(len); } String8 String8::getPathLeaf(void) const @@ -552,7 +559,7 @@ String8& String8::appendPath(const char* name) size_t len = length(); if (len == 0) { // no existing filename, just use the new one - setPathName(*this, name); + setPathName(name); return *this; } @@ -572,7 +579,7 @@ String8& String8::appendPath(const char* name) return *this; } else { - setPathName(*this, name); + setPathName(name); return *this; } } diff --git a/libutils/String8_fuzz.cpp b/libutils/String8_fuzz.cpp index faf49b66b..a45d67514 100644 --- a/libutils/String8_fuzz.cpp +++ b/libutils/String8_fuzz.cpp @@ -89,6 +89,10 @@ std::vectorwalkPath(path_out_str.get()); path_out_str->clear(); }, + [](FuzzedDataProvider* dataProvider, android::String8* str1, + android::String8*) -> void { + str1->setPathName(dataProvider->ConsumeBytesWithTerminator(5).data()); + }, [](FuzzedDataProvider* dataProvider, android::String8* str1, android::String8*) -> void { str1->appendPath(dataProvider->ConsumeBytesWithTerminator(5).data()); diff --git a/libutils/include/utils/String8.h b/libutils/include/utils/String8.h index 8b2dcf9a0..cee5dc640 100644 --- a/libutils/include/utils/String8.h +++ b/libutils/include/utils/String8.h @@ -136,6 +136,14 @@ public: * These methods operate on the string as if it were a path name. */ + /* + * Set the filename field to a specific value. + * + * Normalizes the filename, removing a trailing '/' if present. + */ + void setPathName(const char* name); + void setPathName(const char* name, size_t numChars); + /* * Get just the filename component. *