resource-monitor: Fix static analysis issue 52/280352/5 accepted/tizen/unified/20220829.215641 submit/tizen/20220829.053833
authorChanwoo Choi <cw00.choi@samsung.com>
Mon, 29 Aug 2022 02:35:49 +0000 (11:35 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Mon, 29 Aug 2022 03:43:01 +0000 (12:43 +0900)
Change-Id: Ic3d63bbe27cebe3d7eef6fb77ec404e49b84a209
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
test/resource-monitor-test.c
tools/system-resource-monitor/system-resource-monitor.c

index 32e9acf9502b7adb447766ad76943a21c8598aa8..64a93a05c9a8f1e4955a5351b8bd5e4c7cd75bd9 100644 (file)
@@ -14,9 +14,6 @@
 * limitations under the License.
 */
 
-#include <stdio.h>
-#include "../include/resource-monitor.h"
-
 int main(int argc, char **argv)
 {
        /* TODO */
index 7a3f6539729a64df702aff324b87e78bd8df5773..b0fa74612324d3ade0fb79c8993474bda81fef14 100644 (file)
@@ -21,6 +21,7 @@
 #include <signal.h>
 #include <unistd.h>
 #include <inttypes.h>
+#include <limits.h>
 
 #include "resource-monitor.h"
 
@@ -157,8 +158,8 @@ struct resource_data {
 };
 
 struct resource_monitor_data {
-       unsigned int secs;
-       unsigned int max;
+       int secs;
+       int max;
 
        int mon_id;
        int num_res;
@@ -260,8 +261,8 @@ static inline int get_resource_attr_array_value(struct resource_data *res, int i
        int ret = 0;
        int length;
        double *array = NULL;
-       char buf[BUFF_MAX];
-       char temp[10];
+       char buf[BUFF_MAX + 1];
+       char temp[BUFF_MAX];
 
        if (!res)
                return -1;
@@ -274,10 +275,14 @@ static inline int get_resource_attr_array_value(struct resource_data *res, int i
 
                if (ret < 0) break;
 
-               memset(buf, 0, BUFF_MAX);
+               memset(buf, 0, BUFF_MAX + 1);
                for (i = 0; i < length; i++) {
-                       snprintf(temp, 10, "%2.2f ", array[i]);
-                       strcat(buf, temp);
+                       snprintf(temp, BUFF_MAX, "%2.2f ", array[i]);
+
+                       if (strlen(buf) + strlen(temp) >= BUFF_MAX)
+                               break;
+
+                       strncat(buf, temp, BUFF_MAX);
                }
 
                printf("%40s | %-5s | %s", buf, res->attrs[idx].unit, res->attrs[idx].desc);
@@ -517,12 +522,20 @@ int main(int argc, char *argv[])
        while (opt < argc) {
                if (!strncmp(argv[opt], "-", 1)) {
                        for (i = 1; *(argv[opt] + i); i++) {
+                               int input;
+
                                switch (*(argv[opt] + i)) {
                                case 'd':
-                                       g_data.secs = atoi(argv[opt + 1]);
+                                       input = atoi(argv[opt + 1]);
+                                       if (input < 0 || input >= INT_MAX)
+                                               break;
+                                       g_data.secs = input;
                                        break;
                                case 'n':
-                                       g_data.max = atoi(argv[opt + 1]);
+                                       input = atoi(argv[opt + 1]);
+                                       if (input < 0 || input >= INT_MAX)
+                                               break;
+                                       g_data.max = input;
                                        break;
                                case 'h':
                                        usage();