Added check for empty text when adding reading command to queue 65/230365/6 submit/tizen/20200416.001154
authorMaria Bialota <m.bialota@samsung.com>
Thu, 9 Apr 2020 14:39:58 +0000 (16:39 +0200)
committerMaria Bialota <m.bialota@samsung.com>
Tue, 14 Apr 2020 13:20:16 +0000 (15:20 +0200)
Change-Id: Ibf753d614f42b440643f5bfd4748a57b4b182199

src/screen_reader_tts.c

index 43076e102072795c85422cf4b67e64e07091e887..79478c45021563270634a482c1b565e61072db71 100644 (file)
@@ -641,11 +641,20 @@ static void compress_command_queue(Eina_List **command_queue, Read_Command *new_
 
 static char* duplicate_and_sanitize(const char *text)
 {
+       DEBUG("Sanitizing text to speak...");
+       DEBUG("Text before sanitization: '%s'", text);
        Eina_Strbuf *buf = eina_strbuf_new();
+       if(buf == NULL)
+       {
+               ERROR("Eina_Strbuf allocation failed!");
+               return NULL;
+       }
        eina_strbuf_append_length(buf, text, strlen(text));
+       eina_strbuf_trim(buf);
        while(eina_strbuf_replace_all(buf, "  ", " ") > 0);
        char *ret = eina_strbuf_string_steal(buf);
        eina_strbuf_free(buf);
+       DEBUG("Text after sanitization: %s", ret);
        return ret;
 }
 
@@ -653,15 +662,31 @@ Read_Command *
 tts_speak_customized(char *text_to_speak, Eina_Bool want_discard_previous_reading,
                                         Eina_Bool discardable, AtspiAccessible *obj, unsigned int delay)
 {
-       // A command that cannot be discarded and does not compress the queue is not allowed.
        if (text_to_speak == NULL) return NULL;
 
+       char *text_to_speak_sanitized = duplicate_and_sanitize(text_to_speak);
+       if (text_to_speak_sanitized == NULL)
+       {
+               ERROR("Text sanitization went wrong!");
+               return NULL;
+       }
+       if (!g_strcmp0("", text_to_speak_sanitized))
+       {
+               DEBUG("Text to speak is empty! Doing nothing...");
+               free(text_to_speak_sanitized); // using regular free because it was allocated by regular malloc via eina_strbuf_string_steal().
+               return NULL;
+       }
 
        // note: this log is used by unversal-switch batch mode
        // it must be exactly here
        {
-               char *reading_text = g_strdup(text_to_speak);
-               if(!reading_text) return NULL;
+               char *reading_text = g_strdup(text_to_speak_sanitized);
+               if(!reading_text)
+               {
+                       ERROR("Problem with memory allocation!");
+                       free(text_to_speak_sanitized);
+                       return NULL;
+               }
                int i;
                for(i = 0; reading_text[i] && i < 3900; ++i) if (reading_text[i] < ' ' || reading_text[i] == 127) reading_text[i] = ' ';
                reading_text[i] = 0;
@@ -669,27 +694,39 @@ tts_speak_customized(char *text_to_speak, Eina_Bool want_discard_previous_readin
                g_free(reading_text);
        }
 
+       // 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);
+               g_snprintf(buffer, 32, "%s", text_to_speak_sanitized);
                WARNING("rejecting non-discardable, non-compressing command '%s...'", buffer);
+               free(text_to_speak_sanitized);
                return NULL;
        }
        Service_Data *sd = get_pointer_to_service_data_struct();
        if (!sd || !sd->tts)
+       {
+               ERROR("Problem with accessing service data struct!");
+               free(text_to_speak_sanitized);
                return NULL;
+       }
        tts_state_e state;
        tts_get_state(sd->tts, &state);
        char text[64];
-       g_strlcpy(text, text_to_speak, 63);
+       g_strlcpy(text, text_to_speak_sanitized, 63);
        text[63] = 0;
        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));
-       if (!rc) return NULL;
+       if (!rc)
+       {
+               ERROR("Problem with memory allocation!");
+               free(text_to_speak_sanitized);
+               return NULL;
+       }
 
        Read_Context *ctx = &rc->context;
-       ctx->text = duplicate_and_sanitize(text_to_speak);
+
+       ctx->text = text_to_speak_sanitized;
        ctx->chunk_count = 0;
        ctx->last_id = -1;