Skip to content

itsa: Replace fgets(3) with getline(3)#51

Merged
ac000 merged 1 commit into
masterfrom
getline
May 24, 2026
Merged

itsa: Replace fgets(3) with getline(3)#51
ac000 merged 1 commit into
masterfrom
getline

Conversation

@ac000
Copy link
Copy Markdown
Owner

@ac000 ac000 commented May 24, 2026

itsa: Replace fgets(3) with getline(3)

getline(3) is generally safer than fgets(3), for example you won't get
truncated input and will always get the \n character.

Rather than using getline(3) directly we create a wrapper function for
it, ssize_t getstdin(char **lineptr);

We don't care about the allocated string size and are always reading
from stdin.

The first thing we do is free(*lineptr) and set it to NULL, this makes
subsequent calls to getstdin() in the same function safe and not leak
memory.

If getline(3) returns an error, we free lineptr and set it to NULL so it
can be safely passed to free by the __cleanup_free function and return
-1. That way we can just check lineptr for NULL to check for an error.

lineptr should always be set to NULL before the first call to getstdin()
in a fucntion.

Signed-off-by: Andrew Clayton <ac@sigsegv.uk>

Copilot AI review requested due to automatic review settings May 24, 2026 19:00
@ac000 ac000 changed the title itsa: Replace fgets(3) with getlione(3) itsa: Replace fgets(3) with getline(3) May 24, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR replaces fixed-size fgets(3) stdin reads with a getline(3)-based wrapper (getstdin) to avoid truncated input and simplify interactive prompting across itsa.

Changes:

  • Added getstdin(char **lineptr) wrapper around getline(3) for stdin reading.
  • Updated multiple interactive prompts to use getstdin() instead of stack buffers + fgets().
  • Adjusted related local variables to use __cleanup_free heap strings instead of fixed-size arrays.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/itsa.c
Comment thread src/itsa.c Outdated
Comment thread src/itsa.c
Comment thread src/itsa.c
getline(3) is generally safer than fgets(3), for example you won't get
truncated input and will always get the \n character.

Rather than using getline(3) directly we create a wrapper function for
it, ssize_t getstdin(char **lineptr);

We don't care about the allocated string size and are always reading
from stdin.

The first thing we do is free(*lineptr) and set it to NULL, this makes
subsequent calls to getstdin() in the same function safe and not leak
memory.

If getline(3) returns an error, we free lineptr and set it to NULL so it
can be safely passed to free by the __cleanup_free function and return
-1. That way we can just check lineptr for NULL to check for an error.

lineptr should always be set to NULL before the first call to getstdin()
in a fucntion.

Signed-off-by: Andrew Clayton <ac@sigsegv.uk>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@ac000 ac000 merged commit dbc30f8 into master May 24, 2026
9 checks passed
@ac000 ac000 deleted the getline branch May 24, 2026 20:15
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