Use strdup() wrapper 89/307089/1 accepted/tizen/7.0/unified/20240306.174508
authorArtur Świgoń <a.swigon@samsung.com>
Mon, 4 Mar 2024 14:47:56 +0000 (15:47 +0100)
committerArtur Świgoń <a.swigon@samsung.com>
Mon, 4 Mar 2024 15:25:55 +0000 (15:25 +0000)
The wrapper can be used to gracefully accept a null argument and
guarantee a non-null return value, and not have to pollute the code with
null checks.

Change-Id: I46ebe4151787e72e2df0dfea247294e47ba72abf
(cherry picked from commit 9e5c7fe2073536a9edfb9a6c1cf0d2afa824b5ac)

src/e_dispatch_key_event.c

index 6acf199..a795e38 100644 (file)
@@ -4,6 +4,8 @@
 #include "e_mod_utils.h"
 #include "e_screen_reader_private.h"
 
+#include <assert.h>
+
 static Eina_Bool during_event = EINA_FALSE;
 static double time_between_presses = 0.01;
 
@@ -14,6 +16,14 @@ typedef struct _KeyEventData {
    Eldbus_Connection *conn;
 } KeyEventData;
 
+static char *_xstrdup(const char *str)
+{
+   char *copy = strdup(str ? str : "");
+   assert(copy != NULL);
+   return copy;
+}
+#pragma GCC poison strdup
+
 static Ecore_Event_Key *
 _create_key_event(KeyEventData *ked)
 {
@@ -25,10 +35,10 @@ _create_key_event(KeyEventData *ked)
          return NULL;
       }
 
-   ev->keyname = strdup(ked->info.keyname);
-   ev->key = strdup(ked->info.keyname);
-   ev->string = strdup(ked->info.keyname);
-   ev->compose = strdup(ked->info.keyname);
+   ev->keyname = _xstrdup(ked->info.keyname);
+   ev->key = _xstrdup(ked->info.keyname);
+   ev->string = _xstrdup(ked->info.keyname);
+   ev->compose = _xstrdup(ked->info.keyname);
    ev->window = e_comp->ee_win;
    ev->event_window = e_comp->ee_win;
    ev->root_window = e_comp->ee_win;
@@ -115,7 +125,7 @@ _e_dispatch_key_event(KeyEventInfo kei, Eldbus_Connection *conn, const Eldbus_Me
    if (!ked)
         return eldbus_message_error_new(msg, "org.freedesktop.DBus.Error.Failed", "Malloc error.");
 
-   ked->info.keyname = strdup(kei.keyname);
+   ked->info.keyname = _xstrdup(kei.keyname);
    ked->info.multiplicity = kei.multiplicity;
    ked->info.hold_time = kei.hold_time;
    ked->keycode = _get_keycode_from_string(kei.keyname);