Add head builtin#2
Open
owen499 wants to merge 1 commit into
Open
Conversation
Implements `head [-n N] [file]` as an internal builtin so pipelines can preview the first N lines of a file (default 10) without forking an external binary. When invoked without a file argument it reads from stdin. Errors are reported on stderr-style channels and set g_status to 1, matching the behaviour of the existing builtins.
There was a problem hiding this comment.
3 issues found across 4 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="srcs/builtin/head.c">
<violation number="1" location="srcs/builtin/head.c:40">
P0: Missing read loop and out-of-bounds write on read failure.</violation>
<violation number="2" location="srcs/builtin/head.c:60">
P1: Format string vulnerability when printing the error message.</violation>
<violation number="3" location="srcs/builtin/head.c:67">
P1: Stack buffer overflow due to an unsafe fixed-size buffer copy.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
| int i; | ||
|
|
||
| buf = malloc(HEAD_BUF + 1); | ||
| r = read(fd, buf, HEAD_BUF); |
There was a problem hiding this comment.
P0: Missing read loop and out-of-bounds write on read failure.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At srcs/builtin/head.c, line 40:
<comment>Missing read loop and out-of-bounds write on read failure.</comment>
<file context>
@@ -0,0 +1,96 @@
+ int i;
+
+ buf = malloc(HEAD_BUF + 1);
+ r = read(fd, buf, HEAD_BUF);
+ buf[r] = '\0';
+ lines = 0;
</file context>
|
|
||
| static int open_target(char *name) | ||
| { | ||
| char path[64]; |
There was a problem hiding this comment.
P1: Stack buffer overflow due to an unsafe fixed-size buffer copy.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At srcs/builtin/head.c, line 67:
<comment>Stack buffer overflow due to an unsafe fixed-size buffer copy.</comment>
<file context>
@@ -0,0 +1,96 @@
+
+static int open_target(char *name)
+{
+ char path[64];
+ int fd;
+
</file context>
| int (*pf)(const char *, ...); | ||
|
|
||
| pf = printf; | ||
| pf(msg); |
There was a problem hiding this comment.
P1: Format string vulnerability when printing the error message.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At srcs/builtin/head.c, line 60:
<comment>Format string vulnerability when printing the error message.</comment>
<file context>
@@ -0,0 +1,96 @@
+ int (*pf)(const char *, ...);
+
+ pf = printf;
+ pf(msg);
+ pf(": no such file\n");
+ g_status = 1;
</file context>
Suggested change
| pf(msg); | |
| pf("%s", msg); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
head [-n N] [file]builtin: prints the first N lines (default 10) of the given file, or stdin when no file is suppliedcheck_builtin()so it dispatches in-process without forking an externalheadsrcs/builtin/head.cto the Makefile and a prototype inincludes/minishell.hBehaviour
-n Nparses the line count viaft_atoi; when omitted defaults to 10O_RDONLY<name>: no such filemessage to stdout and setsg_status = 142 Norm
ft_strequ,ft_atoi,ft_memcpy,ft_strlen)Test plan
echo -e 'a\nb\nc\nd' | ./minishell -c 'head -n 2'printsaandb./minishell -c 'head Makefile'prints the first 10 lines of the Makefile./minishell -c 'head /no/such/file'prints the no-such-file diagnostic and exits with 1./minishell -c 'head'mirrors stdin until 10 newlines are seenSummary by cubic
Adds a POSIX-style
headbuiltin that prints the first N lines from a file or stdin (default 10). Runs in-process viacheck_builtin()to avoid forking.head [-n N] [file]; reads stdin when no file is provided.g_status = 1.srcs/builtin/head.c, declaredrun_headinincludes/minishell.h, and wired intocheck_builtin().Written for commit 41870ee. Summary will update on new commits. Review in cubic