wez Sat Oct 19 09:11:48 2002 EDT
Modified files:
/php4/main streams.c php_streams.h
/php4/ext/standard file.c
Log:
made fgets() binary safe.
php_stream_gets is now a macro which calls php_stream_get_line. The latter
has an option argument to return the number of bytes in the line.
Functions like fgetcsv(), fgetss() can be made binary safe by calling
php_stream_get_line directly.
# HEADS UP: You will need to make clean after updating your CVS, as the
# binary signature has changed.
Index: php4/main/streams.c
diff -u php4/main/streams.c:1.111 php4/main/streams.c:1.112
--- php4/main/streams.c:1.111 Sat Oct 19 06:34:10 2002
+++ php4/main/streams.c Sat Oct 19 09:11:48 2002
@@ -20,7 +20,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: streams.c,v 1.111 2002/10/19 10:34:10 wez Exp $ */
+/* $Id: streams.c,v 1.112 2002/10/19 13:11:48 wez Exp $ */
#define _GNU_SOURCE
#include "php.h"
@@ -668,7 +668,8 @@
/* If buf == NULL, the buffer will be allocated automatically and will be of an
* appropriate length to hold the line, regardless of the line length, memory
* permitting */
-PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRMLS_DC)
+PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen,
+ size_t *returned_len TSRMLS_DC)
{
size_t avail = 0;
size_t current_buf_size = 0;
@@ -772,6 +773,8 @@
}
buf[0] = '\0';
+ if (returned_len)
+ *returned_len = total_copied;
return bufstart;
}
Index: php4/main/php_streams.h
diff -u php4/main/php_streams.h:1.55 php4/main/php_streams.h:1.56
--- php4/main/php_streams.h:1.55 Fri Oct 18 16:39:49 2002
+++ php4/main/php_streams.h Sat Oct 19 09:11:48 2002
@@ -359,8 +359,10 @@
PHPAPI int _php_stream_flush(php_stream *stream, int closing TSRMLS_DC);
#define php_stream_flush(stream) _php_stream_flush((stream), 0 TSRMLS_CC)
-PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRMLS_DC);
-#define php_stream_gets(stream, buf, maxlen) _php_stream_gets((stream), (buf),
(maxlen) TSRMLS_CC)
+PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen,
+size_t *returned_len TSRMLS_DC);
+#define php_stream_gets(stream, buf, maxlen) _php_stream_get_line((stream), (buf),
+(maxlen), NULL TSRMLS_CC)
+
+#define php_stream_get_line(stream, buf, maxlen, retlen)
+_php_stream_get_line((stream), (buf), (maxlen), (retlen) TSRMLS_CC)
/* CAREFUL! this is equivalent to puts NOT fputs! */
PHPAPI int _php_stream_puts(php_stream *stream, char *buf TSRMLS_DC);
Index: php4/ext/standard/file.c
diff -u php4/ext/standard/file.c:1.272 php4/ext/standard/file.c:1.273
--- php4/ext/standard/file.c:1.272 Fri Oct 18 16:39:49 2002
+++ php4/ext/standard/file.c Sat Oct 19 09:11:48 2002
@@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: file.c,v 1.272 2002/10/18 20:39:49 iliaa Exp $ */
+/* $Id: file.c,v 1.273 2002/10/19 13:11:48 wez Exp $ */
/* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
@@ -1239,6 +1239,7 @@
int len;
char *buf = NULL;
int argc = ZEND_NUM_ARGS();
+ size_t line_len = 0;
php_stream *stream;
if (argc<1 || argc>2 || zend_get_parameters_ex(argc, &arg1, &arg2) == FAILURE)
{
@@ -1249,7 +1250,7 @@
if (argc == 1) {
/* ask streams to give us a buffer of an appropriate size */
- buf = php_stream_gets(stream, NULL, 0);
+ buf = php_stream_get_line(stream, NULL, 0, &line_len);
if (buf == NULL)
goto exit_failed;
} else if (argc > 1) {
@@ -1262,19 +1263,19 @@
}
buf = ecalloc(len + 1, sizeof(char));
- if (php_stream_gets(stream, buf, len) == NULL)
+ if (php_stream_get_line(stream, buf, len, &line_len) == NULL)
goto exit_failed;
}
if (PG(magic_quotes_runtime)) {
- Z_STRVAL_P(return_value) = php_addslashes(buf, 0,
&Z_STRLEN_P(return_value), 1 TSRMLS_CC);
+ Z_STRVAL_P(return_value) = php_addslashes(buf, line_len,
+&Z_STRLEN_P(return_value), 1 TSRMLS_CC);
Z_TYPE_P(return_value) = IS_STRING;
} else {
- ZVAL_STRING(return_value, buf, 0);
+ ZVAL_STRINGL(return_value, buf, line_len, 0);
/* resize buffer if it's much larger than the result.
* Only needed if the user requested a buffer size. */
if (argc > 1 && Z_STRLEN_P(return_value) < len / 2) {
- Z_STRVAL_P(return_value) = erealloc(buf,
Z_STRLEN_P(return_value) + 1);
+ Z_STRVAL_P(return_value) = erealloc(buf, line_len + 1);
}
}
return;
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php