From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=43801 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OohLz-0005Ew-4q for qemu-devel@nongnu.org; Thu, 26 Aug 2010 14:39:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OohLx-0006xT-SP for qemu-devel@nongnu.org; Thu, 26 Aug 2010 14:39:11 -0400 Received: from mail-qw0-f45.google.com ([209.85.216.45]:44249) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OohLx-0006pV-Pm for qemu-devel@nongnu.org; Thu, 26 Aug 2010 14:39:09 -0400 Received: by mail-qw0-f45.google.com with SMTP id 5so1984947qwh.4 for ; Thu, 26 Aug 2010 11:39:09 -0700 (PDT) MIME-Version: 1.0 From: Blue Swirl Date: Thu, 26 Aug 2010 18:38:49 +0000 Message-ID: Content-Type: text/plain; charset=UTF-8 Subject: [Qemu-devel] [PATCH 4/5] HACKING: add string management rules List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel Add string management rules, somewhat like libvirt HACKING. Signed-off-by: Blue Swirl --- HACKING | 24 ++++++++++++++++++++++++ 1 files changed, 24 insertions(+), 0 deletions(-) diff --git a/HACKING b/HACKING index 554009e..a3d714f 100644 --- a/HACKING +++ b/HACKING @@ -83,3 +83,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