# HG changeset patch # User magedrifaat # Date 1658876088 -7200 # Node ID 68762676dab106a5bb81439338c28e7ba6ab1fbd # Parent 233130c0b1f60eac52b9b1e183e5e06f2b2e459c 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. diff -r 233130c0b1f6 -r 68762676dab1 libinterp/dldfcn/__tiff__.cc --- 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 #include +// 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); diff -r 233130c0b1f6 -r 68762676dab1 scripts/io/Tiff.m --- 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