postlinker: Add casting to uint64_t before multiply 41/315641/2 accepted/tizen/unified/20241205.060804
authorSangYoun Kwak <sy.kwak@samsung.com>
Wed, 4 Dec 2024 03:23:52 +0000 (12:23 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Wed, 4 Dec 2024 04:39:07 +0000 (13:39 +0900)
In C, the result of multiplying two 16-bit unsigned integer is 32-bit
signed integer since the compiler casts numbers implicitly before
multiply them.
This may cause unintended result, so numbers should be casted to the
larger type such as 64-bit unsigned integer.
To resolve this issue, expressions that multiplying two 16-bit integers
are modified to cast numbers to 64-bit unsigned integer before
multiplying them.

Change-Id: Ia52bbe38c5796b651dba057b8ae7d4791482197d
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
script/postlinker.c
script/postlinker_64.c

index a25da2ff4f0e4c562b09f5461fd9a8ef27757ee4..15808fd3d89d7f55f56a854e9bc40502896c2bce 100755 (executable)
@@ -2,13 +2,10 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
+#include <stdint.h>
 
 #define ELF_FORMAT 0x464c457f
 
-typedef unsigned short uint16_t;
-typedef unsigned int uint32_t;
-typedef int int32_t;
-
 typedef uint16_t Elf32_Half;
 typedef uint32_t Elf32_Word;
 typedef int32_t  Elf32_Sword;
@@ -120,7 +117,7 @@ int PostLinker(char *fileName)
        }
 
        /* Memory Allocation for Segment and Section entries */
-       pSegmentEntries = (Elf32_Phdr *)malloc(elfHeader.e_phentsize * elfHeader.e_phnum);
+       pSegmentEntries = (Elf32_Phdr *)malloc((uint64_t)elfHeader.e_phentsize * (uint64_t)elfHeader.e_phnum);
        if (pSegmentEntries == NULL) {
                printf("error[%s] : memory allocation <pSegmentEntries>\n", fileName);
                exit_status = EXIT_FAILURE;
@@ -134,7 +131,7 @@ int PostLinker(char *fileName)
                goto finalize;
        }
 
-       if (!fread(pSegmentEntries, elfHeader.e_phentsize * elfHeader.e_phnum, 1, fp)) {
+       if (!fread(pSegmentEntries, (uint64_t)elfHeader.e_phentsize * (uint64_t)elfHeader.e_phnum, 1, fp)) {
                printf("error[%s] : file read <pSegmentEntries>\n", fileName);
                exit_status = EXIT_FAILURE;
                goto finalize;
index 0764fdef30ee6123376f3ee3a76644c06ad74f39..3e378430a7773a3c171fe8e7e6e39171d39f1a95 100755 (executable)
@@ -3,6 +3,7 @@
 #include <errno.h>
 #include <string.h>
 #include <elf.h>
+#include <stdint.h>
 
 #define ELF_FORMAT 0x464c457f
 #define DT_GARBAGE 0xffffffff /* End of processor-specific */
@@ -52,7 +53,7 @@ int PostLinker64(char *fileName)
        }
 
        /* Memory Allocation for Segment and Section entries */
-       pSegmentEntries = (Elf64_Phdr *)malloc(elfHeader.e_phentsize * elfHeader.e_phnum);
+       pSegmentEntries = (Elf64_Phdr *)malloc((uint64_t)elfHeader.e_phentsize * (uint64_t)elfHeader.e_phnum);
        if (pSegmentEntries == NULL) {
                printf("error[%s] : memory allocation <pSegmentEntries>\n", fileName);
                exit_status = EXIT_FAILURE;
@@ -66,7 +67,7 @@ int PostLinker64(char *fileName)
                goto finalize;
        }
 
-       if (!fread(pSegmentEntries, elfHeader.e_phentsize * elfHeader.e_phnum, 1, fp)) {
+       if (!fread(pSegmentEntries, (uint64_t)elfHeader.e_phentsize * (uint64_t)elfHeader.e_phnum, 1, fp)) {
                printf("error[%s] : file read <pSegmentEntries>\n", fileName);
                exit_status = EXIT_FAILURE;
                goto finalize;