Skip to content

Commit bb267be

Browse files
committed
file_win32: fix file handling bugs in cio_file_win32.c
- cio_file_native_delete: Prevent deletion of open/mapped files (resource leak) - cio_file_native_sync: Fix NULL pointer access when syncing unmapped files (crash) - cio_file_native_map: Fix mapping size mismatch causing data corruption These bugs could cause crashes, resource leaks, and data corruption on Windows. Signed-off-by: Eduardo Silva <[email protected]>
1 parent 87a70cb commit bb267be

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/cio_file_win32.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ int cio_file_native_map(struct cio_file *cf, size_t map_size)
6767
{
6868
DWORD desired_protection;
6969
DWORD desired_access;
70+
size_t file_size;
71+
size_t actual_map_size;
72+
int ret;
7073

7174
if (cf == NULL) {
7275
return CIO_ERROR;
@@ -92,12 +95,39 @@ int cio_file_native_map(struct cio_file *cf, size_t map_size)
9295
return CIO_ERROR;
9396
}
9497

98+
/* Get current file size to ensure we don't map beyond it for read-only files */
99+
ret = cio_file_native_get_size(cf, &file_size);
100+
if (ret != CIO_OK) {
101+
return CIO_ERROR;
102+
}
103+
104+
/* For read-only files, we cannot map beyond the file size */
105+
/* For read-write files, if map_size > file_size, we should resize first */
106+
if (cf->flags & CIO_OPEN_RD) {
107+
if (map_size > file_size) {
108+
actual_map_size = file_size;
109+
}
110+
else {
111+
actual_map_size = map_size;
112+
}
113+
}
114+
else {
115+
/* For RW files, if map_size > file_size, resize the file first */
116+
if (map_size > file_size) {
117+
ret = cio_file_native_resize(cf, map_size);
118+
if (ret != CIO_OK) {
119+
return CIO_ERROR;
120+
}
121+
}
122+
actual_map_size = map_size;
123+
}
124+
95125
/* CreateFileMappingA requires size as two DWORDs (high and low) */
96-
/* Passing (0, 0) uses current file size, but we want map_size */
126+
/* Use actual_map_size to ensure consistency */
97127
cf->backing_mapping = CreateFileMappingA(cf->backing_file, NULL,
98128
desired_protection,
99-
(DWORD)(map_size >> 32),
100-
(DWORD)(map_size & 0xFFFFFFFFUL),
129+
(DWORD)(actual_map_size >> 32),
130+
(DWORD)(actual_map_size & 0xFFFFFFFFUL),
101131
NULL);
102132

103133
if (cf->backing_mapping == NULL) {
@@ -106,7 +136,7 @@ int cio_file_native_map(struct cio_file *cf, size_t map_size)
106136
return CIO_ERROR;
107137
}
108138

109-
cf->map = MapViewOfFile(cf->backing_mapping, desired_access, 0, 0, map_size);
139+
cf->map = MapViewOfFile(cf->backing_mapping, desired_access, 0, 0, actual_map_size);
110140

111141
if (cf->map == NULL) {
112142
cio_file_native_report_os_error();
@@ -118,7 +148,7 @@ int cio_file_native_map(struct cio_file *cf, size_t map_size)
118148
return CIO_ERROR;
119149
}
120150

121-
cf->alloc_size = map_size;
151+
cf->alloc_size = actual_map_size;
122152

123153
return CIO_OK;
124154
}

0 commit comments

Comments
 (0)