Skip to content

Commit fa9dade

Browse files
authored
fix(fits): Make sure to close if open fails to find right magic number (#3771)
There are two ways to fail the FITS magic number check: fail to read the bytes, or the bytes don't match the magic number. It seemed suspicious that we call close() for the latter but not the former. Combine them into a single error response (they had the same message, anyway). Also, make sure FitsInput::init() fully clears some other data structures. Oversight? I don't know if any of these things are real bugs, but they look sloppy and potentially problematic, so fixing them up.
1 parent 9cb000f commit fa9dade

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/fits.imageio/fits_pvt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class FitsInput final : public ImageInput {
8181
m_cur_subimage = 0;
8282
m_bitpix = 0;
8383
m_naxes = 0;
84+
m_naxis.clear();
85+
keys.clear();
8486
m_subimages.clear();
8587
m_comment.clear();
8688
m_history.clear();
@@ -145,6 +147,7 @@ class FitsOutput final : public ImageOutput {
145147
m_bitpix = 0;
146148
m_simple = true;
147149
m_scratch.clear();
150+
m_tilebuffer.clear();
148151
m_sep = '\n';
149152
}
150153

src/fits.imageio/fitsinput.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,7 @@ FitsInput::open(const std::string& name, ImageSpec& spec)
6868

6969
// checking if the file is FITS file
7070
char magic[6] = { 0 };
71-
if (fread(magic, 1, 6, m_fd) != 6) {
72-
errorf("%s isn't a FITS file", m_filename);
73-
return false; // Read failed
74-
}
75-
76-
if (strncmp(magic, "SIMPLE", 6)) {
71+
if (fread(magic, 1, 6, m_fd) != 6 || strncmp(magic, "SIMPLE", 6)) {
7772
errorf("%s isn't a FITS file", m_filename);
7873
close();
7974
return false;

0 commit comments

Comments
 (0)