aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/style-guide.txt92
1 files changed, 88 insertions, 4 deletions
diff --git a/docs/style-guide.txt b/docs/style-guide.txt
index 374a822b2..9a3b10207 100644
--- a/docs/style-guide.txt
+++ b/docs/style-guide.txt
@@ -195,17 +195,23 @@ because it looks like whitespace; using lower-case is easy on the eyes.
195 195
196 hitList 196 hitList
197 TotalChars 197 TotalChars
198 szFileName (blech) 198 szFileName
199 pf_Nfol_TriState
199 200
200 Preferred: 201 Preferred:
201 202
202 hit_list 203 hit_list
203 total_chars 204 total_chars
204 file_name 205 file_name
206 sensible_name
205 207
206The exception to this rule are enums, macros, and constant variables which 208Exceptions:
207should all be in upper-case, with words optionally seperatedy by underscores 209
208(i.e. FIFOTYPE, ISBLKDEV()). 210 - Enums, macros, and constant variables should all be in upper-case with
211 words optionally seperatedy by underscores (i.e. FIFOTYPE, ISBLKDEV()).
212
213 - Nobody is going to get mad at you for using 'pvar' as the name of a
214 variable that is a pointer to 'var'.
209 215
210Note: The Busybox codebase is very much a mixture of code gathered from a 216Note: The Busybox codebase is very much a mixture of code gathered from a
211variety of sources. This explains why the current codebase contains such a 217variety of sources. This explains why the current codebase contains such a
@@ -218,6 +224,11 @@ low priority task. Perhaps in the future we will include some magical Perl
218script that can go through and convert variable names, left as an exercise for 224script that can go through and convert variable names, left as an exercise for
219the reader for now. 225the reader for now.
220 226
227For the time being, if you want to do a search-and-replace of a variable name
228in different files, do the following in the busybox directory:
229
230 $ perl -pi -e 's/\bOldVar\b/new_var/g' *.[ch]
231
221 232
222 233
223Avoid The Preprocessor 234Avoid The Preprocessor
@@ -355,6 +366,79 @@ perfect world, we would have a streq() function in the string library, but
355that ain't the world we're living in. 366that ain't the world we're living in.
356 367
357 368
369Avoid Dangerous String Functions
370~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
371
372Unfortunately, the way C handles strings makes them prone to overruns when
373certain library functions are (mis)used. The following table offers a summary
374of some of the more notorious troublemakers:
375
376function overflows preferred
377----------------------------------------
378strcpy dest string strncpy
379strcat dest string strncat
380gets string it gets fgets
381getwd buf string getcwd
382[v]sprintf str buffer [v]snprintf
383realpath path buffer use with pathconf
384[vf]scanf its arguments just avoid it
385
386
387The above is by no means a complete list. Be careful out there.
388
389
390
391Avoid Big Static Buffers
392------------------------
393
394First, some background to put this discussion in context: Static buffers look
395like this in code:
396
397 /* in a .c file outside any functions */
398 static char *buffer[BUFSIZ]; /* happily used by any function in this file,
399 but ick! big! */
400
401The problem with these is that any time any busybox app is run, you pay a
402memory penalty for this buffer, even if the applet that uses said buffer is
403not run. This can be fixed, thusly:
404
405 static char *buffer
406 ...
407 other_func()
408 {
409 strcpy(buffer, lotsa_chars); /* happily uses global *buffer */
410 ...
411 foo_main()
412 {
413 buffer = xmalloc(sizeof(char)*BUFSIZ);
414 ...
415
416However, this approach trades bss segment for text segment. Rather than
417mallocing the buffers (and thus growing the text size), buffers can be
418declared on the stack in the *_main() function and made available globally by
419assigning them to a global pointer thusly:
420
421 static char *pbuffer
422 ...
423 other_func()
424 {
425 strcpy(pbuffer, lotsa_chars); /* happily uses global *pbuffer */
426 ...
427 foo_main()
428 {
429 char *buffer[BUFSIZ]; /* declared locally, on stack */
430 pbuffer = buffer; /* but available globally */
431 ...
432
433Thus:
434 - global static buffers are eliminated
435 - we don't grow the text segment as much because no malloc() call is made;
436 memory is automatically allocated on the stack when execution context
437 enters the function. (We still grow text a little bit because of the
438 assignment, but that's cheap compared to a function call.)
439 - the buffer is still available globally via the pointer
440
441
358 442
359Miscellaneous Coding Guidelines 443Miscellaneous Coding Guidelines
360------------------------------- 444-------------------------------