]> granicus.if.org Git - php/commitdiff
Fix socket read returning bad values.
authorStanislav Malyshev <stas@php.net>
Tue, 12 Dec 2000 16:56:34 +0000 (16:56 +0000)
committerStanislav Malyshev <stas@php.net>
Tue, 12 Dec 2000 16:56:34 +0000 (16:56 +0000)
ext/sockets/sockets.c

index 43017c70bb61cad492ae549db0126c6cd764a35f..c9464d4620f664e5b5cf46710e86d366d5b619a0 100644 (file)
@@ -683,7 +683,7 @@ PHP_FUNCTION(read)
                read_function = (int (*)(int, void *, int)) php_read;
        }
 
-       tmpbuf = emalloc(Z_LVAL_PP(length)*sizeof(char));
+       tmpbuf = emalloc((Z_LVAL_PP(length)+1)*sizeof(char));
        if (tmpbuf == NULL) {
                php_error(E_WARNING, "Couldn't allocate memory from %s()", get_active_function_name());
                RETURN_FALSE;
@@ -692,19 +692,9 @@ PHP_FUNCTION(read)
        ret = (*read_function)(Z_LVAL_PP(fd), tmpbuf, Z_LVAL_PP(length));
        
        if (ret >= 0) {
-               if (Z_STRLEN_PP(buf) > 0) {
-                       efree(Z_STRVAL_PP(buf));
-               }
-               if (ZEND_NUM_ARGS() == 4 && Z_LVAL_PP(binary) != PHP_NORMAL_READ) {
-                       Z_STRVAL_PP(buf) = emalloc(ret);
-                       memcpy(Z_STRVAL_PP(buf), tmpbuf, ret);
-                       Z_STRLEN_PP(buf) = ret;
-               } else {
-                       Z_STRVAL_PP(buf) = estrndup(tmpbuf,strlen(tmpbuf));
-                       Z_STRLEN_PP(buf) = strlen(tmpbuf);
-                       ret = strlen(tmpbuf);
-               }
-               efree(tmpbuf);
+               tmpbuf[ret] = '\0';
+               Z_STRVAL_PP(buf) = erealloc(tmpbuf, ret+1);
+               Z_STRLEN_PP(buf) = ret;
 
                RETURN_LONG(ret);
        } else {