Skip to content

Fix fgets(..., size=1)#15

Closed
marxin wants to merge 1 commit into
fanf2:masterfrom
marxin:fix-fgets-with-size-equal-to-1
Closed

Fix fgets(..., size=1)#15
marxin wants to merge 1 commit into
fanf2:masterfrom
marxin:fix-fgets-with-size-equal-to-1

Conversation

@marxin

@marxin marxin commented Aug 10, 2022

Copy link
Copy Markdown

I noticed the following 2 tests are failing with -O1
-D_FORTIFY_SOURCE=1:

[ 44s] FAILED: whitespace-1.out: unifdef -DFOO whitespace.c
[ 44s] FAILED: whitespace-2.out: unifdef -DBAR whitespace.c

It's caused by fact that:

fgets returns '\0' if n == 1:

char *
_IO_fgets (char *buf, int n, FILE *fp)
{
  size_t count;
  char *result;
  int old_error;
  CHECK_FILE (fp, NULL);
  if (n <= 0)
    return NULL;
  if (__glibc_unlikely (n == 1))
    {
      /* Another irregular case: since we have to store a NUL byte and
	 there is only room for exactly one byte, we don't have to
	 read anything.  */
      buf[0] = '\0';
      return buf;
    }

I noticed the following 2 tests are failing with -O1
-D_FORTIFY_SOURCE=1:

[   44s] FAILED: whitespace-1.out: unifdef -DFOO whitespace.c
[   44s] FAILED: whitespace-2.out: unifdef -DBAR whitespace.c

It's caused by fact that:

fgets returns '\0' if n == 1:

char *
_IO_fgets (char *buf, int n, FILE *fp)
{
  size_t count;
  char *result;
  int old_error;
  CHECK_FILE (fp, NULL);
  if (n <= 0)
    return NULL;
  if (__glibc_unlikely (n == 1))
    {
      /* Another irregular case: since we have to store a NUL byte and
	 there is only room for exactly one byte, we don't have to
	 read anything.  */
      buf[0] = '\0';
      return buf;
    }
@marxin

marxin commented Jan 6, 2023

Copy link
Copy Markdown
Author

Closing due to no action.

@marxin marxin closed this Jan 6, 2023
@thesamesam

Copy link
Copy Markdown

@fanf2 Could you take a look? Thanks.

@marxin marxin reopened this Jan 10, 2023
@marxin

marxin commented Jan 16, 2023

Copy link
Copy Markdown
Author

Please let me know if you are interested and if so, I'll reopen the pull request.

@marxin marxin closed this Jan 16, 2023
gentoo-bot pushed a commit to gentoo/gentoo that referenced this pull request May 10, 2025
The unifdef testsuite has been failing sometimes for years with
_FORTIFY_SOURCE in a non-obvious way, where glibc's fgets will return
\0 w/ n=1.

I'd found marxin's PR for this years ago and apparently completely forgot
about it (nor did I backport the patch into Gentoo at the time, as
I didn't maintain it, and I was fairly new then).

Backport marxin's patch from 2022 accordingly. Thanks to Ben Beasley
poking me on the upstream PR (19) which made me look at all of this
again.

Bug: https://gcc.gnu.org/PR120205
Bug: fanf2/unifdef#15
Bug: fanf2/unifdef#19
Closes: https://bugs.gentoo.org/836698
Signed-off-by: Sam James <sam@gentoo.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants