Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

All great discoveries are made by mistake. -- Young


devel / comp.lang.c / Re: reading entire input using getdelim

SubjectAuthor
* reading entire input using getdelimOğuz
+* Re: reading entire input using getdelimKeith Thompson
|`* Re: reading entire input using getdelimOğuz
| `* Re: reading entire input using getdelimBen Bacarisse
|  `* Re: reading entire input using getdelimOğuz
|   `- Re: reading entire input using getdelimKaz Kylheku
`* Re: reading entire input using getdelimStefan Ram
 +- Re: reading entire input using getdelimKenny McCormack
 +- Re: reading entire input using getdelimKaz Kylheku
 `* Re: reading entire input using getdelimStefan Ram
  `- Re: reading entire input using getdelimKenny McCormack

1
reading entire input using getdelim

<tg3k99$3k87$1@oguzismail.eternal-september.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23218&group=comp.lang.c#23218

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!oguzismail.eternal-september.org!.POSTED!not-for-mail
From: oguzisma...@gmail.com (Oğuz)
Newsgroups: comp.lang.c
Subject: reading entire input using getdelim
Date: Sat, 17 Sep 2022 08:00:23 +0300
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <tg3k99$3k87$1@oguzismail.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 17 Sep 2022 05:00:25 -0000 (UTC)
Injection-Info: oguzismail.eternal-september.org; posting-host="fdeab5d648489235ec304dbbeb148cf9";
logging-data="119047"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/UcpZmSl9RKzYz6cco2qNIkBUUpl27zbs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:XQA4rCB/7+lyHJYYB+mG+weRiWA=
Content-Language: en-US
 by: Oğuz - Sat, 17 Sep 2022 05:00 UTC

I'm writing a program that needs to read its input in entirety before
processing it. And since its input is expected to be text (i.e. no NULs
embedded), it does it like this:

char *buf = NULL;
size_t n = 0;
if (getdelim(&buf, &n, 0, stdin) == -1)
exit(1);

This seems to work just fine, but feels too good to be true. Am I
missing something? Is there anything wrong with it?

Re: reading entire input using getdelim

<87y1uifugo.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23219&group=comp.lang.c#23219

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: reading entire input using getdelim
Date: Fri, 16 Sep 2022 22:20:07 -0700
Organization: None to speak of
Lines: 33
Message-ID: <87y1uifugo.fsf@nosuchdomain.example.com>
References: <tg3k99$3k87$1@oguzismail.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader01.eternal-september.org; posting-host="d0308493c6059990f0cb58e715a20e2d";
logging-data="119815"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Y/0Es5jzc+LrnBrvSEe1S"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:pKb0gyraGhm2T1myRBHo9TU3Xw4=
sha1:fCEX6H9BT8dnOvKQmqqKKuJU0V8=
 by: Keith Thompson - Sat, 17 Sep 2022 05:20 UTC

Oğuz <oguzismailuysal@gmail.com> writes:
> I'm writing a program that needs to read its input in entirety before
> processing it. And since its input is expected to be text (i.e. no
> NULs embedded), it does it like this:
>
> char *buf = NULL;
> size_t n = 0;
> if (getdelim(&buf, &n, 0, stdin) == -1)
> exit(1);
>
> This seems to work just fine, but feels too good to be true. Am I
> missing something? Is there anything wrong with it?

It's POSIX-specific, so it's not 100% portable. If there does happen to
be a null character in the input, it will quietly drop everything after
it. It would be cleaner to have a function that does the same thing
without a delimiter, so it reads everything up to end-of-file, but I'm
not aware that POSIX has such a function (though you could roll your
own).

An implementation is likely to use realloc() internally, which might
waste some memory if the input file is very large.

And if the input is empty, it returns -1.

It's arguably an abuse of getdelim(), and the special treatment of null
characters in the input *could* an issue, but I don't see any other
problems with it.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: reading entire input using getdelim

<tg444a$4put$1@oguzismail.eternal-september.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23220&group=comp.lang.c#23220

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!reader01.eternal-september.org!oguzismail.eternal-september.org!.POSTED!not-for-mail
From: oguzisma...@gmail.com (Oğuz)
Newsgroups: comp.lang.c
Subject: Re: reading entire input using getdelim
Date: Sat, 17 Sep 2022 12:30:48 +0300
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <tg444a$4put$1@oguzismail.eternal-september.org>
References: <tg3k99$3k87$1@oguzismail.eternal-september.org>
<87y1uifugo.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 17 Sep 2022 09:30:50 -0000 (UTC)
Injection-Info: oguzismail.eternal-september.org; posting-host="fdeab5d648489235ec304dbbeb148cf9";
logging-data="157661"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18J6nw5QugDFZ1+qlo2jmxvPXFexAV4Vo8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:j63/MXgsGnFHROGyVbfIxXIg8l4=
In-Reply-To: <87y1uifugo.fsf@nosuchdomain.example.com>
Content-Language: en-US
 by: Oğuz - Sat, 17 Sep 2022 09:30 UTC

On 9/17/22 8:20 AM, Keith Thompson wrote:
> It's POSIX-specific, so it's not 100% portable. If there does happen to
> be a null character in the input, it will quietly drop everything after
> it.

The rest of the program doesn't expect a NUL byte either, should have
mentioned that in OP. getdelim returns the number of bytes read into the
buffer so I can make it so that it doesn't quietly drop everything after
a NUL but fail the program instead.

> And if the input is empty, it returns -1.

Didn't notice that, thanks. I guess I can detect empty input by setting
errno to 0 before calling getdelim and testing if it changed if getdelim
returns -1.

Re: reading entire input using getdelim

<slurp-20220917131018@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23221&group=comp.lang.c#23221

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news.szaf.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: reading entire input using getdelim
Date: 17 Sep 2022 12:13:20 GMT
Organization: Stefan Ram
Lines: 72
Expires: 1 Sep 2023 11:59:58 GMT
Message-ID: <slurp-20220917131018@ram.dialup.fu-berlin.de>
References: <tg3k99$3k87$1@oguzismail.eternal-september.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de WJwJp/cFGDuzZxw85lZtXwMmw9B+Llts4OAn55m5Xa0xK/
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Sat, 17 Sep 2022 12:13 UTC

=?UTF-8?B?T8SfdXo=?= <oguzismailuysal@gmail.com> writes:
>This seems to work just fine, but feels too good to be true. Am I
>missing something? Is there anything wrong with it?

Here's my attempt at it with only standard C functions.
This is the first code that worked, written today and
not carefully reviewed!

#include <stdio.h> /* printf */
#include <stdlib.h> /* malloc, remalloc */

/* Append a char to *buff and reallocate if necessary.
If insufficient memory, free the buffer and set it to 0. */
void append
( unsigned char * *buff,
size_t *size,
size_t *offset,
unsigned char const c )
{ if( *offset >= *size )
{ size_t newsize = 2 *( *size );
unsigned char * newbuff = realloc( *buff, newsize );
if( !newbuff )
{ free( *buff );
*buff = 0; }
else
{ *buff = newbuff;
*size = newsize; }}
if( *buff )*( *buff + (*offset)++ )= c; }

/* Slurp a file into a buffer.
If read error or insufficient memory free the buffer and set it to 0. */
void slurp2
( FILE * const file,
unsigned char * *buff,
size_t *size,
size_t *offset )
{ while( !feof( file ))
{ if( ferror( file )){ free( buff ); return; }
int const i = fgetc( file );
if( !*buff )return;
if( i < 0 )return;
append( buff, size, offset,( unsigned char )i ); }}

/* Slurp a file into a buffer of the given initial size.
Return the buffer or 0 on error. */
unsigned char * slurp1( FILE * const file,
size_t *size,
size_t *offset )
{ unsigned char *buff = malloc( *size );
slurp2( file, &buff, size, offset );
fclose( file );
return buff; }

/* Slurp a file of the given filename into a buffer.
Require the filename, initial buffer, and an offset
which must 0. Return the buffer or 0 on error. */
unsigned char * slurp( char const * const filename,
size_t *size,
size_t *offset )
{ FILE * file = fopen( filename, "rb" );
return file? slurp1( file, size, offset ): 0; }

int main( void )
{ char const * const filename = "main.c";
size_t size = 1024;
size_t offset = 0;
unsigned char * const address = slurp( filename, &size, &offset );
if( address )
{ printf( "%.*s",( int )size, ( char const * const )address );
free( address ); }}

Re: reading entire input using getdelim

<87zgeykvzo.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23222&group=comp.lang.c#23222

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: reading entire input using getdelim
Date: Sat, 17 Sep 2022 13:48:11 +0100
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <87zgeykvzo.fsf@bsb.me.uk>
References: <tg3k99$3k87$1@oguzismail.eternal-september.org>
<87y1uifugo.fsf@nosuchdomain.example.com>
<tg444a$4put$1@oguzismail.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader01.eternal-september.org; posting-host="566edb7cc89d5bab63df1ca7f1a3ade2";
logging-data="186960"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Y22tUCR6Lfa+uwJ0r58riINq13l/7F/A="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:jUZg4xHOQ3BAz//HJeKPYBHOR6o=
sha1:VW83ekhK0Tv/F7q8/i5PfojnGYE=
X-BSB-Auth: 1.916d8e6c72481b6839de.20220917134811BST.87zgeykvzo.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 17 Sep 2022 12:48 UTC

Oğuz <oguzismailuysal@gmail.com> writes:

> On 9/17/22 8:20 AM, Keith Thompson wrote:
<cut>
>> And if the input is empty, it returns -1.
>
> Didn't notice that, thanks. I guess I can detect empty input by
> setting errno to 0 before calling getdelim and testing if it changed
> if getdelim returns -1.

You can use ferror() to distinguish between the two.

Beware of malicious input as there is no way to stop the function from
trying to allocate enough memory for a giant file.

--
Ben.

Re: reading entire input using getdelim

<tg4hkd$5u2j$1@oguzismail.eternal-september.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23223&group=comp.lang.c#23223

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!oguzismail.eternal-september.org!.POSTED!not-for-mail
From: oguzisma...@gmail.com (Oğuz)
Newsgroups: comp.lang.c
Subject: Re: reading entire input using getdelim
Date: Sat, 17 Sep 2022 16:21:13 +0300
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <tg4hkd$5u2j$1@oguzismail.eternal-september.org>
References: <tg3k99$3k87$1@oguzismail.eternal-september.org>
<87y1uifugo.fsf@nosuchdomain.example.com>
<tg444a$4put$1@oguzismail.eternal-september.org> <87zgeykvzo.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 17 Sep 2022 13:21:17 -0000 (UTC)
Injection-Info: oguzismail.eternal-september.org; posting-host="e21753c3494232efe19ef94ec00df4e9";
logging-data="194643"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/F6GpfYMzSg208Y8W1EiJS5KW2ttn9pwM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:vmWgPTaWc/J3vsNCTcLarIeXlMw=
In-Reply-To: <87zgeykvzo.fsf@bsb.me.uk>
Content-Language: en-US
 by: Oğuz - Sat, 17 Sep 2022 13:21 UTC

On 9/17/22 3:48 PM, Ben Bacarisse wrote:> You can use ferror() to
distinguish between the two

Ah, that's better. Thank you

>
> Beware of malicious input as there is no way to stop the function from
> trying to allocate enough memory for a giant file.
>

Yes, I wouldn't use this in a production setting

Re: reading entire input using getdelim

<tg4k5l$1jl46$2@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23224&group=comp.lang.c#23224

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: Re: reading entire input using getdelim
Date: Sat, 17 Sep 2022 14:04:37 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tg4k5l$1jl46$2@news.xmission.com>
References: <tg3k99$3k87$1@oguzismail.eternal-september.org> <slurp-20220917131018@ram.dialup.fu-berlin.de>
Injection-Date: Sat, 17 Sep 2022 14:04:37 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1692806"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 17 Sep 2022 14:04 UTC

In article <slurp-20220917131018@ram.dialup.fu-berlin.de>,
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>=?UTF-8?B?T8SfdXo=?= <oguzismailuysal@gmail.com> writes:
>>This seems to work just fine, but feels too good to be true. Am I
>>missing something? Is there anything wrong with it?
>
> Here's my attempt at it with only standard C functions.
> This is the first code that worked, written today and
> not carefully reviewed!

Anyone can "roll their own". That's not the point of this thread.
(And zillions of CLC posters have done so, over the years... Add your name
to the list [if it isn't there already])

The whole point of this thread is to ask if the built-in one is up to the
task. It seems to me the basic answer is "Yes".

--
Trump - the President for the rest of us.

https://www.youtube.com/watch?v=JSkUJKgdcoE

Re: reading entire input using getdelim

<20220917102445.183@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23226&group=comp.lang.c#23226

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: reading entire input using getdelim
Date: Sat, 17 Sep 2022 17:42:47 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <20220917102445.183@kylheku.com>
References: <tg3k99$3k87$1@oguzismail.eternal-september.org>
<87y1uifugo.fsf@nosuchdomain.example.com>
<tg444a$4put$1@oguzismail.eternal-september.org> <87zgeykvzo.fsf@bsb.me.uk>
<tg4hkd$5u2j$1@oguzismail.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 17 Sep 2022 17:42:47 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="cc03025c6c81b54fd7c9b754e7249e85";
logging-data="236814"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Xx/Qy9U+eyyMbVcYuBQOuQxGaeHFi5eo="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:wSpUtKLsuVJdYE33iFS0OCP9OGQ=
 by: Kaz Kylheku - Sat, 17 Sep 2022 17:42 UTC

On 2022-09-17, Oğuz <oguzismailuysal@gmail.com> wrote:
> On 9/17/22 3:48 PM, Ben Bacarisse wrote:> You can use ferror() to
> distinguish between the two
>
> Ah, that's better. Thank you
>
>>
>> Beware of malicious input as there is no way to stop the function from
>> trying to allocate enough memory for a giant file.
>>
>
> Yes, I wouldn't use this in a production setting

Chances are you're using tools and languages in a production setting
which have "snarf file as string" functions in their run-time libraries
that they are happily using.

The shell syntax "`$(cat file)" will grab the entire file and turn it into
a word interpolated into the command line; it takes no argument on
limiting the size, and you see it in system shell scripts all the time.

Just because the code is "in production" doesn't mean that the input is
controlled by a malicious user who is trying to bring down the
application.

If your code processes a stream in its entirety, it's may be open to a
DoS, even if it doesn't buffer all of it. It might not run out of
memory, but it will be stuck there reading the stream. The malicious
user just feeds a really large stream, perhaps an infinite one. Or a
small amount of input, but at a glacial pace, like one byte at a time,
with five minutes in between. (Anti-spam "honey pot" mail servers
do this sort of things to suspected spammer connections.)

To be completely paranoid, you need timeouts everywhere.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: reading entire input using getdelim

<20220917104318.614@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23227&group=comp.lang.c#23227

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: reading entire input using getdelim
Date: Sat, 17 Sep 2022 17:56:40 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <20220917104318.614@kylheku.com>
References: <tg3k99$3k87$1@oguzismail.eternal-september.org>
<slurp-20220917131018@ram.dialup.fu-berlin.de>
Injection-Date: Sat, 17 Sep 2022 17:56:40 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="cc03025c6c81b54fd7c9b754e7249e85";
logging-data="236814"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KSZL550ikWqCQuQ7OXd8FmxNoY2X6Oe0="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:ahwLIsSbIO+3G5f9fouTdHkiSxQ=
 by: Kaz Kylheku - Sat, 17 Sep 2022 17:56 UTC

On 2022-09-17, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> { while( !feof( file ))
> { if( ferror( file )){ free( buff ); return; }
> int const i = fgetc( file );
> if( !*buff )return;
> if( i < 0 )return;
> append( buff, size, offset,( unsigned char )i ); }}

I've used this sort of Lisp-like formatting for a bunch of code in the past.

The grammar actions in the TXR parser use it, e.g:

json_vals : json_val { $$ = if3(parser->quasi_level > 0 &&
unquotes_occur($1, 0),
cons($1, nil),
vector(one, $1)); }
| json_vals ',' json_val { if (consp($1))
{ $$ = cons($3, $1); }
else if (parser->quasi_level > 0 &&
unquotes_occur($3, 0))
{ val li = list_vec($1);
$$ = cons($3, li); }
else
{ vec_push($1, $3);
$$ = $1; } }

Re: reading entire input using getdelim

<slurp-20220918141208@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23230&group=comp.lang.c#23230

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: reading entire input using getdelim
Date: 18 Sep 2022 13:12:43 GMT
Organization: Stefan Ram
Lines: 42
Expires: 1 Sep 2023 11:59:58 GMT
Message-ID: <slurp-20220918141208@ram.dialup.fu-berlin.de>
References: <tg3k99$3k87$1@oguzismail.eternal-september.org> <slurp-20220917131018@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de OTZO0YFG/NDUN/EVZ+guHQanigZwwpT6Se2vHGN2TRMkK4
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Sun, 18 Sep 2022 13:12 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>( FILE * const file,
> unsigned char * *buff,
> size_t *size,
> size_t *offset )
>{ while( !feof( file ))
> { if( ferror( file )){ free( buff ); return; }

"free( buff )" is a bug, it should be "free( *buff )".

In the meantime, I rewrote the function to accept a maximum
size for the buffer and use "fread" instead of "fgetc".

/* Free buffer and mark it invalid. */
static inline void disable
( unsigned char * *buff )
{ free( *buff ); *buff = 0; }

/* Slurp a file into a buffer.
On read error or insufficient memory, free the buffer and set it to 0.
result = 1 if there still is more to be read from the stream. */
int slurp2
( FILE * const stream,
unsigned char * *buff,
size_t *size,
size_t max_size,
size_t *offset )
{ while( 1 )
{ size_t const to_be_read = *size - *offset;
size_t const read = fread( *buff + *offset, 1, to_be_read, stream );
if( ferror( stream )){ disable( buff ); return 0; }
*offset += read;
if( feof( stream )|| read < to_be_read )return 0;
else
{ /* buffer filled completely and not feof */
size_t const newsize = *size * 2;
if( newsize > max_size )return 1;
unsigned char * newbuff = realloc( *buff, newsize );
if( !newbuff )return 1;
else { *buff = newbuff; *size = newsize; }}}}

Re: reading entire input using getdelim

<tg7cpc$1l2js$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23231&group=comp.lang.c#23231

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: Re: reading entire input using getdelim
Date: Sun, 18 Sep 2022 15:17:00 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tg7cpc$1l2js$1@news.xmission.com>
References: <tg3k99$3k87$1@oguzismail.eternal-september.org> <slurp-20220917131018@ram.dialup.fu-berlin.de> <slurp-20220918141208@ram.dialup.fu-berlin.de>
Injection-Date: Sun, 18 Sep 2022 15:17:00 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1739388"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sun, 18 Sep 2022 15:17 UTC

In article <slurp-20220918141208@ram.dialup.fu-berlin.de>,
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>( FILE * const file,
>> unsigned char * *buff,
>> size_t *size,
>> size_t *offset )
>>{ while( !feof( file ))
>> { if( ferror( file )){ free( buff ); return; }
>
> "free( buff )" is a bug, it should be "free( *buff )".
>
> In the meantime, I rewrote the function to accept a maximum
> size for the buffer and use "fread" instead of "fgetc".

You're still not accomplishing anything that hasn't been done already
thousands of times.

Nor are you answering OP's question.

--
I've learned that people will forget what you said, people will forget
what you did, but people will never forget how you made them feel.

- Maya Angelou -

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor