ngx_reopen_files() assumes null termination

Maxim Dounin mdounin at mdounin.ru
Tue Mar 11 12:12:35 UTC 2014


Hello!

On Tue, Mar 11, 2014 at 02:32:52PM +1100, Andrew Punch wrote:

> I just noticed that ngx_reopen_files() treats the file name as a null
> terminated string but there is no guarantee that a ngx_str_t will be null
> terminated.

While ngx_str_t strings aren't required to be null-terminated, in 
some cases terminating null is required, and file names is one of 
such cases (because file names must be null-terminated for 
syscalls).

There are also some cases where ngx_str_t strings are guaranteed 
to be null-terminated - e.g., arguments of configuration 
directives as seen during configuration parsing are 
null-terminated.

Usually there are easy ways to explicitly request null-termination 
where it may be required but nog guaranteed.  E.g., complex values 
have "zero" flag, which may be set during compilation and results 
in trailing zero added to values returned at runtime.  See 
auth_basic_user_file implementation which uses it.

> I noticed in ngx_string.h that the ngx_string() macro sets the length to be
> exclusive of the null termination character, however the null termination
> would still be present at s.data[s.len]
> 
> However ngx_pstrdup() in ngx_string.c only copies the size of s.len - so if
> there is a null character at s.data[s.len] it will not be copied.
> 
> Could you please help clarify the correct way to handle the strings?

Trailing null should be handled explicitly when needed.

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx-devel mailing list