From da5c73352402df3a04fa2b2d205167486ff36403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Tomko?= Date: Fri, 18 Oct 2019 23:14:40 +0200 Subject: [PATCH] docs: hacking: document preferred strdup alternatives MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Recommend g_str(n)dup instead of VIR_STRDUP. Signed-off-by: Ján Tomko Reviewed-by: Daniel P. Berrangé Reviewed-by: Andrea Bolognani --- docs/hacking.html.in | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/docs/hacking.html.in b/docs/hacking.html.in index d60ea5d120..60dc2f18e3 100644 --- a/docs/hacking.html.in +++ b/docs/hacking.html.in @@ -1275,18 +1275,14 @@ BAD:

-  VIR_STRDUP(char *dst, const char *src);
-  VIR_STRNDUP(char *dst, const char *src, size_t n);
+    dst = g_strdup(src);
+    dst = g_strndup(src, n);
 

- You should avoid using strdup or strndup directly as they do not report - out-of-memory error, and do not allow a NULL source. Use - VIR_STRDUP or VIR_STRNDUP macros instead, which return 0 for - NULL source, 1 for successful copy, and -1 for allocation - failure with the error already reported. In very - specific cases, when you don't want to report the out-of-memory error, you - can use VIR_STRDUP_QUIET or VIR_STRNDUP_QUIET, but such usage is very rare - and usually considered a flaw. + You should avoid using strdup or strndup directly as they do not handle + out-of-memory errors, and do not allow a NULL source. + Use g_strdup and g_strndup from GLib which + abort on OOM and handle NULL source by returning NULL.

Variable length string buffer