]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
datum, mem, str: add helper functions to steal pointers
authorDaiki Ueno <ueno@gnu.org>
Tue, 21 Jan 2025 22:45:46 +0000 (07:45 +0900)
committerDaiki Ueno <ueno@gnu.org>
Sat, 25 Jan 2025 00:50:05 +0000 (09:50 +0900)
This introduces 3 new inline functions, namely _gnutls_steal_datum,
_gnutls_steal_buffer, and _gnutls_steal_pointer, to return a copy of
data structure and reset the original pointer. Those would enable to
return a populated data structure upon success; otherwise free the
partially filled data structure in a single code path, e.g.,

```c
  gnutls_datum_t tmp_result = { NULL, 0 };

  // Calculate tmp_result
  ...
  if (error)
    goto cleanup;

  // Propagate tmp_result to *result
  *result = _gnutls_steal_datum(&tmp_result);

cleanup:
  _gnutls_free_datum(&tmp_result);
  return ret;
```

Signed-off-by: Daiki Ueno <ueno@gnu.org>
lib/datum.h
lib/mem.h
lib/str.h

index 1ae62e3d11c3a71bed6450325a1b1699dee808a8..082aebb0f14990e92186f5fa91e21000c8678142 100644 (file)
@@ -68,4 +68,15 @@ inline static ATTRIBUTE_NONNULL() void _gnutls_free_key_datum(
        dat->size = 0;
 }
 
+inline static ATTRIBUTE_NONNULL() gnutls_datum_t
+       _gnutls_steal_datum(gnutls_datum_t *src)
+{
+       gnutls_datum_t dst = *src;
+
+       src->data = NULL;
+       src->size = 0;
+
+       return dst;
+}
+
 #endif /* GNUTLS_LIB_DATUM_H */
index 899c6bb7ddd989c534a283b5fe6a263f8795e85d..dba676ae79290894bbebd1796cf520c318fcc3e4 100644 (file)
--- a/lib/mem.h
+++ b/lib/mem.h
@@ -33,6 +33,8 @@
 #include <valgrind/memcheck.h>
 #endif
 
+#include "attribute.h"
+
 /* These realloc functions will return ptr if size==0, and will free
  * the ptr if the new allocation failed.
  */
@@ -78,4 +80,11 @@ static inline void _gnutls_memory_mark_defined(void *addr, size_t size)
 #endif
 }
 
+static inline ATTRIBUTE_NONNULL() void *_gnutls_steal_pointer(void **src)
+{
+       void *dst = *src;
+       *src = NULL;
+       return dst;
+}
+
 #endif /* GNUTLS_LIB_MEM_H */
index 7ac598d2c3137d0113916342209f8a97dc245e1f..bc4272ca8489bf4f01c08d0951ac5e95f42298e1 100644 (file)
--- a/lib/str.h
+++ b/lib/str.h
@@ -30,6 +30,7 @@
 #include "datum.h"
 #include <c-ctype.h>
 #include "errors.h"
+#include "attribute.h"
 
 #ifdef HAVE_DCGETTEXT
 #include "gettext.h"
@@ -97,6 +98,19 @@ inline static void _gnutls_buffer_reset(gnutls_buffer_st *buf)
        buf->length = 0;
 }
 
+inline static ATTRIBUTE_NONNULL() gnutls_buffer_st
+       _gnutls_steal_buffer(gnutls_buffer_st *src)
+{
+       gnutls_buffer_st dst = *src;
+
+       src->allocd = NULL;
+       src->data = NULL;
+       src->max_length = 0;
+       src->length = 0;
+
+       return dst;
+}
+
 int _gnutls_buffer_resize(gnutls_buffer_st *, size_t new_size);
 
 int _gnutls_buffer_append_str(gnutls_buffer_st *, const char *str);
@@ -170,15 +184,8 @@ int _gnutls_buffer_append_escape(gnutls_buffer_st *dest, const void *data,
                                 size_t data_size, const char *invalid_chars);
 int _gnutls_buffer_unescape(gnutls_buffer_st *dest);
 
-#ifndef __attribute__
-/* This feature is available in gcc versions 2.5 and later.  */
-#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
-#define __attribute__(Spec) /* empty */
-#endif
-#endif
-
 int _gnutls_buffer_append_printf(gnutls_buffer_st *dest, const char *fmt, ...)
-       __attribute__((format(printf, 2, 3)));
+       ATTRIBUTE_FORMAT((printf, 2, 3));
 
 void _gnutls_buffer_hexprint(gnutls_buffer_st *str, const void *data,
                             size_t len);