fix svace issues 07/155907/1 accepted/tizen/unified/20171017.204709 submit/tizen/20171016.132224
authorLukasz Stanislawski <l.stanislaws@samsung.com>
Thu, 12 Oct 2017 14:27:23 +0000 (16:27 +0200)
committerLukasz Stanislawski <lukasz.stanislawski@gmail.com>
Mon, 16 Oct 2017 11:05:49 +0000 (13:05 +0200)
Fix WGID: 260763, 260343, 170556, 170555.
Add util_strlcpy function to handle possible 'no null-terminated'
string issues

Change-Id: I4818fe4053942587de3ab37748e2b54e0cbc2fb3

inc/util.h
src/modules/clock/clock.c
src/modules/connection/connection.c
src/util.c

index 74f330b..a153738 100644 (file)
@@ -372,6 +372,18 @@ int util_runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_change
 void util_runtime_info_unset_changed_cb(runtime_info_key_e key, runtime_info_changed_cb cb);
 
 /**
+ * @brief Copy src string to dest and null-termianates dest string.
+ *
+ * @param dest destination string
+ * @param src source string
+ * @param dest_size size of dest buffer
+ *
+ * @return number of chars copied to dest buffer or value <= 0 if no characters
+ * has been copied.
+ */
+int util_strlcpy(char *dest, const char *src, size_t dest_size);
+
+/**
  * @}
  */
 
index 23526bc..e89eb85 100644 (file)
@@ -83,8 +83,8 @@ static int register_clock_tts(void *data,int win_type);
 #define ICON_PRIORITY  INDICATOR_PRIORITY_FIXED8
 #define MODULE_NAME            "clock"
 
-static void indicator_get_apm_by_region(char* output, void* data);
-static void indicator_get_time_by_region(char* output, void* data);
+static void indicator_get_apm_by_region(char* output, size_t output_size);
+static void indicator_get_time_by_region(char* output, size_t output_size);
 
 #ifdef _SUPPORT_SCREEN_READER
 static void ICU_set_timezone(const char *timezone);
@@ -192,8 +192,8 @@ static Eina_Bool indicator_clock_changed_cb(void *data)
                _E("Fail to add timer !");
        }
 
-       indicator_get_apm_by_region(icu_apm,data);
-       indicator_get_time_by_region(time_buf,data);
+       indicator_get_apm_by_region(icu_apm, CLOCK_STR_LEN);
+       indicator_get_time_by_region(time_buf, CLOCK_STR_LEN);
 
 
        if (clock_mode == INDICATOR_CLOCK_MODE_12H) {
@@ -417,13 +417,10 @@ static char *_string_replacer(const char *src, const char *pattern, const char *
        return result;
 }
 
-
-
-void indicator_get_apm_by_region(char* output,void *data)
+void indicator_get_apm_by_region(char* output, size_t output_size)
 {
        int ret = -1;
        char *locale = NULL;
-       retm_if(data == NULL, "Data parameter is NULL");
        retm_if(output == NULL, "output parameter is NULL");
 
        i18n_uchar u_custom_skeleton[CLOCK_STR_LEN] = { 0, };
@@ -512,23 +509,17 @@ void indicator_get_apm_by_region(char* output,void *data)
        i18n_ustring_copy_au(s_formatted, u_formatted);
        apm_length = i18n_ustring_get_length(u_formatted);
 
-       if (strlen(s_formatted) < CLOCK_STR_LEN) {
-               strncpy(output, s_formatted, strlen(s_formatted));
-       }
-       else {
-               strncpy(output, s_formatted, CLOCK_STR_LEN - 1);
-       }
+       util_strlcpy(output, s_formatted, output_size);
 
        return;
 }
 
 
 
-void indicator_get_time_by_region(char* output,void *data)
+void indicator_get_time_by_region(char* output, size_t output_size)
 {
        int ret = -1;
        char *locale;
-       retm_if(data == NULL, "Data parameter is NULL");
        retm_if(output == NULL, "output parameter is NULL");
 
        i18n_uchar u_custom_skeleton[CLOCK_STR_LEN] = { 0, };
@@ -644,12 +635,7 @@ void indicator_get_time_by_region(char* output,void *data)
                return;
        }
 
-       if (strlen(s_convert_formatted) < CLOCK_STR_LEN) {
-               strncpy(output, s_convert_formatted, strlen(s_convert_formatted));
-       }
-       else {
-               strncpy(output, s_convert_formatted, CLOCK_STR_LEN - 1);
-       }
+       util_strlcpy(output, s_convert_formatted, output_size);
 
        free(s_convert_formatted);
 
index 5f796b1..ce2faea 100644 (file)
@@ -200,7 +200,7 @@ static void _view_icon_update_ps_network(telephony_network_ps_type_e ps_type, vo
 {
        icon_e icon = _icon_level_for_ps_network_type(ps_type);
 
-       if (icon < LEVEL_LAST && icon >= LEVEL_MIN) {
+       if (icon < LEVEL_LAST) {
                show_image_icon(icon);
                show_connection_transfer_icon(data);
        } else
@@ -215,7 +215,7 @@ static void _view_icon_update_network(telephony_network_service_state_e state,
        switch (state) {
                case TELEPHONY_NETWORK_SERVICE_STATE_IN_SERVICE:
                        icon = _icon_level_for_network_type(network_type);
-                       if (icon < LEVEL_LAST && icon >= LEVEL_MIN) {
+                       if (icon < LEVEL_LAST) {
                                show_image_icon(icon);
                                show_connection_transfer_icon(data);
                        } else
index 5876357..d18492b 100644 (file)
@@ -19,6 +19,7 @@
 #include <app_common.h>
 #include <Eina.h>
 #include <system_settings.h>
+#include <string.h>
 
 #include "common.h"
 #include "indicator.h"
@@ -743,4 +744,16 @@ void util_runtime_info_unset_changed_cb(runtime_info_key_e key, runtime_info_cha
 
 }
 
+int util_strlcpy(char *dest, const char *src, size_t size)
+{
+       int dlen = strlen(src);
+       dlen = dlen >= size ? size - 1 : dlen;
+       if (dlen >= 0)
+       {
+               strncpy(dest, src, dlen);
+               dest[dlen] = '\0';
+       }
+       return dlen;
+}
+
 /* End of file */