[Datacontrol] Prevent crashes by purging code with undefined behavior 34/251834/3
authorPawel Wasowski <p.wasowski2@samsung.com>
Tue, 19 Jan 2021 01:36:44 +0000 (02:36 +0100)
committerPawel Wasowski <p.wasowski2@samsung.com>
Tue, 19 Jan 2021 17:28:46 +0000 (17:28 +0000)
The following line caused crashes:

IdMap[info->requestId] = info.release();

Argument evaluation order was not strictly defined in C++
before C++17 - behavior of the expression above was undefined.
C++17 introduced more strict rules and their implementation in GCC
caused a crash after setting -std=c++17 - it seems, that info.release()
would be now called before info->requestId.

[Verification] tct-datacontrol-tizen-tests (auto, c++14 and c++17 build):
100% pass rate

The snippet below crashed apps 100% times when webapi-plugins were
compiled with C++17. Now, the snippet works fine

var PROVIDER_ID =
        "http://tizen.org/datacontrol/provider/DictionaryDataControlProvider";
var DATA_ID = "Dictionary";
var TYPE_SQL = "SQL";
var TYPE_MAP = "MAP";

var globalDataControl = tizen.datacontrol.getDataControlConsumer(PROVIDER_ID,
                                                                 DATA_ID,
                                                                 TYPE_MAP);
function successcb(id) {
  console.log("Ok: reqid " + id);
}

function errorcb(id, error) {
  console.log("Error id: " + id + ", error msg: " + error.message);
}

try {
  globalDataControl.addValue(123, "tizen", "Foo", successcb, errorcb);
}
catch (err)
{
  console.log(err.name + ": " + err.message);
}

Change-Id: Ibd80f60d100a616b8a9607c27e9eebf8a64e7a10

src/datacontrol/datacontrol_instance.cc

index d954fa752ef423bcd3139cbc54d71d8256128f51..16488964c40a374bd2095d71d3f007592c1fcee9 100644 (file)
@@ -490,7 +490,8 @@ int DatacontrolInstance::RunMAPDataControlJob(const std::string& providerId,
   result = job(handle, &info->requestId);
   RETURN_IF_FAIL(result, "Doing job failed with error");
 
-  IdMap[info->requestId] = info.release();
+  auto requestId = info->requestId;
+  IdMap[requestId] = info.release();
 
   return result;
 }
@@ -524,7 +525,8 @@ int DatacontrolInstance::RunSQLDataControlJob(const std::string& providerId,
   result = job(handle, &info->requestId);
   RETURN_IF_FAIL(result, "Doing job failed with error");
 
-  IdMap[info->requestId] = info.release();
+  auto requestId = info->requestId;
+  IdMap[requestId] = info.release();
 
   return result;
 }