Clarify a bit the entry verification 67/265967/3
authorMateusz Majewski <m.majewski2@samsung.com>
Tue, 2 Nov 2021 18:24:30 +0000 (19:24 +0100)
committerMateusz Majewski <m.majewski2@samsung.com>
Fri, 5 Nov 2021 06:50:00 +0000 (07:50 +0100)
Change-Id: Ie56b714754b983ac35b1fcf224bab5c18669ae62

src/shared/queued_entry_timestamp.c

index 5798b1a..8924571 100644 (file)
@@ -176,14 +176,47 @@ LIBDLOGUTIL_EXPORT_API int dlogutil_entry_get_priority(const dlogutil_entry_s *e
        return TIZEN_ERROR_NONE;
 }
 
+bool verfiy_entry(const dlogutil_entry_s *entry)
+{
+       assert(entry);
+
+       const int payload_len = entry->len - (int) sizeof(dlogutil_entry_s);
+
+       /* This just checks if the entry is big enough to contain both the delimiter
+        * between the tag and the message, and the ending '\0'. */
+       if (payload_len < 2)
+               return false;
+
+       /* This condition is slightly weird.
+        * The point is that the payload of the entry cannot be too big.
+        * We have the limit of the payload introduced in the logger backend
+        * which had been then adopted by the pipe backend, and which we can check
+        * against. However, while this is the limit enforced by the pipe backend,
+        * the logger backend actually enforces a limit that is one character less.
+        * This makes sense in the context of the logger backend,
+        * as it simply expects (at least outside the stdout mode) some data;
+        * it doesn't really care about it's formatting. In our usage the data
+        * contains a single character for the priority, which is inside the struct
+        * in dlogutil_entry_s. However, this discreparency cannot be fixed easily,
+        * since the pipe backend actually can produce entries that use this limit
+        * completely. TODO: Is this inconsistency something we need to fix? */
+       if (payload_len > LOG_MAX_PAYLOAD_SIZE)
+               return false;
+
+       /* This makes sure that the message actually ends with a '\0'.
+        * This is important, since we return the data as a string. */
+       if (((const char *)entry)[entry->len - 1] != '\0')
+               return false;
+
+       return true;
+}
+
 LIBDLOGUTIL_EXPORT_API int dlogutil_entry_get_tag(const dlogutil_entry_s *entry, const char **tag)
 {
        CHECK_PARAM(entry);
        CHECK_PARAM(tag);
 
-       if (entry->len - (int) sizeof(dlogutil_entry_s) > LOG_MAX_PAYLOAD_SIZE
-       || entry->len - (int) sizeof(dlogutil_entry_s) < 2
-       || ((const char *)entry)[entry->len - 1] != '\0') {
+       if (!verfiy_entry(entry)) {
                *tag = NULL;
                return TIZEN_ERROR_NO_DATA;
        }
@@ -197,9 +230,7 @@ LIBDLOGUTIL_EXPORT_API int dlogutil_entry_get_message(const dlogutil_entry_s *en
        CHECK_PARAM(entry);
        CHECK_PARAM(msg);
 
-       if (entry->len - (int) sizeof(dlogutil_entry_s) > LOG_MAX_PAYLOAD_SIZE
-       || entry->len - (int) sizeof(dlogutil_entry_s) < 2
-       || ((const char *)entry)[entry->len - 1] != '\0') {
+       if (!verfiy_entry(entry)) {
                *msg = NULL;
                return TIZEN_ERROR_NO_DATA;
        }