[PATCH 03 of 12] Win32: non-ASCII directory names support in ngx_getcwd()

Sergey Kandaurov pluknet at nginx.com
Fri Feb 17 15:04:04 UTC 2023



> On 13 Jan 2023, at 01:35, Maxim Dounin <mdounin at mdounin.ru> wrote:
> 
> # HG changeset patch
> # User Maxim Dounin <mdounin at mdounin.ru>
> # Date 1673548916 -10800
> #      Thu Jan 12 21:41:56 2023 +0300
> # Node ID 7cf820c46860796cff91f53a5d2db669bb5b5a6c
> # Parent  d05c0adf5890aecc68ce8906ef19ca07502ed071
> Win32: non-ASCII directory names support in ngx_getcwd().
> 
> This makes it possible to start nginx without a prefix explicitly set
> in a directory with non-ASCII characters in it.
> 
> diff -r d05c0adf5890 -r 7cf820c46860 src/os/win32/ngx_files.c
> --- a/src/os/win32/ngx_files.c	Thu Jan 12 21:41:39 2023 +0300
> +++ b/src/os/win32/ngx_files.c	Thu Jan 12 21:41:56 2023 +0300
> @@ -428,6 +428,30 @@ ngx_realpath(u_char *path, u_char *resol
> }
> 
> 
> +size_t
> +ngx_getcwd(u_char *buf, size_t size)
> +{
> +    u_char   *p;
> +    size_t    n;
> +    u_short   utf16[NGX_MAX_PATH];
> +
> +    n = GetCurrentDirectoryW(NGX_MAX_PATH, utf16);
> +
> +    if (n == 0) {
> +        return 0;
> +    }
> +
> +    p = ngx_utf16_to_utf8(buf, utf16, &size, NULL);
> +

No error check may result in double-free, first freed
after (re-)allocation on NGX_EILSEQ, then as below.

> +    if (p != buf) {
> +        ngx_free(p);
> +        return 0;

Why return an error if (re-)allocation happened?
Sizes (calculated in 1-byte units) above NGX_MAX_PATH
seem perfectly valid with multibyte UTF-8.

> +    }
> +
> +    return size - 1;
> +}
> +
> +
> ngx_int_t
> ngx_open_dir(ngx_str_t *name, ngx_dir_t *dir)
> {
> diff -r d05c0adf5890 -r 7cf820c46860 src/os/win32/ngx_files.h
> --- a/src/os/win32/ngx_files.h	Thu Jan 12 21:41:39 2023 +0300
> +++ b/src/os/win32/ngx_files.h	Thu Jan 12 21:41:56 2023 +0300
> @@ -178,8 +178,12 @@ void ngx_close_file_mapping(ngx_file_map
> 
> u_char *ngx_realpath(u_char *path, u_char *resolved);
> #define ngx_realpath_n              ""
> -#define ngx_getcwd(buf, size)       GetCurrentDirectory(size, (char *) buf)
> +
> +
> +size_t ngx_getcwd(u_char *buf, size_t size);
> #define ngx_getcwd_n                "GetCurrentDirectory()"
> +
> +
> #define ngx_path_separator(c)       ((c) == '/' || (c) == '\\')
> 
> #define NGX_HAVE_MAX_PATH           1

-- 
Sergey Kandaurov


More information about the nginx-devel mailing list