From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17862 invoked by alias); 4 Nov 2013 23:43:57 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 17849 invoked by uid 89); 4 Nov 2013 23:43:56 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.8 required=5.0 tests=AWL,BAYES_50,FREEMAIL_FROM,RDNS_NONE,SPF_PASS autolearn=no version=3.3.2 X-HELO: dub0-omc1-s11.dub0.hotmail.com Received: from Unknown (HELO dub0-omc1-s11.dub0.hotmail.com) (157.55.0.210) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 04 Nov 2013 23:43:54 +0000 Received: from DUB122-W5 ([157.55.0.239]) by dub0-omc1-s11.dub0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Mon, 4 Nov 2013 15:43:46 -0800 X-TMN: [z7dMgrJw4pnGR3ZD1r7x+iI4fhQQeL57] Message-ID: From: Bernd Edlinger To: Dodji Seketeli , Jakub Jelinek CC: "gcc-patches@gcc.gnu.org" Subject: RE: [PATCH] preprocessor/58580 - preprocessor goes OOM with warning for zero literals Date: Tue, 05 Nov 2013 00:10:00 -0000 In-Reply-To: <87zjpkrx8p.fsf@redhat.com> References: ,<20131031144309.GR27813@tucnak.zalov.cz> <87y559xz7y.fsf@redhat.com>,<20131031173649.GW27813@tucnak.zalov.cz> <87r4awtmnx.fsf@redhat.com>,<20131104115748.GO27813@tucnak.zalov.cz>,<87zjpkrx8p.fsf@redhat.com> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-SW-Source: 2013-11/txt/msg00309.txt.bz2 Hi, On Mon, 4 Nov 2013 16:40:38, Dodji Seketeli wrote: > +static ssize_t > +get_line (char **lineptr, size_t *n, FILE *fp) > +{ > + ssize_t cur_len =3D 0, len; > + char buf[16384]; > + > + if (lineptr =3D=3D NULL || n =3D=3D NULL) > + return -1; > + > + if (*lineptr =3D=3D NULL || *n =3D=3D 0) > + { > + *n =3D 120; > + *lineptr =3D XNEWVEC (char, *n); > + } > + > + len =3D fread (buf, 1, sizeof buf, fp); > + if (ferror (fp)) > + return -1; > + > + for (;;) > + { > + size_t needed; > + char *t =3D (char*) memchr (buf, '\n', len); > + if (t !=3D NULL) len =3D (t - buf) + 1; > + if (__builtin_expect (len>=3D SSIZE_MAX - cur_len, 0)) > + return -1; > + needed =3D cur_len + len + 1; > + if (needed> *n) > + { > + char *new_lineptr; > + if (needed < 2 * *n) > + needed =3D 2 * *n; > + new_lineptr =3D XRESIZEVEC (char, *lineptr, needed); > + *lineptr =3D new_lineptr; > + *n =3D needed; > + } > + memcpy (*lineptr + cur_len, buf, len); > + cur_len +=3D len; > + if (t !=3D NULL) > + break; > + len =3D fread (buf, 1, sizeof buf, fp); > + if (ferror (fp)) > + return -1; > + if (len =3D=3D 0) > + break; > + } > + (*lineptr)[cur_len] =3D '\0'; > + return cur_len; > +} > + > +/* Reads one line from FILE into a static buffer. LINE_LENGTH is set > + by this function to the length of the returned line. Note that the > + returned line can contain several zero bytes. */ > static const char * > -read_line (FILE *file) > +read_line (FILE *file, int *line_length) > { > static char *string; > - static size_t string_len; > + static size_t string_len, cur_len; > size_t pos =3D 0; > char *ptr; > > if (!string_len) > { > string_len =3D 200; > - string =3D XNEWVEC (char, string_len); > + string =3D XCNEWVEC (char, string_len); > } > + else > + memset (string, 0, string_len); Is this memset still necessary? If the previous invocation of read_line already had read some characters of the following line, how is that information recovered? How is it detected if another file is to be read this time? > > - while ((ptr =3D fgets (string + pos, string_len - pos, file))) > + ptr =3D string; > + cur_len =3D string_len; > + while (size_t len =3D get_line (&ptr, &cur_len, file)) > { > - size_t len =3D strlen (string + pos); > - > - if (string[pos + len - 1] =3D=3D '\n') > + if (ptr[len - 1] =3D=3D '\n') > { > - string[pos + len - 1] =3D 0; > + ptr[len - 1] =3D 0; > + *line_length =3D len; > return string; > } > pos +=3D len; > string =3D XRESIZEVEC (char, string, string_len * 2); > string_len *=3D 2; > - } > - > + ptr =3D string + pos; If "ptr" is passed to get_line it will try to reallocate it, which must fail, right? Maybe, this line of code is unreachable? Who is responsible for reallocating "string" get_line or read_line? > + cur_len =3D string_len - pos; > + } > + > + *line_length =3D pos ? string_len : 0; > return pos ? string : NULL; > } > > /* Return the physical source line that corresponds to xloc in a > buffer that is statically allocated. The newline is replaced by > - the null character. */ > + the null character. Note that the line can contain several null > + characters, so LINE_LEN contains the actual length of the line. */ > > const char * > -location_get_source_line (expanded_location xloc) > +location_get_source_line (expanded_location xloc, > + int& line_len) > { > const char *buffer; > int lines =3D 1; > @@ -132,7 +204,7 @@ location_get_source_line (expanded_location xloc) > if (!stream) > return NULL; > > - while ((buffer =3D read_line (stream)) && lines < xloc.line) > + while ((buffer =3D read_line (stream, &line_len)) && lines < xloc.line) > lines++; > > fclose (stream); Regards Bernd.=20=09=09=20=09=20=20=20=09=09=20=20