diff options
| author | Mark Whitley <markw@lineo.com> | 2001-01-24 00:18:13 +0000 |
|---|---|---|
| committer | Mark Whitley <markw@lineo.com> | 2001-01-24 00:18:13 +0000 |
| commit | a5b55ca8a935ccc04293fb5a94d362b0fa40ebb3 (patch) | |
| tree | f7940a03a23f4b3a561d1ee4b31bf11ae02b725a /docs | |
| parent | f1b0c77c229abb000e72c17f0d2f588d66af4061 (diff) | |
| download | busybox-w32-a5b55ca8a935ccc04293fb5a94d362b0fa40ebb3.tar.gz busybox-w32-a5b55ca8a935ccc04293fb5a94d362b0fa40ebb3.tar.bz2 busybox-w32-a5b55ca8a935ccc04293fb5a94d362b0fa40ebb3.zip | |
- Added a section on avoiding static buffers, per mailing list discussions
- Added a section on avoiding dangerous, overrun-prone string functions, per
mailing list comment by David Douthitt
- Added comment on how to search-and-replace in multiple files per (very old)
mailing list comment by Larry Doolittle.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/style-guide.txt | 92 |
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 | ||
| 206 | The exception to this rule are enums, macros, and constant variables which | 208 | Exceptions: |
| 207 | should 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 | ||
| 210 | Note: The Busybox codebase is very much a mixture of code gathered from a | 216 | Note: The Busybox codebase is very much a mixture of code gathered from a |
| 211 | variety of sources. This explains why the current codebase contains such a | 217 | variety 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 | |||
| 218 | script that can go through and convert variable names, left as an exercise for | 224 | script that can go through and convert variable names, left as an exercise for |
| 219 | the reader for now. | 225 | the reader for now. |
| 220 | 226 | ||
| 227 | For the time being, if you want to do a search-and-replace of a variable name | ||
| 228 | in 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 | ||
| 223 | Avoid The Preprocessor | 234 | Avoid The Preprocessor |
| @@ -355,6 +366,79 @@ perfect world, we would have a streq() function in the string library, but | |||
| 355 | that ain't the world we're living in. | 366 | that ain't the world we're living in. |
| 356 | 367 | ||
| 357 | 368 | ||
| 369 | Avoid Dangerous String Functions | ||
| 370 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 371 | |||
| 372 | Unfortunately, the way C handles strings makes them prone to overruns when | ||
| 373 | certain library functions are (mis)used. The following table offers a summary | ||
| 374 | of some of the more notorious troublemakers: | ||
| 375 | |||
| 376 | function overflows preferred | ||
| 377 | ---------------------------------------- | ||
| 378 | strcpy dest string strncpy | ||
| 379 | strcat dest string strncat | ||
| 380 | gets string it gets fgets | ||
| 381 | getwd buf string getcwd | ||
| 382 | [v]sprintf str buffer [v]snprintf | ||
| 383 | realpath path buffer use with pathconf | ||
| 384 | [vf]scanf its arguments just avoid it | ||
| 385 | |||
| 386 | |||
| 387 | The above is by no means a complete list. Be careful out there. | ||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | Avoid Big Static Buffers | ||
| 392 | ------------------------ | ||
| 393 | |||
| 394 | First, some background to put this discussion in context: Static buffers look | ||
| 395 | like 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 | |||
| 401 | The problem with these is that any time any busybox app is run, you pay a | ||
| 402 | memory penalty for this buffer, even if the applet that uses said buffer is | ||
| 403 | not 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 | |||
| 416 | However, this approach trades bss segment for text segment. Rather than | ||
| 417 | mallocing the buffers (and thus growing the text size), buffers can be | ||
| 418 | declared on the stack in the *_main() function and made available globally by | ||
| 419 | assigning 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 | |||
| 433 | Thus: | ||
| 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 | ||
| 359 | Miscellaneous Coding Guidelines | 443 | Miscellaneous Coding Guidelines |
| 360 | ------------------------------- | 444 | ------------------------------- |
