* [Qemu-devel] [PATCH 4/5] CODING_STYLE: add string management rules
@ 2010-08-12 17:50 Blue Swirl
0 siblings, 0 replies; only message in thread
From: Blue Swirl @ 2010-08-12 17:50 UTC (permalink / raw)
To: qemu-devel
Add string management rules, somewhat like libvirt HACKING.
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
---
CODING_STYLE | 24 ++++++++++++++++++++++++
1 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/CODING_STYLE b/CODING_STYLE
index 085c86f..b230a01 100644
--- a/CODING_STYLE
+++ b/CODING_STYLE
@@ -156,3 +156,27 @@ Use of the malloc/free/realloc/calloc APIs is not
allowed in the QEMU
codebase. Instead of these routines, use the replacement
qemu_malloc/qemu_mallocz/qemu_realloc/qemu_free or
qemu_vmalloc/qemu_memalign/qemu_vfree APIs.
+
+8. 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
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2010-08-12 17:51 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-12 17:50 [Qemu-devel] [PATCH 4/5] CODING_STYLE: add string management rules Blue Swirl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).