Discussion:
[PATCH] Win32: Don't discards const qualifiers for WriteFile
Kentaro Hayashi
2018-11-27 05:07:45 UTC
Permalink
Hi,
It seems that there is a mismatch between ngx_write_fd and WriteFile
prototype.
I've attached a patch for it.

# HG changeset patch
# User ***@clear-code.com
# Date 1543294459 -32400
# Tue Nov 27 13:54:19 2018 +0900
# Branch use-const-void-pointer
# Node ID 04809b6012bd8a744e3beded6fc385cdb3f788ee
# Parent a7ff19afbb14795fef14f599a304d0ad21052b70
Win32: Don't discards const qualifiers for WriteFile

The 2nd argument of ngx_write_fd() is declared as
void pointer instead of const void pointer.
It means that void pointer is passed to WriteFile which accepts
LPCVOID pointer, instead of LPVOID. Thus const qualifier isn't respected.

ref.
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-writefile

It causes a compile warning.

diff -r a7ff19afbb14 -r 04809b6012bd src/os/win32/ngx_files.c
--- a/src/os/win32/ngx_files.c Mon Nov 26 18:29:56 2018 +0300
+++ b/src/os/win32/ngx_files.c Tue Nov 27 13:54:19 2018 +0900
@@ -175,7 +175,7 @@


ssize_t
-ngx_write_fd(ngx_fd_t fd, void *buf, size_t size)
+ngx_write_fd(ngx_fd_t fd, const void *buf, size_t size)
{
u_long n;

diff -r a7ff19afbb14 -r 04809b6012bd src/os/win32/ngx_files.h
--- a/src/os/win32/ngx_files.h Mon Nov 26 18:29:56 2018 +0300
+++ b/src/os/win32/ngx_files.h Tue Nov 27 13:54:19 2018 +0900
@@ -107,7 +107,7 @@
#define ngx_read_fd_n "ReadFile()"


-ssize_t ngx_write_fd(ngx_fd_t fd, void *buf, size_t size);
+ssize_t ngx_write_fd(ngx_fd_t fd, const void *buf, size_t size);
#define ngx_write_fd_n "WriteFile()"
--
Kentaro Hayashi <***@gmail.com>
Maxim Dounin
2018-11-27 13:27:06 UTC
Permalink
Hello!
Post by Kentaro Hayashi
Hi,
It seems that there is a mismatch between ngx_write_fd and WriteFile
prototype.
I've attached a patch for it.
# HG changeset patch
# Date 1543294459 -32400
# Tue Nov 27 13:54:19 2018 +0900
# Branch use-const-void-pointer
# Node ID 04809b6012bd8a744e3beded6fc385cdb3f788ee
# Parent a7ff19afbb14795fef14f599a304d0ad21052b70
Win32: Don't discards const qualifiers for WriteFile
The 2nd argument of ngx_write_fd() is declared as
void pointer instead of const void pointer.
It means that void pointer is passed to WriteFile which accepts
LPCVOID pointer, instead of LPVOID. Thus const qualifier isn't respected.
ref.
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-writefile
It causes a compile warning.
Could you please elaborate a bit more about the compile warning?
Which compiler you use, how the warning looks like?

The const qualifier is only important when it's removed from a
variable - that is, when you pass something declared as "const"
into a function with non-const argument. That is, when you pass a
constant variable into a function which does not promise to handle
it as a constant variable. The opposite is perfectly fine, so you
can safely pass a non-constant variable into a function which
promises not to change the variable. From this point of view
there are no problems in ngx_write_fd(), it simply passes a
non-constant string to a function which promises not to change
this string.

[...]
--
Maxim Dounin
http://mdounin.ru/
Loading...