Skip to content

Commit 5f49264

Browse files
committed
Fix fseek undefined behavior
1 parent 03ca089 commit 5f49264

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

main/streams/memory.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static int php_stream_memory_seek(php_stream *stream, zend_off_t offset, int whe
128128
switch(whence) {
129129
case SEEK_CUR:
130130
if (offset < 0) {
131-
if (ms->fpos < (size_t)(-offset)) {
131+
if (ms->fpos < -(size_t)offset) {
132132
ms->fpos = 0;
133133
*newoffs = -1;
134134
return -1;
@@ -165,7 +165,7 @@ static int php_stream_memory_seek(php_stream *stream, zend_off_t offset, int whe
165165
stream->eof = 0;
166166
stream->fatal_error = 0;
167167
return 0;
168-
} else if (ZSTR_LEN(ms->data) < (size_t)(-offset)) {
168+
} else if (ZSTR_LEN(ms->data) < -(size_t)offset) {
169169
ms->fpos = 0;
170170
*newoffs = -1;
171171
return -1;

tests/basic/bug20964.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Bug #20964 fseek with PHP_INT_MIN on php://memory
3+
--FILE--
4+
<?php
5+
$stream = fopen('php://memory', 'r+');
6+
7+
$result = fseek($stream, PHP_INT_MIN, SEEK_END);
8+
var_dump($result);
9+
?>
10+
--EXPECTF--
11+
int(%d)

0 commit comments

Comments
 (0)