changeset 31138:68762676dab1

Tiff writeEncodedStrip: prevent writing to a read-only file * __tiff__.cc(F__tiff_write_encoded_strip__): add a check to disallow calling the function on a file opened in read-only mode. * Tiff.m: added a test for this new failure case.
author magedrifaat <magedrifaat@gmail.com>
date Wed, 27 Jul 2022 00:54:48 +0200
parents 233130c0b1f6
children 3431a15b2c75
files libinterp/dldfcn/__tiff__.cc scripts/io/Tiff.m
diffstat 2 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/dldfcn/__tiff__.cc	Wed Jul 27 00:37:50 2022 +0200
+++ b/libinterp/dldfcn/__tiff__.cc	Wed Jul 27 00:54:48 2022 +0200
@@ -5,6 +5,11 @@
 #include <string>
 #include <iostream>
 
+// Workaround to not have to include fcntl which doesn't exist on windows
+#ifndef O_RDONLY
+#define O_RDONLY 0
+#endif
+
 #include "defun-dld.h"
 #include "ov.h"
 #include "ovl.h"
@@ -1180,6 +1185,9 @@
     
     TIFF *tif = (TIFF *)(args (0).uint64_value ());
 
+    if (TIFFGetMode (tif) == O_RDONLY)
+      error ("Can't write data to a file opened in read-only mode");
+
     // Obtain all necessary tags
     tiff_image_data image_data (tif);
 
--- a/scripts/io/Tiff.m	Wed Jul 27 00:37:50 2022 +0200
+++ b/scripts/io/Tiff.m	Wed Jul 27 00:54:48 2022 +0200
@@ -379,3 +379,19 @@
 %!  unwind_protect_cleanup
 %!    unlink (filename);
 %!  end_unwind_protect
+
+## test failure writing to a read-only file
+%!testif HAVE_TIFF
+%!  filename = [tempname() ".tif"];
+%!  unwind_protect
+%!    a = Tiff (filename, "w");
+%!    set_configs (a, 1, 1, 1, 8);
+%!    data = uint8 (randi (intmax ("uint8"), 1, 1));
+%!    writeEncodedStrip (a, 1, data);
+%!    a.close();
+%!    a = Tiff (filename, "r");
+%!    fail ("writeEncodedStrip(a, 1, [1])", "Can't write data to a file opened in read-only mode");
+%!    a.close();
+%!  unwind_protect_cleanup
+%!    unlink (filename);
+%!  end_unwind_protect
\ No newline at end of file