public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "me at duckie dot co" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug stdio/28038] New: getdelim()/getline() does not add NUL terminator if first character is EOF.
Date: Wed, 30 Jun 2021 21:43:15 +0000	[thread overview]
Message-ID: <bug-28038-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=28038

            Bug ID: 28038
           Summary: getdelim()/getline() does not add NUL terminator if
                    first character is EOF.
           Product: glibc
           Version: 2.28
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: stdio
          Assignee: unassigned at sourceware dot org
          Reporter: me at duckie dot co
  Target Milestone: ---

Created attachment 13520
  --> https://sourceware.org/bugzilla/attachment.cgi?id=13520&action=edit
Testcase (run with: printf '' | valgrind ./testcase)

According to IEEE Std 1003.1-2017 (
https://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html ):

> The characters read, including any delimiter, shall be stored in the object,
> and a terminating NUL added when the delimiter or end-of-file is encountered.

However, if the first character is an end-of-file, the first character of the
line should be a NUL -- instead, it's uninitialized.

Looking at the source, it appears this is still a problem:
https://sourceware.org/git/?p=glibc.git;a=blob;f=libio/iogetdelim.c;hb=HEAD

I have attached a testcase. If you compile the testcase (gcc -Wall -Werror
-pedantic testcase.c -o testcase), then do `printf '' | valgrind
--track-origins=yes ./testcase`, you get the following:


puppy@cerberus:~/boreutils$ printf '' | valgrind --track-origins=yes ./testcase
==2904== Memcheck, a memory error detector
==2904== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2904== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==2904== Command: ./testcase
==2904== 
bytes_read = -1, line = '77709376'
==2904== Conditional jump or move depends on uninitialised value(s)
==2904==    at 0x4838C65: strlen (vg_replace_strmem.c:460)
==2904==    by 0x48A99EE: vfprintf (vfprintf.c:1638)
==2904==    by 0x48B0605: printf (printf.c:33)
==2904==    by 0x4011CE: main (in /home/puppy/boreutils/testcase)
==2904==  Uninitialised value was created by a heap allocation
==2904==    at 0x483577F: malloc (vg_replace_malloc.c:299)
==2904==    by 0x48C8C2E: getdelim (iogetdelim.c:62)
==2904==    by 0x401183: main (in /home/puppy/boreutils/testcase)
==2904== 
> (bytes_read == -1 && line != NULL): ''
==2904== 
==2904== HEAP SUMMARY:
==2904==     in use at exit: 0 bytes in 0 blocks
==2904==   total heap usage: 3 allocs, 3 frees, 5,240 bytes allocated
==2904== 
==2904== All heap blocks were freed -- no leaks are possible
==2904== 
==2904== For counts of detected and suppressed errors, rerun with: -v
==2904== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
puppy@cerberus:~/boreutils$

-- 
You are receiving this mail because:
You are on the CC list for the bug.

             reply	other threads:[~2021-06-30 21:43 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-30 21:43 me at duckie dot co [this message]
2021-06-30 21:44 ` [Bug stdio/28038] " me at duckie dot co

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-28038-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).