Constness of key in ngx_hash_find_

I was wondering if the “name” argument and also “hash” in the following
functions should be const

void *ngx_hash_find(ngx_hash_t *hash, ngx_uint_t key, u_char *name,
size_t len);
void *ngx_hash_find_wc_head(ngx_hash_wildcard_t *hwc, u_char *name,
size_t len);
void *ngx_hash_find_wc_tail(ngx_hash_wildcard_t *hwc, u_char *name,
size_t len);
void *ngx_hash_find_combined(ngx_hash_combined_t *hash, ngx_uint_t key,
u_char *name, size_t len);

If that happens, then I can ask for similar changes in
ngx_http_variables{c,h} :slight_smile:

On Sun, Oct 19, 2008 at 12:13:05AM +0530, Arvind Jayaprakash wrote:

u_char *name, size_t len);

If that happens, then I can ask for similar changes in
ngx_http_variables{c,h} :slight_smile:

Do you mean

-void *ngx_hash_find(ngx_hash_t *hash, …, u_char *name,
+void *ngx_hash_find(const ngx_hash_t *hash, …, const u_char *name,

?

But what will this improve ?

On Mon, Oct 20, 2008 at 05:56:24PM +0530, Arvind Jayaprakash wrote:

tells me that the function will not modify the contents of the variable
implementation.
OK, I will change this eventually.

Igor S. wrote:

Do you mean

-void *ngx_hash_find(ngx_hash_t *hash, …, u_char *name,
+void *ngx_hash_find(const ngx_hash_t *hash, …, const u_char *name,

But what will this improve ?

This is more of an API design/style question. When passing pointers, if
the function signature declares the argument to be “const type *ptr”, it
tells me that the function will not modify the contents of the variable
being passed. For eg: A hash lookup function should never need to modify
either the hash or the key being searched for.

If a function violates this contract, then it results in a compile time
error. If someone tries to forcibly discard the constness using a
typecast, it will result in a warning.

Having the const declarations thus gives people the assurance that the
values being passed will not be modified inside the function. Without
that, I am not sure if the value will get modified unless I read the
implementation.

Igor S. wrote:

Igor S. wrote:

Do you mean

-void *ngx_hash_find(ngx_hash_t *hash, …, u_char *name,
+void *ngx_hash_find(const ngx_hash_t *hash, …, const u_char *name,

But what will this improve ?

OK, I will change this eventually.

Here is my first set of changes: I’ve worked only on the ngx_string.*
files at this point and it seems to break nothing in terms of
compilation.

I’ve left out a few functions for the following reasons:
ngx_strlchr, ngx_strnstr, ngx_strstrn, ngx_strcasestrn return an
address in the input string; so they are not const
ngx_utf8_length, ngx_utf8_cpystrn seem to modify the input string (I
don’t know why a length function tries to modify the content of a
string)
ngx_unescape_uri again, I dont know why the source string is being
modified here.

If you find these patches ok, I’ll continue my work on rest of the code
base.