Skip to content

Commit

Permalink
fix(fits): Make sure to close if open fails to find right magic number (
Browse files Browse the repository at this point in the history
AcademySoftwareFoundation#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.
  • Loading branch information
lgritz committed Feb 13, 2023
1 parent f222d05 commit 6a7e404
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/fits.imageio/fits_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class FitsInput final : public ImageInput {
m_cur_subimage = 0;
m_bitpix = 0;
m_naxes = 0;
m_naxis.clear();
keys.clear();
m_subimages.clear();
m_comment.clear();
m_history.clear();
Expand Down Expand Up @@ -145,6 +147,7 @@ class FitsOutput final : public ImageOutput {
m_bitpix = 0;
m_simple = true;
m_scratch.clear();
m_tilebuffer.clear();
m_sep = '\n';
}

Expand Down
7 changes: 1 addition & 6 deletions src/fits.imageio/fitsinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ FitsInput::open(const std::string& name, ImageSpec& spec)

// checking if the file is FITS file
char magic[6] = { 0 };
if (fread(magic, 1, 6, m_fd) != 6) {
errorf("%s isn't a FITS file", m_filename);
return false; // Read failed
}

if (strncmp(magic, "SIMPLE", 6)) {
if (fread(magic, 1, 6, m_fd) != 6 || strncmp(magic, "SIMPLE", 6)) {
errorf("%s isn't a FITS file", m_filename);
close();
return false;
Expand Down

0 comments on commit 6a7e404

Please sign in to comment.