Correct g_strlcpy() & g_strlcat() usage 45/280745/1 accepted/tizen/unified/20220908.172936 submit/tizen/20220906.053732
authorArtur Świgoń <a.swigon@samsung.com>
Fri, 2 Sep 2022 12:25:41 +0000 (14:25 +0200)
committerArtur Świgoń <a.swigon@samsung.com>
Fri, 2 Sep 2022 12:38:25 +0000 (14:38 +0200)
g_strlcpy() and g_strlcat() are either wrappers around system strlcpy()
and strlcat(), respectively, or GLib-provided replacements. Either way,
these functions take the whole buffer size as a parameter and take all
necessary care to copy at most (sizeof(buf) - strlen(buf) - 1) bytes and
NUL-terminate the output as well.

References:
- https://linux.die.net/man/3/strlcpy
- https://linux.die.net/man/3/strlcat
- https://docs.gtk.org/glib/func.strlcpy.html
- https://docs.gtk.org/glib/func.strlcat.html

Change-Id: I66de1f03ca84098ddc00975a7525148e1aada3a5

src/app_tracker.c
src/granularity_read.c
src/screen_reader_spi.c
src/screen_reader_tts.c

index 6e4dc9269967b8eb1339be27b6f787c1d5df2991..5252f8ff8c466a9407919c9292959fc900fb3f17 100644 (file)
@@ -440,18 +440,18 @@ static void _on_atspi_event_cb(AtspiEvent *event, void *user_data)
                char buf[256] = "\0";
                if (event->detail1) {
                        if (role == ATSPI_ROLE_TOGGLE_BUTTON)
-                               g_strlcat(buf, _("IDS_MSG_BODY_ON_T_TTS"), sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, _("IDS_MSG_BODY_ON_T_TTS"), sizeof(buf));
                        else if (role == ATSPI_ROLE_CHECK_BOX)
-                               g_strlcat(buf, _("COM_TV_SID_CHECKED"), sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, _("COM_TV_SID_CHECKED"), sizeof(buf));
                        else
-                               g_strlcat(buf, _("IDS_ACCS_BODY_SELECTED_TTS"), sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, _("IDS_ACCS_BODY_SELECTED_TTS"), sizeof(buf));
                } else {
                        if (role == ATSPI_ROLE_TOGGLE_BUTTON)
-                               g_strlcat(buf, _("IDS_STORYALBUM_BODY_OFF_T_TTS"), sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, _("IDS_STORYALBUM_BODY_OFF_T_TTS"), sizeof(buf));
                        else if (role == ATSPI_ROLE_CHECK_BOX)
-                               g_strlcat(buf, _("COM_TV_SID_UNCHECKED"), sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, _("COM_TV_SID_UNCHECKED"), sizeof(buf));
                        else
-                               g_strlcat(buf, _("IDS_ACCS_OPT_NOT_SELECTED_TTS"), sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, _("IDS_ACCS_OPT_NOT_SELECTED_TTS"), sizeof(buf));
                }
                tw_speak(buf, EINA_TRUE);
        } else if (!g_strcmp0(event->type, "object:state-changed:selected")
@@ -474,15 +474,15 @@ static void _on_atspi_event_cb(AtspiEvent *event, void *user_data)
                        char tab_index[16] = "\0";
                        int children_count = atspi_accessible_get_child_count(parent, NULL);
                        int index = atspi_accessible_get_index_in_parent(event->source, NULL);
-                       g_strlcat(buf, name, sizeof(buf) - strlen(buf) - 1);
-                       g_strlcat(buf, ", ", sizeof(buf) - strlen(buf) - 1);
+                       g_strlcat(buf, name, sizeof(buf));
+                       g_strlcat(buf, ", ", sizeof(buf));
                        SUPPRESS_FORMAT_ERROR(g_snprintf(tab_index, sizeof(tab_index), _("IDS_ACCS_BODY_TAB_P1SD_OF_P2SD"), index + 1, children_count));
-                       g_strlcat(buf, tab_index, sizeof(buf) - strlen(buf) - 1);
-                       g_strlcat(buf, ", ", sizeof(buf) - strlen(buf) - 1);
+                       g_strlcat(buf, tab_index, sizeof(buf));
+                       g_strlcat(buf, ", ", sizeof(buf));
                        if (event->detail1)
-                               g_strlcat(buf, _("IDS_ACCS_BODY_SELECTED_TTS"), sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, _("IDS_ACCS_BODY_SELECTED_TTS"), sizeof(buf));
                        else
-                               g_strlcat(buf, _IGNORE_ON_TV("IDS_ACCS_BODY_DOUBLE_TAP_TO_SELECT_TTS"), sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, _IGNORE_ON_TV("IDS_ACCS_BODY_DOUBLE_TAP_TO_SELECT_TTS"), sizeof(buf));
                        tw_speak(buf, EINA_TRUE);
                } else if (role == ATSPI_ROLE_LIST_ITEM && atspi_accessible_get_role(parent, NULL) == ATSPI_ROLE_TREE_TABLE) {
                        if (event->detail1) {
@@ -532,11 +532,11 @@ static void _on_atspi_event_cb(AtspiEvent *event, void *user_data)
                        }
                        if ((event->detail2 == 1) && isupper((int)*text)) {
                                SUPPRESS_FORMAT_ERROR(g_snprintf(buf, sizeof(buf), _("IDS_ACCS_OPT_CAPITAL_PS_TTS"), text));
-                               g_strlcat(buf, " ", sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, " ", sizeof(buf));
                        } else if ((symtext = symbol_lookup(text))) {
                                        g_strlcat(buf, _(symtext), sizeof(buf));
                        } else {
-                               g_strlcat(buf, text, sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, text, sizeof(buf));
                        }
                        DEBUG("Text Inserted :%s", buf);
 
@@ -579,18 +579,18 @@ static void _on_atspi_event_cb(AtspiEvent *event, void *user_data)
                                else if ((symtext = symbol_lookup(text)))
                                        g_strlcat(buf, _(symtext), sizeof(buf));
                                else
-                                       g_strlcat(buf, text, sizeof(buf) - strlen(buf) - 1);
-                               g_strlcat(buf, " ", sizeof(buf) - strlen(buf) - 1);
+                                       g_strlcat(buf, text, sizeof(buf));
+                               g_strlcat(buf, " ", sizeof(buf));
                                SUPPRESS_FORMAT_ERROR(g_snprintf(buf_deleted, sizeof(buf_deleted), _("IDS_ACCS_TBOPT_PS_DELETED"), ""));
-                               g_strlcat(buf, buf_deleted, sizeof(buf) - strlen(buf) - 1);
+                               g_strlcat(buf, buf_deleted, sizeof(buf));
                                if (event->detail1 == 0) {
                                        iface_text = atspi_accessible_get_text_iface(event->source);
                                        if (iface_text) {
                                                etext = atspi_text_get_text(iface_text, 0, atspi_text_get_character_count(iface_text, NULL), NULL);
                                                if (etext) {
                                                        if (!g_strcmp0(etext, "")) {
-                                                               g_strlcat(buf, " ", sizeof(buf) - strlen(buf) - 1);//entry should be empty, need to find/get more detail here
-                                                               g_strlcat(buf, _("IDS_ACCS_BODY_ALL_CHARACTERS_DELETED_TTS"), sizeof(buf) - strlen(buf) - 1);
+                                                               g_strlcat(buf, " ", sizeof(buf));//entry should be empty, need to find/get more detail here
+                                                               g_strlcat(buf, _("IDS_ACCS_BODY_ALL_CHARACTERS_DELETED_TTS"), sizeof(buf));
                                                        }
                                                        free(etext);
                                                }
index 16124f23419c320f25681d5f5d3e9437f1932c1e..0fa998ef3cef894f24dbc339e2dc5a07b6d1dabb 100644 (file)
@@ -194,7 +194,7 @@ void granularity_read_type_set(Eina_Bool next)
                grd->granularity_read_type = (grd->granularity_read_type == 0) ? GRANULARITY_READ_TYPE_COUNT - 1 : grd->granularity_read_type - 1;
 
        gchar text[30] = "";
-       g_snprintf(text, sizeof(text) - strlen(text), "%s granularity", granularity_read_type_text[grd->granularity_read_type]); /* TODO : UX dependent*/
+       g_snprintf(text, sizeof(text), "%s granularity", granularity_read_type_text[grd->granularity_read_type]); /* TODO : UX dependent*/
        DEBUG("SPEAK : %s", text);
        tw_speak(text, EINA_TRUE);
 
index c476b8804d2ee4934bd1cc0dfa0eb256be015a2a..9ccbf0f2ca38fed6eda4508d6955558b69206de8 100644 (file)
@@ -135,11 +135,11 @@ char *generate_description_for_subtree(AtspiAccessible *obj)
                name = atspi_accessible_get_name(child, NULL);
                DEBUG("%d child name:%s", i, name);
                if (name && strncmp(name, "\0", 1))
-                       g_strlcat(ret, name, sizeof(ret) - strlen(ret) - 1);
-               g_strlcat(ret, " ", sizeof(ret) - strlen(ret) - 1);
+                       g_strlcat(ret, name, sizeof(ret));
+               g_strlcat(ret, " ", sizeof(ret));
                below = generate_description_for_subtree(child);
                if (strncmp(below, "\0", 1))
-                       g_strlcat(ret, below, sizeof(ret) - strlen(ret) - 1);
+                       g_strlcat(ret, below, sizeof(ret));
                g_clear_object(&child);
                g_free(below);
                g_free(name);
@@ -186,22 +186,22 @@ static char *spi_on_text_delete(SpiData *spi, AtspiEvent *event)
 {
        char ret[TTS_MAX_TEXT_SIZE] = "\0";
 
-       g_strlcpy(ret, g_value_get_string(&event->any_data), sizeof(ret) - 1);
-       g_strlcat(ret, " ", sizeof(ret) - strlen(ret) - 1);
-       g_strlcat(ret, _("IDS_HEALTH_TBBODY_REMOVED"), sizeof(ret) - strlen(ret) - 1);
+       g_strlcpy(ret, g_value_get_string(&event->any_data), sizeof(ret));
+       g_strlcat(ret, " ", sizeof(ret));
+       g_strlcat(ret, _("IDS_HEALTH_TBBODY_REMOVED"), sizeof(ret));
        //FIXME : This IDS value is not supported.
        /*else {
-               g_strlcpy(ret, _("IDS_TEXT_REMOVED"), sizeof(ret) - 1);
+               g_strlcpy(ret, _("IDS_TEXT_REMOVED"), sizeof(ret));
        }*/
 
        if (event->detail1 != spi->last_caret_position) {
                //FIXME : This IDS value is not supported.
                /*if (event->detail1 == 0) {
-                       g_strlcat(ret, _("IDS_REACHED_MIN_POS"), sizeof(ret) - strlen(ret) - 1);
+                       g_strlcat(ret, _("IDS_REACHED_MIN_POS"), sizeof(ret));
                }*/
                spi->ignore_next_caret_move = EINA_TRUE;
        } /*else if (event->detail1 == spi_get_text_interface_text_length(event, user_data)) {
-               g_strlcat(ret, _("IDS_REACHED_MAX_POS"), sizeof(ret) - strlen(ret) - 1);
+               g_strlcat(ret, _("IDS_REACHED_MAX_POS"), sizeof(ret));
        }*/
        return g_strdup(ret);
 }
index 346fae4d7ee850923f16da675438d3d73607238d..5ece303980facbc507cdcd34dab76afedca27ada 100644 (file)
@@ -77,7 +77,7 @@ static const char *sprintf_command(Read_Command *command)
                                 command->is_playing ? 'P' : '-',
                                 length, slices, text);
        } else {
-               g_snprintf(buffer, 64, "(nil)");
+               g_strlcpy(buffer, "(nil)", sizeof(buffer));
        }
        return buffer;
 }
@@ -252,7 +252,7 @@ static Eina_Bool can_discard(const Read_Command *prev, const Read_Command *next)
 {
        DEBUG("[START] checking if can discard: prev (%p), next(%p)", prev, next);
        char buffer[64];
-       g_snprintf(buffer, 64, "CAN DISCARD %s, %s",
+       g_snprintf(buffer, sizeof(buffer), "CAN DISCARD %s, %s",
                         prev ? (prev->discardable ? "prev discardable" : "prev non-discardable") : "prev nil",
                         next ? (next->want_discard_previous_reading ? "next want discard" : "next want no discard") : "next nil");
        DEBUG("%s", buffer);
@@ -633,8 +633,7 @@ static void tw_speak_do(TWData *tw)
                        Read_Command *command = get_read_command_from_queue(tw, &(tw->read_command_queue));
                        if (command) {
                                char text[64];
-                               g_strlcpy(text, command->context.text, 63);
-                               text[63] = 0;
+                               g_strlcpy(text, command->context.text, sizeof(text));
                                DEBUG("READ COMMAND text: %s", text);
                                DEBUG("TTS_STATE: %d", state);
                                DEBUG("TTS_STATE_NAME: %s", get_tts_state(state));
@@ -751,7 +750,7 @@ tw_speak_customized(char *text_to_speak, Eina_Bool want_discard_previous_reading
        // A command that cannot be discarded and does not compress the queue is not allowed.
        if (!discardable && !want_discard_previous_reading) {
                char buffer[32];
-               g_snprintf(buffer, 32, "%s", text_to_speak_sanitized);
+               g_snprintf(buffer, sizeof(buffer), "%s", text_to_speak_sanitized);
                WARNING("rejecting non-discardable, non-compressing command '%s...'", buffer);
                free(text_to_speak_sanitized);
                return NULL;
@@ -765,8 +764,7 @@ tw_speak_customized(char *text_to_speak, Eina_Bool want_discard_previous_reading
        tts_state_e state;
        tts_get_state(tw->tts, &state);
        char text[64];
-       g_strlcpy(text, text_to_speak_sanitized, 63);
-       text[63] = 0;
+       g_strlcpy(text, text_to_speak_sanitized, sizeof(text));
        DEBUG("READ COMMAND PARAMS, TEXT: %s, DISCARDABLE: %d, ATSPI_OBJECT: %p, DELAY %d", text, discardable, obj, delay);
 
        Read_Command *rc = g_malloc0(sizeof(Read_Command));