summaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorSusant Palai <spalai@redhat.com>2018-08-03 17:43:28 +0530
committerAmar Tumballi <amarts@redhat.com>2018-11-26 05:39:52 +0000
commita2829d1151cabd13764e271a07568b9c90261a76 (patch)
tree0f92ad2f1ebae790b7b9c2ce74b8ecadb8a961af /doc
parentc97938e0508d991f44fd6a02216ecae120bfcac8 (diff)
coding-standard: memset before string functions similar to sprintf/snprintf etc
String functions like sprintf/snprintf/vprintf etc terminates the string with a null character. This makes meset redundant and also incurs some performance penalty. Also added a comment about using GF_MALLOC instead of GF_CALLOC for allocating buffer for strings, targeted by such functions. Fixes: bz#1193929 Change-Id: I4f40d4fd5342b70983936119606c6c9f352c303c Signed-off-by: Susant Palai <spalai@redhat.com>
Diffstat (limited to 'doc')
-rw-r--r--doc/developer-guide/coding-standard.md31
1 files changed, 31 insertions, 0 deletions
diff --git a/doc/developer-guide/coding-standard.md b/doc/developer-guide/coding-standard.md
index 7c1beddc2d5..a8aa49cc2da 100644
--- a/doc/developer-guide/coding-standard.md
+++ b/doc/developer-guide/coding-standard.md
@@ -366,6 +366,37 @@ strcpy (entry_path, real_path);
strncpy (entry_path, real_path, entry_path_len);
```
+Do not use memset prior to sprintf/snprintf/vsnprintf etc...
+------------------------------------------------------------
+snprintf(and other similar string functions) terminates the buffer with a
+'\0'(null character). Hence, there is no need to do a memset before using
+snprintf. (Of course you need to account one extra byte for the null character
+in your allocation).
+
+Note: Similarly if you are doing pre-memory allocation for the buffer, use
+GF_MALLOC instead of GF_CALLOC, since the later is bit costlier.
+
+*Bad:*
+
+```
+char buffer[x];
+memset (buffer, 0, x);
+bytes_read = snprintf (buffer, sizeof buffer, "bad standard");
+```
+
+*Good:*
+```
+char buffer[x];
+bytes_read = snprintf (buffer, sizeof (buffer), "good standard");
+```
+
+And it is always to good initialize the char array if the string is static.
+
+E.g.
+```
+char buffer[] = "good standard";
+```
+
No dead or commented code
-------------------------