[mapserver-dev] Wired behavior of msOWSGetEPSGProj()

Even Rouault even.rouault at spatialys.com
Fri Mar 13 06:47:55 PDT 2015

Selon "Eichner, Andreas - SID" <Andreas.Eichner at sid.sachsen.de>:

> Hello gurus,
> due to an issue asked on the mapserver-users list I stumbled upon strange
> behavior of msOWSGetEPSGProj() from mapows.c. To me it looks as if it returns
> a pointer to a character buffer on the local stack frame:
> const char *msOWSGetEPSGProj(projectionObj *proj, hashTableObj *metadata,
> const char *namespaces, int bReturnOnlyFirstOne)
> {
>   static char epsgCode[20] ="";
>   char *value;
> ...
>     if ((value=strchr(epsgCode, ' ')) != NULL) *value = '\0';
>     return epsgCode;
> I guess the correct way would be to copy the buffer into an malloc'ed one.
> But then the caller needs to free the buffer and there are lots of...
> The only reason why this works in most cases might be that most (if not all)
> users of this function copy the result into another buffer before overwriting
> the stack. But that's unsafe an dangerous :(


Actually, epsgCode is not a stack allocated buffer, but a global one (see static
keyword), so msOWSGetEPSGProj() is technically correct. However, in a
multithreaded usage, it would be unsafe since several threads could attempt to
use that buffer at the same time.

> Any oppinions on that and how to deal with it?

Your solution is the most standard one (similarly to what msOWSGetProjURN does).
Another one would be that the user provides the output buffer as a parameter,
but that kind be dangerous in case the provided buffer isn't large enough.


Spatialys - Geospatial professional services

More information about the mapserver-dev mailing list