diff options
| author | 2022-11-24 11:59:33 +0800 | |
|---|---|---|
| committer | 2022-11-24 11:59:33 +0800 | |
| commit | 8f101080828c3aa752995a107646b6b548827f67 (patch) | |
| tree | 84ab1d1a5e4e68144f9a7fd786c45e1678b64ae5 | |
| parent | Add unit test for MemFS access before __enter__ (diff) | |
Add request file IO error handling
| -rw-r--r-- | snekbox/nsjail.py | 10 | ||||
| -rw-r--r-- | tests/test_nsjail.py | 8 | 
2 files changed, 16 insertions, 2 deletions
| diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py index ec5c6b1..b33e41e 100644 --- a/snekbox/nsjail.py +++ b/snekbox/nsjail.py @@ -218,8 +218,14 @@ class NsJail:              # Write provided files if any              for file in files: -                file.save_to(fs.home) -                log.info(f"Created file at {(fs.home / file.path)!r}.") +                try: +                    file.save_to(fs.home) +                    log.info(f"Created file at {(fs.home / file.path)!r}.") +                except OSError as e: +                    log.info(f"Failed to create file at {(fs.home / file.path)!r}.", exc_info=e) +                    return EvalResult( +                        args, None, f"{e.__class__.__name__}: Failed to create file '{file.path}'." +                    )              msg = "Executing code..."              if DEBUG: diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index d63180d..127753e 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -207,6 +207,14 @@ class NsJailTests(unittest.TestCase):          )          self.assertEqual(result.stderr, None) +    def test_file_write_error(self): +        """Test errors during file write.""" +        result = self.nsjail.python3([""], [FileAttachment("output", "hello")]) + +        self.assertEqual(result.returncode, None) +        self.assertEqual(result.stdout, "IsADirectoryError: Failed to create file 'output'.") +        self.assertEqual(result.stderr, None) +      def test_sigsegv_returns_139(self):  # In honour of Juan.          code = dedent(              """ | 
