From 80edb84a04b7bef2273010e728aeff63a15c4d82 Mon Sep 17 00:00:00 2001 From: Bill Cleveland <120688282+BillCleveland-USRA@users.noreply.github.com> Date: Fri, 12 May 2023 09:39:04 -0500 Subject: [PATCH] Fixed issue 14808: Added condition type(recformat) == _FormatP to mitigate the issue cause by _FormatQ is a subclass of _FormatP. Added regress test for Issue 14808. Added "what's new" file for pull 14810. --- astropy/io/fits/fitsrec.py | 2 +- astropy/io/fits/tests/test_table.py | 23 +++++++++++++++++++++++ docs/changes/io.fits/14810.bugfix.rst | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 docs/changes/io.fits/14810.bugfix.rst diff --git a/astropy/io/fits/fitsrec.py b/astropy/io/fits/fitsrec.py index a9addc8b811..4552cc8322c 100644 --- a/astropy/io/fits/fitsrec.py +++ b/astropy/io/fits/fitsrec.py @@ -1130,7 +1130,7 @@ def _scale_back(self, update_heap_pointers=True): # Even if this VLA has not been read or updated, we need to # include the size of its constituent arrays in the heap size # total - if heapsize >= 2**31: + if type(recformat) == _FormatP and heapsize >= 2**31: raise ValueError( "The heapsize limit for 'P' format has been reached. " "Please consider using the 'Q' format for your file." diff --git a/astropy/io/fits/tests/test_table.py b/astropy/io/fits/tests/test_table.py index 31512839510..66a1e35e35e 100644 --- a/astropy/io/fits/tests/test_table.py +++ b/astropy/io/fits/tests/test_table.py @@ -3230,6 +3230,29 @@ def test_heapsize_P_limit(self): ): t.writeto(self.temp("matrix.fits")) + @pytest.mark.skipif(sys.maxsize < 2**32, reason="requires 64-bit system") + @pytest.mark.skipif(sys.platform == "win32", reason="Cannot test on Windows") + @pytest.mark.hugemem + def test_heapsize_Q_limit(self): + """ + Regression test for https://github.com/astropy/astropy/issues/14808 + + Check if the error is no longer raised when the heap size is bigger than what can be + indexed with a 32 bit signed int. + """ + + # a matrix with variable length array elements is created + nelem = 2**28 + matrix = np.zeros(1, dtype=np.object_) + matrix[0] = np.arange(0.0, float(nelem + 1)) + + col = fits.Column(name="MATRIX", format=f"QD({nelem})", unit="", array=matrix) + + t = fits.BinTableHDU.from_columns([col]) + t.name = "MATRIX" + + t.writeto(self.temp("matrix.fits")) + def test_empty_vla_raw_data(self): """ Regression test for https://github.com/astropy/astropy/issues/12881 diff --git a/docs/changes/io.fits/14810.bugfix.rst b/docs/changes/io.fits/14810.bugfix.rst new file mode 100644 index 00000000000..c2736bf0530 --- /dev/null +++ b/docs/changes/io.fits/14810.bugfix.rst @@ -0,0 +1,2 @@ +Fixes an issue where FITS_rec was incorrectly raising a ValueError exception when the heapsize was greater than 2**31 +when the Column type was 'Q' instead of 'P'.