Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

STYLE.md 6.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # BoringSSL Style Guide
  2. BoringSSL usually follows the
  3. [Google C++ style guide](https://google-styleguide.googlecode.com/svn/trunk/cppguide.html).
  4. The rest of this document describes differences and clarifications on
  5. top of the base guide.
  6. ## Legacy code
  7. As a derivative of OpenSSL, BoringSSL contains a lot of legacy code that
  8. does not follow this style guide. Particularly where public API is
  9. concerned, balance consistency within a module with the benefits of a
  10. given rule. Module-wide deviations on naming should be respected while
  11. integer and return value conventions take precedence over consistency.
  12. Some modules have seen few changes, so they still retain the original
  13. indentation style for now. When editing these, try to retain the
  14. original style. For Emacs, `doc/c-indentation.el` from OpenSSL may be
  15. helpful in this.
  16. ## Language
  17. The majority of the project is in C, so C++-specific rules in the
  18. Google style guide do not apply. Support for C99 features depends on
  19. our target platforms. Typically, Chromium's target MSVC is the most
  20. restrictive.
  21. Variable declarations in the middle of a function are allowed.
  22. Comments should be `/* C-style */` for consistency.
  23. When declaration pointer types, `*` should be placed next to the variable
  24. name, not the type. So
  25. uint8_t *ptr;
  26. not
  27. uint8_t* ptr;
  28. Rather than `malloc()` and `free()`, use the wrappers `OPENSSL_malloc()`
  29. and `OPENSSL_free()`. Use the standard C `assert()` function freely.
  30. For new constants, prefer enums when the values are sequential and typed
  31. constants for flags. If adding values to an existing set of `#define`s,
  32. continue with `#define`.
  33. ## Formatting
  34. Single-statement blocks are not allowed. All conditions and loops must
  35. use braces:
  36. if (foo) {
  37. do_something();
  38. }
  39. not
  40. if (foo)
  41. do_something();
  42. ## Integers
  43. Prefer using explicitly-sized integers where appropriate rather than
  44. generic C ones. For instance, to represent a byte, use `uint8_t`, not
  45. `unsigned char`. Likewise, represent a two-byte field as `uint16_t`, not
  46. `unsigned short`.
  47. Sizes are represented as `size_t`.
  48. Within a struct that is retained across the lifetime of an SSL
  49. connection, if bounds of a size are known and it's easy, use a smaller
  50. integer type like `uint8_t`. This is a "free" connection footprint
  51. optimization for servers. Don't make code significantly more complex for
  52. it, and do still check the bounds when passing in and out of the
  53. struct. This narrowing should not propagate to local variables and
  54. function parameters.
  55. When doing arithmetic, account for overflow conditions.
  56. Except with platform APIs, do not use `ssize_t`. MSVC lacks it, and
  57. prefer out-of-band error signaling for `size_t` (see Return values).
  58. ## Naming
  59. Follow Google naming conventions in C++ files. In C files, use the
  60. following naming conventions for consistency with existing OpenSSL and C
  61. styles:
  62. Define structs with typedef named `TYPE_NAME`. The corresponding struct
  63. should be named `struct type_name_st`.
  64. Name public functions as `MODULE_function_name`, unless the module
  65. already uses a different naming scheme for legacy reasons. The module
  66. name should be a type name if the function is a method of a particular
  67. type.
  68. Some types are allocated within the library while others are initialized
  69. into a struct allocated by the caller, often on the stack. Name these
  70. functions `TYPE_NAME_new`/`TYPE_NAME_free` and
  71. `TYPE_NAME_init`/`TYPE_NAME_cleanup`, respectively. All `TYPE_NAME_free`
  72. functions must do nothing on `NULL` input.
  73. If a variable is the length of a pointer value, it has the suffix
  74. `_len`. An output parameter is named `out` or has an `out_` prefix. For
  75. instance, For instance:
  76. uint8_t *out,
  77. size_t *out_len,
  78. const uint8_t *in,
  79. size_t in_len,
  80. Name public headers like `include/openssl/evp.h` with header guards like
  81. `OPENSSL_HEADER_EVP_H`. Name internal headers like
  82. `crypto/ec/internal.h` with header guards like
  83. `OPENSSL_HEADER_EC_INTERNAL_H`.
  84. Name enums like `enum unix_hacker_t`. For instance:
  85. enum should_free_handshake_buffer_t {
  86. free_handshake_buffer,
  87. dont_free_handshake_buffer,
  88. };
  89. ## Return values
  90. As even `malloc` may fail in BoringSSL, the vast majority of functions
  91. will have a failure case. Functions should return `int` with one on
  92. success and zero on error. Do not overload the return value to both
  93. signal success/failure and output an integer. For example:
  94. OPENSSL_EXPORT int CBS_get_u16(CBS *cbs, uint16_t *out);
  95. If a function needs more than a true/false result code, define an enum
  96. rather than arbitrarily assigning meaning to int values.
  97. If a function outputs a pointer to an object on success and there are no
  98. other outputs, return the pointer directly and `NULL` on error.
  99. ## Parameters
  100. Where not constrained by legacy code, parameter order should be:
  101. 1. context parameters
  102. 2. output parameters
  103. 3. input parameters
  104. For example,
  105. /* CBB_add_asn sets |*out_contents| to a |CBB| into which the contents of an
  106. * ASN.1 object can be written. The |tag| argument will be used as the tag for
  107. * the object. It returns one on success or zero on error. */
  108. OPENSSL_EXPORT int CBB_add_asn1(CBB *cbb, CBB *out_contents, uint8_t tag);
  109. ## Documentation
  110. All public symbols must have a documentation comment in their header
  111. file. The style is based on that of Go. The first sentence begins with
  112. the symbol name, optionally prefixed with "A" or "An". Apart from the
  113. initial mention of symbol, references to other symbols or parameter
  114. names should be surrounded by |pipes|.
  115. Documentation should be concise but completely describe the exposed
  116. behavior of the function. Pay special note to success/failure behaviors
  117. and caller obligations on object lifetimes. If this sacrifices
  118. conciseness, consider simplifying the function's behavior.
  119. /* EVP_DigestVerifyUpdate appends |len| bytes from |data| to the data which
  120. * will be verified by |EVP_DigestVerifyFinal|. It returns one on success and
  121. * zero otherwise. */
  122. OPENSSL_EXPORT int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data,
  123. size_t len);
  124. Explicitly mention any surprising edge cases or deviations from common
  125. return value patterns in legacy functions.
  126. /* RSA_private_encrypt encrypts |flen| bytes from |from| with the private key in
  127. * |rsa| and writes the encrypted data to |to|. The |to| buffer must have at
  128. * least |RSA_size| bytes of space. It returns the number of bytes written, or
  129. * -1 on error. The |padding| argument must be one of the |RSA_*_PADDING|
  130. * values. If in doubt, |RSA_PKCS1_PADDING| is the most common.
  131. *
  132. * WARNING: this function is dangerous because it breaks the usual return value
  133. * convention. Use |RSA_sign_raw| instead. */
  134. OPENSSL_EXPORT int RSA_private_encrypt(int flen, const uint8_t *from,
  135. uint8_t *to, RSA *rsa, int padding);
  136. Document private functions in their `internal.h` header or, if static,
  137. where defined.