Fix INTEGER_OVERFLOW coverity issues
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program.cpp
index bdc7053..c149767 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -56,6 +56,7 @@ size_t DEFAULT_UNIFORM_HASHTABLE[NUMBER_OF_DEFAULT_UNIFORMS] =
     CalculateHash(std::string("uModelView")),
     CalculateHash(std::string("uNormalMatrix")),
     CalculateHash(std::string("uProjection")),
+    CalculateHash(std::string("uScale")),
     CalculateHash(std::string("uSize")),
     CalculateHash(std::string("uColor")),
     CalculateHash(std::string("uActorColor"))};
@@ -84,7 +85,8 @@ Program* Program::New(ProgramCache& cache, const Internal::ShaderDataPtr& shader
   {
     // program not found so create it
     program = new Program(cache, shaderData, gfxController);
-    DALI_LOG_RELEASE_INFO("Program::New() created a unique program\n");
+
+    DALI_LOG_INFO(Debug::Filter::gShader, Debug::Verbose, "Program::New() created a unique program:\n  VertexShader:\n%s\n\n  FragShader:\n%s\n", shaderData->GetVertexShader(), shaderData->GetFragmentShader());
     cache.AddProgram(shaderHash, program);
   }
 
@@ -93,8 +95,6 @@ Program* Program::New(ProgramCache& cache, const Internal::ShaderDataPtr& shader
 
 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller)
 : mCache(cache),
-  mProjectionMatrix(nullptr),
-  mViewMatrix(nullptr),
   mGfxProgram(nullptr),
   mGfxController(controller),
   mProgramData(std::move(shaderData))
@@ -225,11 +225,17 @@ bool Program::GetUniform(const std::string_view& name, Hash hashedName, Hash has
   Hash             hash  = hashedName;
   std::string_view match = name;
 
+  int arrayIndex = 0;
+
   if(!name.empty() && name.back() == ']')
   {
-    hash     = hashedNameNoArray;
     auto pos = name.rfind("[");
-    match    = name.substr(0, pos - 1); // Remove subscript
+    if(pos != std::string::npos)
+    {
+      hash       = hashedNameNoArray;
+      match      = name.substr(0, pos); // Remove subscript
+      arrayIndex = atoi(&name[pos + 1]);
+    }
   }
 
   for(const ReflectionUniformInfo& item : mReflection)
@@ -239,6 +245,16 @@ bool Program::GetUniform(const std::string_view& name, Hash hashedName, Hash has
       if(!item.hasCollision || item.uniformInfo.name == match)
       {
         out = item.uniformInfo;
+
+        // Array out of bounds
+        if(item.uniformInfo.elementCount > 0 && arrayIndex >= int(item.uniformInfo.elementCount))
+        {
+          DALI_LOG_ERROR("Uniform %s, array index out of bound [%d >= %d]!\n",
+                         item.uniformInfo.name.c_str(),
+                         int(arrayIndex),
+                         int(item.uniformInfo.elementCount));
+          return false;
+        }
         return true;
       }
       else