Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:10451 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38589 invoked by uid 1010); 14 Jun 2004 18:26:59 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 38563 invoked by uid 1007); 14 Jun 2004 18:26:59 -0000 Message-ID: <20040614182658.38550.qmail@pb1.pair.com> To: internals@lists.php.net References: <40CDD0EC.5070905@php-tools.net> <20040614171119.24785.qmail@pb1.pair.com> <40CDE75C.3010502@php-tools.net> Date: Mon, 14 Jun 2004 11:26:58 -0700 Lines: 42 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 X-Posted-By: 169.229.135.175 Subject: Re: [PHP-DEV] Re: Need help with streams From: pollita@php.net ("Sara Golemon") > I'm currently using: > > php_stream_write(stream, Z_STRVAL_P(*data), 30); > > As the string is terminated with a zero-byte, it already works, but it > would be better to fill the unused space with zero-bytes. > Actually, that'll also open the door to a potential segfault since you may overrun the allocated block of memory. Try: /* Not binary safe, but depending on your data that may be okay */ if (Z_STRLEN_PP(data) > 30) { php_stream_write(stream, Z_STRVAL_PP(data), 30); } else { php_stream_printf(stream TSRMLS_CC, "%-30s", Z_STRVAL_PP(data)); } or: /* This is binary safe */ if (Z_STRLEN_PP(data) > 30) { php_stream_write(stream, Z_STRVAL_PP(data), 30); } else { php_stream_write(stream, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); /* That's 30 spaces in that string constant */ php_stream_write(stream, " ", 30 - Z_STRLEN_PP(data)); } -Sara P.S. - As you might have noticed in the above code Z_STRVAL_PP(data) is the same as Z_STRVAL_P(*data). The same is true of the Z_LVAL_P(*data) you used in your previous question. I don't recall if using the _PP is dictated by the coding standards, but it's certainly in line with established convention. You'll probably find your code becomes more readable by others who are accostomed to the PHP Core if you stick to the secondary indirection macros.