public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang] cli: add libc error handling
Date: Tue, 17 Sep 2024 15:14:58 +0000 (UTC)	[thread overview]
Message-ID: <20240917151447.247230-1-carlos@maniero.me> (raw)

The application was ignoring libc functions result and not handling
possible errors. This effort also makes the application POSIX complaint.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/main.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/src/main.c b/src/main.c
index e16695b..030f34c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -164,14 +164,24 @@ handle_codegen_linux(cli_opts_t *opts)
 
     char command[512];
     sprintf(command, "%s/bin/as %s -o " SV_FMT ".o", opts->sysroot, asm_file, SV_ARG(opts->output_bin));
-    system(command);
+
+    int exit_code = system(command);
+
+    if (exit_code != 0) {
+        exit(exit_code);
+    }
 
     sprintf(command,
             "%s/bin/ld " SV_FMT ".o -o " SV_FMT "",
             opts->sysroot,
             SV_ARG(opts->output_bin),
             SV_ARG(opts->output_bin));
-    system(command);
+
+    exit_code = system(command);
+
+    if (exit_code != 0) {
+        exit(exit_code);
+    }
 
     if (!(opts->options & CLI_OPT_SAVE_TEMPS)) {
         char output_file[256];
@@ -211,7 +221,13 @@ read_entire_file(char *file_path, arena_t *arena)
         exit(EXIT_FAILURE);
     }
 
-    fread(file_content.chars, 1, file_content.size, stream);
+    size_t read_bytes = fread(file_content.chars, 1, file_content.size, stream);
+
+    if (read_bytes != file_content.size) {
+        fprintf(stderr, "error: failed to read all file bytes %s\n", file_path);
+        exit(EXIT_FAILURE);
+    }
+
     fclose(stream);
 
     return file_content;

base-commit: 5802359caf011c960b455dcda7ce10ea794b0ea1
-- 
2.34.1


             reply	other threads:[~2024-09-17 15:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-17 15:14 Carlos Maniero [this message]
2024-09-17 15:15 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-17 15:17 ` [PATCH olang] cli: add libc error handling Johnny Richard

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=20240917151447.247230-1-carlos@maniero.me \
    --to=carlos@maniero.me \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox