Skip to content

Commit 27f168c

Browse files
committed
Fix erroneous assertions
Since PHP strings are binary safe (i.e. they may contain NUL bytes), we must not assume that strlen()/wcslen() actually return the length of the string. Only if the given in_len is zero, it is safe to assert this.
1 parent 35c8058 commit 27f168c

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

win32/codepage.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ __forceinline static wchar_t *php_win32_cp_to_w_int(const char* in, size_t in_le
6363
}
6464

6565
assert(ret ? tmp_len == ret_len : 1);
66-
assert(ret ? wcslen(ret) == ret_len - 1 : 1);
66+
assert(ret && !in_len ? wcslen(ret) == ret_len - 1 : 1);
6767

6868
ret[ret_len-1] = L'\0';
6969

@@ -97,7 +97,10 @@ PW32CP wchar_t *php_win32_cp_conv_ascii_to_w(const char* in, size_t in_len, size
9797
{/*{{{*/
9898
wchar_t *ret = NULL;
9999
const char *idx = in, *end;
100-
100+
#if PHP_DEBUG
101+
size_t save_in_len = in_len;
102+
#endif
103+
101104
assert(in && in_len ? in[in_len] == '\0' : 1);
102105

103106
if (!in) {
@@ -154,7 +157,7 @@ PW32CP wchar_t *php_win32_cp_conv_ascii_to_w(const char* in, size_t in_len, size
154157
} while (i < in_len);
155158
ret[in_len] = L'\0';
156159

157-
assert(ret ? wcslen(ret) == in_len : 1);
160+
assert(ret && !save_in_len ? wcslen(ret) == in_len : 1);
158161

159162
if (PHP_WIN32_CP_IGNORE_LEN_P != out_len) {
160163
*out_len = in_len;
@@ -202,7 +205,7 @@ __forceinline static char *php_win32_cp_from_w_int(const wchar_t* in, size_t in_
202205
}
203206

204207
assert(target ? r == target_len : 1);
205-
assert(target ? strlen(target) == target_len - 1 : 1);
208+
assert(target && !in_len ? strlen(target) == target_len - 1 : 1);
206209

207210
target[target_len-1] = '\0';
208211

0 commit comments

Comments
 (0)