[Common] Fix preventing crash in argument validator 67/198867/1
authorLukasz Bardeli <l.bardeli@samsung.com>
Mon, 7 Jan 2019 10:24:28 +0000 (11:24 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Wed, 30 Jan 2019 11:38:20 +0000 (11:38 +0000)
If to method which takes object as parameter, array with one element
of this object will be passed them applicaiton will crash. This fix prevent
such situations and will throw TypeMissmatch

[Verification] Code compiles without error. Tested in chrome console and
execute automatic TCT (modules which have methods taking platform object as parameter -
account, alarm, application, bluetooth, bookmark, calendar, callhistory, contact,
content, download, exif, filesystem, iotcon, mediacontroller, messageport,
messaging, nfc, notification, push, time)
100% passrate

Change-Id: Idfd9ad6f1acc0a6d5fd140dfd655d1cab7d2291a
Signed-off-by: Lukasz Bardeli <l.bardeli@samsung.com>
(cherry picked from commit faaf457e2aa1307f0c3406208374d71d5c98827b)

src/utils/utils_api.js

index 83f18c9..8550192 100644 (file)
@@ -491,28 +491,25 @@ Converter.prototype.toString = function(val, nullable) {
 };
 
 function _toPlatformObject(val, types) {
-  var v;
   var t;
-  if (_type.isArray(val)) {
-    v = val;
-  } else {
-    v = [val];
-  }
 
   if (_type.isArray(types)) {
     t = types;
   } else {
     t = [types];
   }
+
+  if (_type.isArray(val)) {
+    throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
+      'Cannot convert ' + String(val) + ' to ' + String(t[0].name) + '.');
+  }
+
   var match = false;
   for (var i = 0; i < t.length; ++i) {
-    for (var j = 0; j < v.length; ++j) {
-      match = match || (v[j] instanceof t[i]);
+    if (val instanceof t[i]) {
+      return val;
     }
   }
-  if (match) {
-    return val;
-  }
 
   throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
       'Cannot convert ' + String(val) + ' to ' + String(t[0].name) + '.');