Skip to content

Non-finite RAT value in a PAM sidecar crashes open_geotiff with OverflowError #3590

Description

@brendancol

Description

read_pam_sidecar() must never raise on a bad sidecar: open_geotiff() reads the PAM .aux.xml for any local string source, and a malformed sidecar is supposed to degrade to {} rather than break the read (#3520, #3522).

There's still a gap. _parse_rat() converts a thematic RAT's Value cell with int(float(fields[value_col])). When the cell text parses to infinity (1e400, inf, -inf), int() raises OverflowError, which subclasses ArithmeticError, not ValueError, so the (OSError, ValueError, TypeError, IndexError, ParseError) tuple in read_pam_sidecar() doesn't cover it. The exception escapes and crashes open_geotiff().

Same contract violation as #3520, different exception. A corrupt or hostile .aux.xml next to an otherwise valid raster makes that raster unopenable.

Steps to reproduce

import numpy as np
import xarray as xr
from xrspatial.geotiff import to_geotiff, open_geotiff

da = xr.DataArray(np.arange(12, dtype='float32').reshape(3, 4), dims=('y', 'x'),
                  coords={'y': [2.5, 1.5, 0.5], 'x': [0.5, 1.5, 2.5, 3.5]},
                  attrs={'crs': 32633})
to_geotiff(da, 'dem_3xxx.tif')

with open('dem_3xxx.tif.aux.xml', 'w') as fh:
    fh.write('''<PAMDataset>
  <PAMRasterBand band="1">
    <GDALRasterAttributeTable tableType="thematic">
      <FieldDefn index="0"><Name>Value</Name><Type>1</Type><Usage>5</Usage></FieldDefn>
      <FieldDefn index="1"><Name>Class</Name><Type>2</Type><Usage>2</Usage></FieldDefn>
      <Row index="0"><F>1e400</F><F>water</F></Row>
    </GDALRasterAttributeTable>
  </PAMRasterBand>
</PAMDataset>
''')

open_geotiff('dem_3xxx.tif')
# OverflowError: cannot convert float infinity to integer

Expected behaviour

The raster opens and the sidecar is ignored (no category_names attr), like every other malformed-sidecar case.

Actual behaviour

OverflowError: cannot convert float infinity to integer from inside _parse_rat, crashing the read.

Fix sketch

Add OverflowError to the caught exception tuple in read_pam_sidecar(), the same way #3522 added IndexError and ParseError.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggeotiffGeoTIFF moduleseverity:mediumSweep finding: MEDIUMsweep-securityFound by /sweep-security

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions