fix potential use-after-frees of stack memory

`devname_value` is a local variable; if `property_get` succeeds, we'll
return a pointer to it. Returning a `std::string` instead sidesteps this
problem.

Bug: 190757198
Test: TreeHugger
Change-Id: If9ca733dd21128706f2a9f62e8460b1286631aa5
Merged-In: If9ca733dd21128706f2a9f62e8460b1286631aa5
This commit is contained in:
George Burgess IV 2021-07-07 09:59:32 -07:00 committed by Hao Chen
parent cb8474d7b6
commit 3f467bd10f
3 changed files with 9 additions and 10 deletions

View file

@ -21,18 +21,17 @@ namespace hardware {
namespace gnss {
namespace common {
const char* ReplayUtils::getGnssPath() {
const char* gnss_dev_path = GNSS_PATH;
std::string ReplayUtils::getGnssPath() {
char devname_value[PROPERTY_VALUE_MAX] = "";
if (property_get("debug.location.gnss.devname", devname_value, NULL) > 0) {
gnss_dev_path = devname_value;
return devname_value;
}
return gnss_dev_path;
return GNSS_PATH;
}
bool ReplayUtils::hasGnssDeviceFile() {
struct stat sb;
return stat(getGnssPath(), &sb) != -1;
return stat(getGnssPath().c_str(), &sb) != -1;
}
bool ReplayUtils::isGnssRawMeasurement(const std::string& inputStr) {
@ -47,7 +46,7 @@ bool ReplayUtils::isNMEA(const std::string& inputStr) {
std::string ReplayUtils::getDataFromDeviceFile(const std::string& command, int mMinIntervalMs) {
char inputBuffer[INPUT_BUFFER_SIZE];
int mGnssFd = open(getGnssPath(), O_RDWR | O_NONBLOCK);
int mGnssFd = open(getGnssPath().c_str(), O_RDWR | O_NONBLOCK);
if (mGnssFd == -1) {
return "";

View file

@ -35,7 +35,7 @@ namespace gnss {
namespace common {
struct ReplayUtils {
static const char* getGnssPath();
static std::string getGnssPath();
static std::string getDataFromDeviceFile(const std::string& command, int mMinIntervalMs);

View file

@ -162,11 +162,11 @@ template <class T_IGnss>
std::unique_ptr<V2_0::GnssLocation> GnssTemplate<T_IGnss>::getLocationFromHW() {
if (!mHardwareModeChecked) {
// default using /dev/gnss0
const char* gnss_dev_path = ReplayUtils::getGnssPath();
std::string gnss_dev_path = ReplayUtils::getGnssPath();
mGnssFd = open(gnss_dev_path, O_RDWR | O_NONBLOCK);
mGnssFd = open(gnss_dev_path.c_str(), O_RDWR | O_NONBLOCK);
if (mGnssFd == -1) {
ALOGW("Failed to open %s errno: %d", gnss_dev_path, errno);
ALOGW("Failed to open %s errno: %d", gnss_dev_path.c_str(), errno);
}
mHardwareModeChecked = true;
}