diff mbox series

[bug#45983,3/3] scripts: import: json: Fix error handling.

Message ID 20210119152737.4344-3-zimon.toutoune@gmail.com
State Accepted
Headers show
Series [bug#45983,1/3] import: gnu: Add internationalized messages. | expand

Checks

Context Check Description
cbaines/submitting builds success
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job
cbaines/issue success View issue

Commit Message

Simon Tournier Jan. 19, 2021, 3:27 p.m. UTC
Fixes partially <https://bugs.gnu.org/44115>.

* guix/scripts/import/json.scm (guix-import-json): Handle error.
---
 guix/scripts/import/json.scm | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Ludovic Courtès Jan. 26, 2021, 10:17 p.m. UTC | #1
zimoun <zimon.toutoune@gmail.com> skribis:

> Fixes partially <https://bugs.gnu.org/44115>.
>
> * guix/scripts/import/json.scm (guix-import-json): Handle error.

[...]

> -       (or (json->code file-name)
> -           (leave (G_ "invalid JSON in file '~a'~%") file-name)))
> +       (if (file-exists? file-name)
> +         (or (json->code file-name)
> +             (leave (G_ "invalid JSON in file '~a'~%") file-name))
> +         (leave (G_ "invalid file name~%"))))

I’d suggest this:

  (catch 'system-error
    (lambda ()
      (or (json->code …) …))
    (lambda args
      (leave (G_ "failed to access '~a': ~a~%")
             file-name (strerror (system-error-errno args)))))

This avoids TOCTTOU and gives details about the failure.

Could you send updated patches?

Thanks,
Ludo’.
diff mbox series

Patch

diff --git a/guix/scripts/import/json.scm b/guix/scripts/import/json.scm
index 778e5f4bc5..63fba260ae 100644
--- a/guix/scripts/import/json.scm
+++ b/guix/scripts/import/json.scm
@@ -1,6 +1,7 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
 ;;; Copyright © 2015, 2017 Ricardo Wurmus <rekado@elephly.net>
+;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -88,8 +89,10 @@  Import and convert the JSON package definition in PACKAGE-FILE.\n"))
                            (reverse opts))))
     (match args
       ((file-name)
-       (or (json->code file-name)
-           (leave (G_ "invalid JSON in file '~a'~%") file-name)))
+       (if (file-exists? file-name)
+         (or (json->code file-name)
+             (leave (G_ "invalid JSON in file '~a'~%") file-name))
+         (leave (G_ "invalid file name~%"))))
       (()
        (leave (G_ "too few arguments~%")))
       ((many ...)