[gdal-dev] NULL-returning functions and CPLGetLastErrorMsg

Even Rouault even.rouault at spatialys.com
Wed Nov 27 07:47:57 PST 2024


Laurențiu,

> Many GDAL functions can return nullptr (e.g. OSRGetName, 
> OSRGetAuthorityCode etc.), but it's not clear if they set an error 
> message that can be retrieved using CPLGetLastErrorMsg. I tried to 
> check the code, but CPLErrorSetState isn't called from many places, so 
> I'm probably missing something like a PROJ/GEOS error handler that 
> sets it.

- OSRGetName() would return null only if the SRS is invalid/unset (you 
won't get any CPLError() in that situation). If a unlikely memory 
allocation error would occur in PROJ, I believe, but I'm not completely 
sure, that PROJ would emit a PROJ error that would be transformed as a 
GDAL CE_Failure through the osr_proj_logger() PROJ-error handler 
installed in ogr/ogr_proj_p.cpp.

- similarly for OSRGetAuthorityCode(). Returns null on invalid/unset SRS 
or SRS without identifier, without emitting a CPLError().   And same as 
above if the error occurs within PROJ

>
> Is there any rule-of-thumb for figuring out when CPLGetLastErrorMsg 
> should be used?

No. If you are paranoid about that, reset the error state with 
CPLErrorReset() before calling a function and check 
CPLGetLastErrorType() == CE_Failure after, but probably only if the 
return code of the function is an error. In some circumstances, a 
function might return a valid output, but a CE_Failure can be emitted by 
deep layers of GDAL but recovered by upper layers that will miss 
resetting the error state.

Even

-- 
http://www.spatialys.com
My software is free, but my time generally not.
Butcher of all kinds of standards, open or closed formats. At the end, this is just about bytes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20241127/65e5b59f/attachment.htm>


More information about the gdal-dev mailing list