[PATCH 02/11] Constified numerous function parameters.

Alejandro Colomar alx.manpages at gmail.com
Thu Jun 16 15:45:01 UTC 2022


Hi Andrew,

On 6/16/22 03:00, Andrew Clayton wrote:
> As was pointed out by the cppcheck[0] static code analysis utility we
> can mark numerous function parameters as 'const'. This acts as a hint to
> the compiler about our intentions and the compiler will tell us when we
> deviate from them.
> 
> [0]: https://cppcheck.sourceforge.io/

 From my side, as long as it compiles, and doesn't require any casts, 
`const` is always welcome.
I'm actually surprised that only 41 lines were changed with this.

Reviewed-by: Alejandro Colomar <alx.manpages at gmail.com>

(We don't use those in the commit logs, but just to state that I 
reviewed it).

Cheers,

Alex

> ---
>   src/nxt_conf.c            | 10 +++++-----
>   src/nxt_conf.h            |  6 +++---
>   src/nxt_http.h            |  5 +++--
>   src/nxt_http_parse.c      | 26 +++++++++++++-------------
>   src/nxt_http_parse.h      |  2 +-
>   src/nxt_http_request.c    | 12 ++++++------
>   src/nxt_http_static.c     |  4 ++--
>   src/nxt_mp.c              |  4 ++--
>   src/nxt_port_memory_int.h |  2 +-
>   src/nxt_port_socket.c     |  4 ++--
>   src/nxt_string.c          |  2 +-
>   src/nxt_string.h          |  2 +-
>   src/nxt_unit.c            |  5 +++--
>   13 files changed, 43 insertions(+), 41 deletions(-)
> 
> diff --git a/src/nxt_conf.c b/src/nxt_conf.c
> index 79e776a..a44b8c7 100644
> --- a/src/nxt_conf.c
> +++ b/src/nxt_conf.c
> @@ -102,7 +102,7 @@ typedef struct {
>   static nxt_int_t nxt_conf_path_next_token(nxt_conf_path_parse_t *parse,
>       nxt_str_t *token);
>   
> -static u_char *nxt_conf_json_skip_space(u_char *start, u_char *end);
> +static u_char *nxt_conf_json_skip_space(u_char *start, const u_char *end);
>   static u_char *nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value,
>       u_char *start, u_char *end, nxt_conf_json_error_t *error);
>   static u_char *nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value,
> @@ -266,7 +266,7 @@ nxt_conf_create_object(nxt_mp_t *mp, nxt_uint_t count)
>   
>   void
>   nxt_conf_set_member(nxt_conf_value_t *object, nxt_str_t *name,
> -    nxt_conf_value_t *value, uint32_t index)
> +    const nxt_conf_value_t *value, uint32_t index)
>   {
>       nxt_conf_object_member_t  *member;
>   
> @@ -367,7 +367,7 @@ nxt_conf_create_array(nxt_mp_t *mp, nxt_uint_t count)
>   
>   void
>   nxt_conf_set_element(nxt_conf_value_t *array, nxt_uint_t index,
> -    nxt_conf_value_t *value)
> +    const nxt_conf_value_t *value)
>   {
>       array->u.array->elements[index] = *value;
>   }
> @@ -1271,7 +1271,7 @@ nxt_conf_json_parse(nxt_mp_t *mp, u_char *start, u_char *end,
>   
>   
>   static u_char *
> -nxt_conf_json_skip_space(u_char *start, u_char *end)
> +nxt_conf_json_skip_space(u_char *start, const u_char *end)
>   {
>       u_char  *p, ch;
>   
> @@ -2605,7 +2605,7 @@ nxt_conf_json_escape(u_char *dst, u_char *src, size_t size)
>   
>   
>   void
> -nxt_conf_json_position(u_char *start, u_char *pos, nxt_uint_t *line,
> +nxt_conf_json_position(u_char *start, const u_char *pos, nxt_uint_t *line,
>       nxt_uint_t *column)
>   {
>       u_char      *p;
> diff --git a/src/nxt_conf.h b/src/nxt_conf.h
> index cfbc599..46effac 100644
> --- a/src/nxt_conf.h
> +++ b/src/nxt_conf.h
> @@ -109,7 +109,7 @@ size_t nxt_conf_json_length(nxt_conf_value_t *value,
>       nxt_conf_json_pretty_t *pretty);
>   u_char *nxt_conf_json_print(u_char *p, nxt_conf_value_t *value,
>       nxt_conf_json_pretty_t *pretty);
> -void nxt_conf_json_position(u_char *start, u_char *pos, nxt_uint_t *line,
> +void nxt_conf_json_position(u_char *start, const u_char *pos, nxt_uint_t *line,
>       nxt_uint_t *column);
>   
>   nxt_int_t nxt_conf_validate(nxt_conf_validation_t *vldt);
> @@ -125,7 +125,7 @@ NXT_EXPORT uint8_t nxt_conf_get_boolean(nxt_conf_value_t *value);
>   NXT_EXPORT nxt_uint_t nxt_conf_object_members_count(nxt_conf_value_t *value);
>   nxt_conf_value_t *nxt_conf_create_object(nxt_mp_t *mp, nxt_uint_t count);
>   void nxt_conf_set_member(nxt_conf_value_t *object, nxt_str_t *name,
> -    nxt_conf_value_t *value, uint32_t index);
> +    const nxt_conf_value_t *value, uint32_t index);
>   void nxt_conf_set_member_string(nxt_conf_value_t *object, nxt_str_t *name,
>       nxt_str_t *value, uint32_t index);
>   nxt_int_t nxt_conf_set_member_string_dup(nxt_conf_value_t *object, nxt_mp_t *mp,
> @@ -137,7 +137,7 @@ void nxt_conf_set_member_null(nxt_conf_value_t *object, nxt_str_t *name,
>   
>   nxt_conf_value_t *nxt_conf_create_array(nxt_mp_t *mp, nxt_uint_t count);
>   void nxt_conf_set_element(nxt_conf_value_t *array, nxt_uint_t index,
> -    nxt_conf_value_t *value);
> +    const nxt_conf_value_t *value);
>   nxt_int_t nxt_conf_set_element_string_dup(nxt_conf_value_t *array, nxt_mp_t *mp,
>       nxt_uint_t index, nxt_str_t *value);
>   NXT_EXPORT nxt_uint_t nxt_conf_array_elements_count(nxt_conf_value_t *value);
> diff --git a/src/nxt_http.h b/src/nxt_http.h
> index 37c2732..218d453 100644
> --- a/src/nxt_http.h
> +++ b/src/nxt_http.h
> @@ -370,8 +370,9 @@ nxt_int_t nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
>       nxt_http_action_t *action, nxt_http_action_conf_t *acf);
>   nxt_int_t nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash);
>   nxt_int_t nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash,
> -    nxt_str_t *exten, nxt_str_t *type);
> -nxt_str_t *nxt_http_static_mtype_get(nxt_lvlhsh_t *hash, nxt_str_t *exten);
> +    const nxt_str_t *exten, nxt_str_t *type);
> +nxt_str_t *nxt_http_static_mtype_get(nxt_lvlhsh_t *hash,
> +    const nxt_str_t *exten);
>   
>   nxt_http_action_t *nxt_http_application_handler(nxt_task_t *task,
>       nxt_http_request_t *r, nxt_http_action_t *action);
> diff --git a/src/nxt_http_parse.c b/src/nxt_http_parse.c
> index 1ab6cc9..1bb4291 100644
> --- a/src/nxt_http_parse.c
> +++ b/src/nxt_http_parse.c
> @@ -8,16 +8,16 @@
>   
>   
>   static nxt_int_t nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp,
> -    u_char **pos, u_char *end);
> +    u_char **pos, const u_char *end);
>   static nxt_int_t nxt_http_parse_request_line(nxt_http_request_parse_t *rp,
> -    u_char **pos, u_char *end);
> +    u_char **pos, const u_char *end);
>   static nxt_int_t nxt_http_parse_field_name(nxt_http_request_parse_t *rp,
> -    u_char **pos, u_char *end);
> +    u_char **pos, const u_char *end);
>   static nxt_int_t nxt_http_parse_field_value(nxt_http_request_parse_t *rp,
> -    u_char **pos, u_char *end);
> -static u_char *nxt_http_lookup_field_end(u_char *p, u_char *end);
> +    u_char **pos, const u_char *end);
> +static u_char *nxt_http_lookup_field_end(u_char *p, const u_char *end);
>   static nxt_int_t nxt_http_parse_field_end(nxt_http_request_parse_t *rp,
> -    u_char **pos, u_char *end);
> +    u_char **pos, const u_char *end);
>   
>   static nxt_int_t nxt_http_parse_complex_target(nxt_http_request_parse_t *rp);
>   
> @@ -62,7 +62,7 @@ static const uint8_t  nxt_http_target_chars[256] nxt_aligned(64) = {
>   
>   
>   nxt_inline nxt_http_target_traps_e
> -nxt_http_parse_target(u_char **pos, u_char *end)
> +nxt_http_parse_target(u_char **pos, const u_char *end)
>   {
>       u_char      *p;
>       nxt_uint_t  trap;
> @@ -158,7 +158,7 @@ nxt_http_parse_fields(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b)
>   
>   static nxt_int_t
>   nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos,
> -    u_char *end)
> +    const u_char *end)
>   {
>       u_char                   *p, ch, *after_slash, *args;
>       nxt_int_t                rc;
> @@ -479,7 +479,7 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos,
>   
>   static nxt_int_t
>   nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp, u_char **pos,
> -    u_char *end)
> +    const u_char *end)
>   {
>       u_char  *p, ch;
>   
> @@ -517,7 +517,7 @@ nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp, u_char **pos,
>   
>   static nxt_int_t
>   nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos,
> -    u_char *end)
> +    const u_char *end)
>   {
>       u_char    *p, c;
>       size_t    len;
> @@ -624,7 +624,7 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos,
>   
>   static nxt_int_t
>   nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos,
> -    u_char *end)
> +    const u_char *end)
>   {
>       u_char  *p, *start, ch;
>       size_t  len;
> @@ -704,7 +704,7 @@ nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos,
>   
>   
>   static u_char *
> -nxt_http_lookup_field_end(u_char *p, u_char *end)
> +nxt_http_lookup_field_end(u_char *p, const u_char *end)
>   {
>       while (nxt_fast_path(end - p >= 16)) {
>   
> @@ -771,7 +771,7 @@ nxt_http_lookup_field_end(u_char *p, u_char *end)
>   
>   static nxt_int_t
>   nxt_http_parse_field_end(nxt_http_request_parse_t *rp, u_char **pos,
> -    u_char *end)
> +    const u_char *end)
>   {
>       u_char            *p;
>       nxt_http_field_t  *field;
> diff --git a/src/nxt_http_parse.h b/src/nxt_http_parse.h
> index 3cd9bd1..2b71446 100644
> --- a/src/nxt_http_parse.h
> +++ b/src/nxt_http_parse.h
> @@ -35,7 +35,7 @@ typedef union {
>   
>   struct nxt_http_request_parse_s {
>       nxt_int_t                 (*handler)(nxt_http_request_parse_t *rp,
> -                                         u_char **pos, u_char *end);
> +                                         u_char **pos, const u_char *end);
>   
>       nxt_str_t                 method;
>   
> diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c
> index 0eacf62..df17fd2 100644
> --- a/src/nxt_http_request.c
> +++ b/src/nxt_http_request.c
> @@ -26,11 +26,11 @@ static u_char *nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now,
>   
>   static nxt_http_name_value_t *nxt_http_argument(nxt_array_t *array,
>       u_char *name, size_t name_length, uint32_t hash, u_char *start,
> -    u_char *end);
> +    const u_char *end);
>   static nxt_int_t nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start,
> -    u_char *end);
> +    const u_char *end);
>   static nxt_http_name_value_t *nxt_http_cookie(nxt_array_t *array, u_char *name,
> -    size_t name_length, u_char *start, u_char *end);
> +    size_t name_length, u_char *start, const u_char *end);
>   
>   
>   #define NXT_HTTP_COOKIE_HASH                                                  \
> @@ -876,7 +876,7 @@ nxt_http_arguments_parse(nxt_http_request_t *r)
>   
>   static nxt_http_name_value_t *
>   nxt_http_argument(nxt_array_t *array, u_char *name, size_t name_length,
> -    uint32_t hash, u_char *start, u_char *end)
> +    uint32_t hash, u_char *start, const u_char *end)
>   {
>       size_t                 length;
>       nxt_http_name_value_t  *nv;
> @@ -945,7 +945,7 @@ nxt_http_cookies_parse(nxt_http_request_t *r)
>   
>   
>   static nxt_int_t
> -nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, u_char *end)
> +nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, const u_char *end)
>   {
>       size_t                 name_length;
>       u_char                 c, *p, *name;
> @@ -994,7 +994,7 @@ nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, u_char *end)
>   
>   static nxt_http_name_value_t *
>   nxt_http_cookie(nxt_array_t *array, u_char *name, size_t name_length,
> -    u_char *start, u_char *end)
> +    u_char *start, const u_char *end)
>   {
>       u_char                 c, *p;
>       uint32_t               hash;
> diff --git a/src/nxt_http_static.c b/src/nxt_http_static.c
> index 61dd0cb..7c7991f 100644
> --- a/src/nxt_http_static.c
> +++ b/src/nxt_http_static.c
> @@ -1023,7 +1023,7 @@ typedef struct {
>   
>   nxt_int_t
>   nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash,
> -    nxt_str_t *exten, nxt_str_t *type)
> +    const nxt_str_t *exten, nxt_str_t *type)
>   {
>       nxt_lvlhsh_query_t       lhq;
>       nxt_http_static_mtype_t  *mtype;
> @@ -1048,7 +1048,7 @@ nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash,
>   
>   
>   nxt_str_t *
> -nxt_http_static_mtype_get(nxt_lvlhsh_t *hash, nxt_str_t *exten)
> +nxt_http_static_mtype_get(nxt_lvlhsh_t *hash, const nxt_str_t *exten)
>   {
>       nxt_lvlhsh_query_t       lhq;
>       nxt_http_static_mtype_t  *mtype;
> diff --git a/src/nxt_mp.c b/src/nxt_mp.c
> index d0de2c0..2bd8cde 100644
> --- a/src/nxt_mp.c
> +++ b/src/nxt_mp.c
> @@ -155,7 +155,7 @@ static void *nxt_mp_alloc_large(nxt_mp_t *mp, size_t alignment, size_t size,
>       nxt_bool_t freeable);
>   static intptr_t nxt_mp_rbtree_compare(nxt_rbtree_node_t *node1,
>       nxt_rbtree_node_t *node2);
> -static nxt_mp_block_t *nxt_mp_find_block(nxt_rbtree_t *tree, u_char *p);
> +static nxt_mp_block_t *nxt_mp_find_block(nxt_rbtree_t *tree, const u_char *p);
>   static const char *nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster,
>       u_char *p);
>   
> @@ -830,7 +830,7 @@ nxt_mp_free(nxt_mp_t *mp, void *p)
>   
>   
>   static nxt_mp_block_t *
> -nxt_mp_find_block(nxt_rbtree_t *tree, u_char *p)
> +nxt_mp_find_block(nxt_rbtree_t *tree, const u_char *p)
>   {
>       nxt_mp_block_t     *block;
>       nxt_rbtree_node_t  *node, *sentinel;
> diff --git a/src/nxt_port_memory_int.h b/src/nxt_port_memory_int.h
> index c84615d..21a05b1 100644
> --- a/src/nxt_port_memory_int.h
> +++ b/src/nxt_port_memory_int.h
> @@ -100,7 +100,7 @@ nxt_inline void
>   nxt_port_mmap_set_chunk_free(nxt_free_map_t *m, nxt_chunk_id_t c);
>   
>   nxt_inline nxt_chunk_id_t
> -nxt_port_mmap_chunk_id(nxt_port_mmap_header_t *hdr, u_char *p)
> +nxt_port_mmap_chunk_id(nxt_port_mmap_header_t *hdr, const u_char *p)
>   {
>       u_char  *mm_start;
>   
> diff --git a/src/nxt_port_socket.c b/src/nxt_port_socket.c
> index 2a51dfb..5752d5a 100644
> --- a/src/nxt_port_socket.c
> +++ b/src/nxt_port_socket.c
> @@ -19,7 +19,7 @@ static uint8_t nxt_port_enqueue_buf(nxt_task_t *task, nxt_port_msg_t *pm,
>       void *qbuf, nxt_buf_t *b);
>   static nxt_int_t nxt_port_msg_chk_insert(nxt_task_t *task, nxt_port_t *port,
>       nxt_port_send_msg_t *msg);
> -static nxt_port_send_msg_t *nxt_port_msg_alloc(nxt_port_send_msg_t *m);
> +static nxt_port_send_msg_t *nxt_port_msg_alloc(const nxt_port_send_msg_t *m);
>   static void nxt_port_write_handler(nxt_task_t *task, void *obj, void *data);
>   static nxt_port_send_msg_t *nxt_port_msg_first(nxt_port_t *port);
>   nxt_inline void nxt_port_msg_close_fd(nxt_port_send_msg_t *msg);
> @@ -332,7 +332,7 @@ nxt_port_msg_chk_insert(nxt_task_t *task, nxt_port_t *port,
>   
>   
>   static nxt_port_send_msg_t *
> -nxt_port_msg_alloc(nxt_port_send_msg_t *m)
> +nxt_port_msg_alloc(const nxt_port_send_msg_t *m)
>   {
>       nxt_port_send_msg_t  *msg;
>   
> diff --git a/src/nxt_string.c b/src/nxt_string.c
> index b7aef79..4d89c23 100644
> --- a/src/nxt_string.c
> +++ b/src/nxt_string.c
> @@ -338,7 +338,7 @@ nxt_rmemstrn(const u_char *s, const u_char *end, const char *ss, size_t length)
>   
>   
>   size_t
> -nxt_str_strip(u_char *start, u_char *end)
> +nxt_str_strip(const u_char *start, u_char *end)
>   {
>       u_char  *p;
>   
> diff --git a/src/nxt_string.h b/src/nxt_string.h
> index 80cdbb5..a8673c6 100644
> --- a/src/nxt_string.h
> +++ b/src/nxt_string.h
> @@ -96,7 +96,7 @@ NXT_EXPORT u_char *nxt_memcasestrn(const u_char *s, const u_char *end,
>       const char *ss, size_t length);
>   NXT_EXPORT u_char *nxt_rmemstrn(const u_char *s, const u_char *end,
>       const char *ss, size_t length);
> -NXT_EXPORT size_t nxt_str_strip(u_char *start, u_char *end);
> +NXT_EXPORT size_t nxt_str_strip(const u_char *start, u_char *end);
>   
>   
>   typedef struct {
> diff --git a/src/nxt_unit.c b/src/nxt_unit.c
> index f183ac6..4be0906 100644
> --- a/src/nxt_unit.c
> +++ b/src/nxt_unit.c
> @@ -196,7 +196,8 @@ static int nxt_unit_request_hash_add(nxt_unit_ctx_t *ctx,
>   static nxt_unit_request_info_t *nxt_unit_request_hash_find(
>       nxt_unit_ctx_t *ctx, uint32_t stream, int remove);
>   
> -static char * nxt_unit_snprint_prefix(char *p, char *end, pid_t pid, int level);
> +static char * nxt_unit_snprint_prefix(char *p, const char *end, pid_t pid,
> +    int level);
>   static void *nxt_unit_lvlhsh_alloc(void *data, size_t size);
>   static void nxt_unit_lvlhsh_free(void *data, void *p);
>   static int nxt_unit_memcasecmp(const void *p1, const void *p2, size_t length);
> @@ -6666,7 +6667,7 @@ static const char * nxt_unit_log_levels[] = {
>   
>   
>   static char *
> -nxt_unit_snprint_prefix(char *p, char *end, pid_t pid, int level)
> +nxt_unit_snprint_prefix(char *p, const char *end, pid_t pid, int level)
>   {
>       struct tm        tm;
>       struct timespec  ts;

-- 
Alejandro Colomar
<http://www.alejandro-colomar.es/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://mailman.nginx.org/pipermail/unit/attachments/20220616/57f93437/attachment.bin>


More information about the unit mailing list