Changeset 988 for vendor/current/README.Coding
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/README.Coding
r740 r988 17 17 common and supported by tools and editors. 18 18 19 The basic style , also mentioned in prog_guide4.txt, is the Linux kernel19 The basic style for C code, also mentioned in prog_guide4.txt, is the Linux kernel 20 20 coding style (See Documentation/CodingStyle in the kernel source tree). This 21 21 closely matches what most Samba developers use already anyways, with a few 22 22 exceptions as mentioned below. 23 24 The coding style for Python code is documented in PEP8, 25 http://www.python.org/pep/pep8 (with spaces). 26 If you have ever worked on another free software Python project, you are 27 probably already familiar with it. 23 28 24 29 But to save you the trouble of reading the Linux kernel style guide, here … … 276 281 277 282 if (y < 10) { 278 z = malloc(sizeof(int) *y);279 if ( !z) {283 z = malloc(sizeof(int) * y); 284 if (z == NULL) { 280 285 ret = 1; 281 286 goto done; … … 286 291 287 292 done: 288 if (z ) {293 if (z != NULL) { 289 294 free(z); 290 295 } 291 296 292 297 return ret; 293 }294 295 296 Checking Pointer Values297 -----------------------298 299 When invoking functions that return pointer values, either of the following300 are acceptable. Use your best judgement and choose the more readable option.301 Remember that many other persons will review it:302 303 if ((x = malloc(sizeof(short)*10)) == NULL ) {304 fprintf(stderr, "Unable to alloc memory!\n");305 }306 307 or:308 309 x = malloc(sizeof(short)*10);310 if (!x) {311 fprintf(stderr, "Unable to alloc memory!\n");312 298 } 313 299 … … 335 321 but for new code, please don't do that anymore. 336 322 323 Initialize pointers 324 ------------------- 325 326 All pointer variables MUST be initialized to NULL. History has 327 demonstrated that uninitialized pointer variables have lead to various 328 bugs and security issues. 329 330 Pointers MUST be initialized even if the assignment directly follows 331 the declaration, like pointer2 in the example below, because the 332 instructions sequence may change over time. 333 334 Good Example: 335 336 char *pointer1 = NULL; 337 char *pointer2 = NULL; 338 339 pointer2 = some_func2(); 340 341 ... 342 343 pointer1 = some_func1(); 344 345 Bad Example: 346 347 char *pointer1; 348 char *pointer2; 349 350 pointer2 = some_func2(); 351 352 ... 353 354 pointer1 = some_func1(); 355 337 356 Make use of helper variables 338 357 ---------------------------- … … 344 363 Good Example: 345 364 346 char *name ;365 char *name = NULL; 347 366 348 367 name = get_some_name(); … … 360 379 ... 361 380 381 Please try to avoid passing function return values to if- or 382 while-conditions. The reason for this is better handling of code under a 383 debugger. 384 385 Good example: 386 387 x = malloc(sizeof(short)*10); 388 if (x == NULL) { 389 fprintf(stderr, "Unable to alloc memory!\n"); 390 } 391 392 Bad example: 393 394 if ((x = malloc(sizeof(short)*10)) == NULL ) { 395 fprintf(stderr, "Unable to alloc memory!\n"); 396 } 397 398 There are exceptions to this rule. One example is walking a data structure in 399 an iterator style: 400 401 while ((opt = poptGetNextOpt(pc)) != -1) { 402 ... do something with opt ... 403 } 404 405 But in general, please try to avoid this pattern. 406 407 408 Control-Flow changing macros 409 ---------------------------- 410 411 Macros like NT_STATUS_NOT_OK_RETURN that change control flow 412 (return/goto/etc) from within the macro are considered bad, because 413 they look like function calls that never change control flow. Please 414 do not use them in new code. 415 416 The only exception is the test code that depends repeated use of calls 417 like CHECK_STATUS, CHECK_VAL and others. 418 419 420 DEBUG statements 421 ---------------- 422 423 Use these following macros instead of DEBUG: 424 425 DBG_ERR log level 0 error conditions 426 DBG_WARNING log level 1 warning conditions 427 DBG_NOTICE log level 3 normal, but significant, condition 428 DBG_INFO log level 5 informational message 429 DBG_DEBUG log level 10 debug-level message 430 431 Example usage: 432 433 DBG_ERR("Memory allocation failed\n"); 434 DBG_DEBUG("Received %d bytes\n", count); 435 436 The messages from these macros are automatically prefixed with the 437 function name.
Note:
See TracChangeset
for help on using the changeset viewer.