upgrade-apply: Modify to check th_get_size 47/317347/1 accepted/tizen/unified/20250102.161031 accepted/tizen/unified/x/20250102.211258 accepted/tizen/unified/x/asan/20250113.002122
authorSangYoun Kwak <sy.kwak@samsung.com>
Tue, 31 Dec 2024 03:18:26 +0000 (12:18 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Tue, 31 Dec 2024 03:24:28 +0000 (12:24 +0900)
The return value of th_get_size() is int type, which means it can be
negative value. Previously, the lvalue of assignment was size_t, which
is unsigned integer while rvalue is the return value of th_get_size().
If the return value is positive, it is ok since size_t contains the
range of positive range of int type. But if it is negative it can be a
problem.(also it is an error)
To resolve this issue, a code to check if the return value is negative
or not before assigning it to size_t type variable.

Change-Id: I947484daca4722d3783964ffb98ea85255998164
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
src/upgrade-apply/patch/patch.c

index d863880fb7741ec565ee0a607f38617567004071..0e4cba529988f2cfed7eefd700a530287809c769 100644 (file)
@@ -217,6 +217,8 @@ static int open_files(struct bs_data *data, const char *source_file, const char
     assert(dest_file);
     assert(patch_tar);
 
+    int patch_len = 0;
+
     data->src.fd = open_file(source_file, O_RDONLY);
     data->dest.fd = open_file(dest_file, O_RDWR);
     if (data->src.fd < 0 ||
@@ -226,7 +228,12 @@ static int open_files(struct bs_data *data, const char *source_file, const char
     data->patch_tar = patch_tar;
 
     data->src.len = get_file_len(data->src.fd);
-    data->patch_len = th_get_size(data->patch_tar);
+
+    patch_len = th_get_size(data->patch_tar);
+    if (patch_len < 0)
+        return PF_ERROR_INVALID_PATCH_FILE;
+    data->patch_len = patch_len;
+
     data->dest.len = dest_size == 0 ? get_file_len(data->dest.fd) : dest_size;
     data->patch_remaining = data->patch_len;