[FIX/SVACE] Fix svace error (integer overflow)
authorDongju Chae <dongju.chae@samsung.com>
Thu, 14 Oct 2021 07:38:30 +0000 (16:38 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Thu, 14 Oct 2021 08:45:31 +0000 (17:45 +0900)
This patch fixes svace error, integer overflow, due to tainted
operands.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
src/core/npu/NPUdrvAPI_emul.cc

index 95c35de..4f3cecf 100644 (file)
@@ -28,6 +28,7 @@
 #define MAX_EMUL_DEVICES (3)
 #define DEFAULT_DSPM_SIZE (128 * 1024) /* 128 KiB */
 #define RESERVED_DSPM_SIZE (64 * 1024) /* 64 KiB */
+#define ENVNAME_DSPM_SIZE ("MRPSIM_SPM_SIZE")
 
 static uint64_t global_exec_seq = 0;
 
@@ -233,22 +234,19 @@ TrinityEmulAPI::getDspmSize (uint32_t *dspm) const {
 
   *dspm = DEFAULT_DSPM_SIZE;
 
-  char *dspm_str = getenv ("MRPSIM_SPM_SIZE");
+  char *dspm_str = getenv (ENVNAME_DSPM_SIZE);
   if (dspm_str != NULL) {
-    unsigned long val;
+    unsigned long long val;
     char *unit = nullptr;
 
     errno = 0;
-    val = strtoul (dspm_str, &unit, 10);
-    if (errno == 0) {
+    val = strtoull (dspm_str, &unit, 10);
+    if (errno == 0 && val < 1024 * 1024) {
       if (unit) {
-        if (*unit == 'K' || *unit == 'k') {
+        if (*unit == 'K' || *unit == 'k')
           val *= 1024;
-        } else if (*unit == 'M' || *unit == 'm') {
+        else if (*unit == 'M' || *unit == 'm')
           val *= 1024 * 1024;
-        } else if (*unit == 'G' || *unit == 'g') {
-          val *= 1024 * 1024 * 1024;
-        }
       }
 
       /* unlimited size */