qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 4/5] HACKING: add string management rules


From: Blue Swirl
Subject: [Qemu-devel] [PATCH 4/5] HACKING: add string management rules
Date: Sun, 15 Aug 2010 17:50:51 +0000

Add string management rules, somewhat like libvirt HACKING.

Signed-off-by: Blue Swirl <address@hidden>
---
 HACKING |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/HACKING b/HACKING
index e0342df..54bd2fd 100644
--- a/HACKING
+++ b/HACKING
@@ -79,3 +79,27 @@ qemu_vmalloc/qemu_memalign/qemu_vfree APIs.
 Memory allocated by qemu_vmalloc or qemu_memalign must be freed with
 qemu_vfree, since breaking this will cause problems on Win32 and user
 emulators.
+
+4. String manipulation
+
+Do not use the strncpy function.  According to the man page, it does
+*not* guarantee a NULL-terminated buffer, which makes it extremely dangerous
+to use.  Instead, use functionally equivalent function:
+void pstrcpy(char *buf, int buf_size, const char *str)
+
+Don't use strcat because it can't check for buffer overflows, but:
+char *pstrcat(char *buf, int buf_size, const char *s)
+
+The same limitation exists with sprintf and vsprintf, so use snprintf and
+vsnprintf.
+
+QEMU provides other useful string functions:
+int strstart(const char *str, const char *val, const char **ptr)
+int stristart(const char *str, const char *val, const char **ptr)
+int qemu_strnlen(const char *s, int max_len)
+
+There are also replacement character processing macros for isxyz and toxyz,
+so instead of e.g. isalnum you should use qemu_isalnum.
+
+Because of the memory management rules, you must use qemu_strdup/qemu_strndup
+instead of plain strdup/strndup.
-- 
1.6.2.4



reply via email to

[Prev in Thread] Current Thread [Next in Thread]