Assume memory allocation failed cases 12/307012/1
authorEunki, Hong <eunkiki.hong@samsung.com>
Mon, 4 Mar 2024 01:54:19 +0000 (10:54 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Mon, 4 Mar 2024 01:54:19 +0000 (10:54 +0900)
Let we consider malloc return null.

Change-Id: I5bafbf1cd47880ca5bc488cea264b88d742a7861
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
dali-scene3d/public-api/loader/environment-map-loader.cpp
dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.cpp
dali-toolkit/internal/text/rendering/text-typesetter.cpp

index f7f998e..619732d 100644 (file)
@@ -77,15 +77,22 @@ uint8_t* GetCroppedBuffer(uint8_t* sourceBuffer, uint32_t bytesPerPixel, uint32_
   uint32_t byteSize   = bytesPerPixel * xFaceSize * yFaceSize;
   uint8_t* destBuffer = reinterpret_cast<uint8_t*>(malloc(byteSize + 4u));
 
-  int32_t srcStride  = width * bytesPerPixel;
-  int32_t destStride = xFaceSize * bytesPerPixel;
-  int32_t srcOffset  = xOffset * bytesPerPixel + yOffset * srcStride;
-  int32_t destOffset = 0;
-  for(uint16_t row = yOffset; row < yOffset + yFaceSize; ++row)
+  if(DALI_LIKELY(destBuffer))
   {
-    memcpy(destBuffer + destOffset, sourceBuffer + srcOffset, destStride);
-    srcOffset += srcStride;
-    destOffset += destStride;
+    int32_t srcStride  = width * bytesPerPixel;
+    int32_t destStride = xFaceSize * bytesPerPixel;
+    int32_t srcOffset  = xOffset * bytesPerPixel + yOffset * srcStride;
+    int32_t destOffset = 0;
+    for(uint16_t row = yOffset; row < yOffset + yFaceSize; ++row)
+    {
+      memcpy(destBuffer + destOffset, sourceBuffer + srcOffset, destStride);
+      srcOffset += srcStride;
+      destOffset += destStride;
+    }
+  }
+  else
+  {
+    DALI_LOG_ERROR("malloc is failed. request malloc size : %u\n", byteSize + 4u);
   }
 
   return destBuffer;
@@ -111,7 +118,10 @@ PixelData GetCubeFace(Dali::PixelData cubePixelData, uint32_t faceIndex, CubeTyp
       uint32_t finalFaceHeight = (yOffset + static_cast<uint32_t>(faceHeight) < imageHeight) ? static_cast<uint32_t>(faceHeight) : imageHeight - yOffset;
       uint8_t* tempImageBuffer = GetCroppedBuffer(imageBuffer, bytesPerPixel, imageWidth, imageHeight, xOffset, yOffset, finalFaceWidth, finalFaceHeight);
 
-      cubeFacePixelData = Dali::Integration::NewPixelDataWithReleaseAfterUpload(tempImageBuffer, finalFaceWidth * finalFaceHeight * bytesPerPixel, finalFaceWidth, finalFaceHeight, 0u, imagePixelFormat, PixelData::FREE);
+      if(DALI_LIKELY(tempImageBuffer))
+      {
+        cubeFacePixelData = Dali::Integration::NewPixelDataWithReleaseAfterUpload(tempImageBuffer, finalFaceWidth * finalFaceHeight * bytesPerPixel, finalFaceWidth, finalFaceHeight, 0u, imagePixelFormat, PixelData::FREE);
+      }
     }
   }
   return cubeFacePixelData;
index e93057d..a23c03e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 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.
@@ -154,15 +154,22 @@ uint8_t* Scene3dView::GetCroppedBuffer(uint8_t* sourceBuffer, uint32_t bytesPerP
   uint32_t byteSize   = bytesPerPixel * xFaceSize * yFaceSize;
   uint8_t* destBuffer = reinterpret_cast<uint8_t*>(malloc(byteSize + 4u));
 
-  int32_t srcStride  = width * bytesPerPixel;
-  int32_t destStride = xFaceSize * bytesPerPixel;
-  int32_t srcOffset  = xOffset * bytesPerPixel + yOffset * srcStride;
-  int32_t destOffset = 0;
-  for(uint16_t row = yOffset; row < yOffset + yFaceSize; ++row)
+  if(DALI_LIKELY(destBuffer))
   {
-    memcpy(destBuffer + destOffset, sourceBuffer + srcOffset, destStride);
-    srcOffset += srcStride;
-    destOffset += destStride;
+    int32_t srcStride  = width * bytesPerPixel;
+    int32_t destStride = xFaceSize * bytesPerPixel;
+    int32_t srcOffset  = xOffset * bytesPerPixel + yOffset * srcStride;
+    int32_t destOffset = 0;
+    for(uint16_t row = yOffset; row < yOffset + yFaceSize; ++row)
+    {
+      memcpy(destBuffer + destOffset, sourceBuffer + srcOffset, destStride);
+      srcOffset += srcStride;
+      destOffset += destStride;
+    }
+  }
+  else
+  {
+    DALI_LOG_ERROR("malloc is failed. request malloc size : %u\n", byteSize + 4u);
   }
 
   return destBuffer;
@@ -194,9 +201,12 @@ void Scene3dView::UploadTextureFace(Texture& texture, Devel::PixelBuffer pixelBu
   uint32_t xOffset = CUBEMAP_INDEX_X[cubeType][faceIndex] * faceSize;
   uint32_t yOffset = CUBEMAP_INDEX_Y[cubeType][faceIndex] * faceSize;
 
-  uint8_t*  tempImageBuffer = GetCroppedBuffer(imageBuffer, bytesPerPixel, imageWidth, imageHeight, xOffset, yOffset, faceSize, faceSize);
-  PixelData pixelData       = PixelData::New(tempImageBuffer, faceSize * faceSize * bytesPerPixel, faceSize, faceSize, pixelBuffer.GetPixelFormat(), PixelData::FREE);
-  texture.Upload(pixelData, CubeMapLayer::POSITIVE_X + faceIndex, 0, 0, 0, faceSize, faceSize);
+  uint8_t* tempImageBuffer = GetCroppedBuffer(imageBuffer, bytesPerPixel, imageWidth, imageHeight, xOffset, yOffset, faceSize, faceSize);
+  if(DALI_LIKELY(tempImageBuffer))
+  {
+    PixelData pixelData = PixelData::New(tempImageBuffer, faceSize * faceSize * bytesPerPixel, faceSize, faceSize, pixelBuffer.GetPixelFormat(), PixelData::FREE);
+    texture.Upload(pixelData, CubeMapLayer::POSITIVE_X + faceIndex, 0, 0, 0, faceSize, faceSize);
+  }
 }
 
 void Scene3dView::SetCubeMap(const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor)
index 2176b47..fe164f6 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.
@@ -65,11 +65,12 @@ inline uint8_t MultiplyAndNormalizeColor(const uint8_t x, const uint8_t y) noexc
 /**
  * @brief Prepare decode glyph bitmap data. It must be call END_GLYPH_BITMAP end of same scope.
  */
-#define BEGIN_GLYPH_BITMAP(data)                                                                                                               \
-{                                                                                                                                              \
-  uint32_t   glyphOffet               = 0u;                                                                                                    \
-  const bool useLocalScanline         = data.glyphBitmap.compressionType != TextAbstraction::GlyphBufferData::CompressionType::NO_COMPRESSION; \
-  uint8_t* __restrict__ glyphScanline = useLocalScanline ? (uint8_t*)malloc(data.glyphBitmap.width * glyphPixelSize) : data.glyphBitmap.buffer;
+#define BEGIN_GLYPH_BITMAP(data)                                                                                                                \
+{                                                                                                                                               \
+  uint32_t   glyphOffet               = 0u;                                                                                                     \
+  const bool useLocalScanline         = data.glyphBitmap.compressionType != TextAbstraction::GlyphBufferData::CompressionType::NO_COMPRESSION;  \
+  uint8_t* __restrict__ glyphScanline = useLocalScanline ? (uint8_t*)malloc(data.glyphBitmap.width * glyphPixelSize) : data.glyphBitmap.buffer; \
+  DALI_ASSERT_ALWAYS(glyphScanline && "Glyph scanline for buffer is null!");
 
 /**
  * @brief Macro to skip useless line fast.