diff options
author | Michael Butler <butlermichael@google.com> | 2017-04-19 18:47:55 -0700 |
---|---|---|
committer | Michael Butler <butlermichael@google.com> | 2017-04-20 16:01:08 -0700 |
commit | ca451c3280b6265a9b79273b4bf89e121a050cab (patch) | |
tree | 8830ef5cc93e61b60cd1a0ee8b7324e8dcd5df6e /cpu_ref/rsCpuExecutable.cpp | |
parent | 8467325c6d80de30bb86b7665cc41dff0c276e8d (diff) | |
download | rs-ca451c3280b6265a9b79273b4bf89e121a050cab.tar.gz |
Fix clang-analyzer-security warnings for RenderScript
Replace unsafe strcpy and strcat with bounded strlcpy and strlcat.
Bug: 32511607
Test: mma, cts, vts
Change-Id: I92bc8142f82eaf78f4465bf061871cad864c53b8
Diffstat (limited to 'cpu_ref/rsCpuExecutable.cpp')
-rw-r--r-- | cpu_ref/rsCpuExecutable.cpp | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/cpu_ref/rsCpuExecutable.cpp b/cpu_ref/rsCpuExecutable.cpp index 91d93556..fbc34935 100644 --- a/cpu_ref/rsCpuExecutable.cpp +++ b/cpu_ref/rsCpuExecutable.cpp @@ -299,6 +299,18 @@ static char* strgets(char *s, int size, const char **ppstr) { return s; } +// Creates a duplicate of a string. The new string is as small as possible, +// only including characters up to and including the first null-terminator; +// otherwise, the new string will be the same size as the input string. +// The code that calls duplicateString is responsible for the new string's +// lifetime, and is responsible for freeing it when it is no longer needed. +static char* duplicateString(const char *str, size_t length) { + const size_t newLen = strnlen(str, length-1) + 1; + char *newStr = new char[newLen]; + strlcpy(newStr, str, newLen); + return newStr; +} + ScriptExecutable* ScriptExecutable::createFromSharedObject( void* sharedObj, uint32_t expectedChecksum) { char line[MAXLINE]; @@ -369,8 +381,7 @@ ScriptExecutable* ScriptExecutable::createFromSharedObject( } fieldAddress[i] = addr; fieldIsObject[i] = false; - fieldName[i] = new char[strlen(line)+1]; - strcpy(fieldName[i], line); + fieldName[i] = duplicateString(line, sizeof(line)); } if (strgets(line, MAXLINE, &rsInfo) == nullptr) { @@ -623,13 +634,8 @@ ScriptExecutable* ScriptExecutable::createFromSharedObject( goto error; } - char *pKey = new char[strlen(key)+1]; - strcpy(pKey, key); - pragmaKeys[i] = pKey; - - char *pValue = new char[strlen(value)+1]; - strcpy(pValue, value); - pragmaValues[i] = pValue; + pragmaKeys[i] = duplicateString(key, sizeof(key)); + pragmaValues[i] = duplicateString(value, sizeof(value)); //ALOGE("Pragma %zu: Key: '%s' Value: '%s'", i, pKey, pValue); } |