Skip to content

Commit

Permalink
[Backport branch-7-6] Fix resource leak and pointObj initialization e…
Browse files Browse the repository at this point in the history
…rrors. (#6296)
  • Loading branch information
MapServer-backport-bot committed Apr 14, 2021
1 parent 49f813d commit e864683
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 51 deletions.
10 changes: 8 additions & 2 deletions mapdraw.c
Expand Up @@ -542,6 +542,13 @@ imageObj *msDrawMap(mapObj *map, int querymap)
if(map->legend.status == MS_EMBED && map->legend.postlabelcache)
if(UNLIKELY(MS_FAILURE == msEmbedLegend(map, image))) {
msFreeImage( image );
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
/* Cleanup WMS/WFS Request stuff */
if (pasOWSReqInfo) {
msHTTPFreeRequestObj(pasOWSReqInfo, numOWSRequests);
msFree(pasOWSReqInfo);
}
#endif
return NULL;
}

Expand All @@ -552,7 +559,6 @@ imageObj *msDrawMap(mapObj *map, int querymap)
if(map->gt.need_geotransform)
msMapRestoreRealExtent(map);


if(MS_SUCCESS != msEmbedScalebar(map, image)) {
msFreeImage( image );
#if defined(USE_WMS_LYR) || defined(USE_WFS_LYR)
Expand Down Expand Up @@ -1947,7 +1953,7 @@ int polygonLayerDrawShape(mapObj *map, imageObj *image, layerObj *layer,
{

int c = shape->classindex;
pointObj annopnt;
pointObj annopnt = {0,0,0,0}; // initialize
int i;

if(MS_DRAW_FEATURES(drawmode)) {
Expand Down
7 changes: 4 additions & 3 deletions mapfile.c
Expand Up @@ -1993,7 +1993,7 @@ void msFreeExpression(expressionObj *exp)

int loadExpression(expressionObj *exp)
{
/* TODO: should we fall msFreeExpression if exp->string != NULL? We do some checking to avoid a leak but is it enough... */
/* TODO: should we call msFreeExpression if exp->string != NULL? We do some checking to avoid a leak but is it enough... */

msyystring_icase = MS_TRUE;
if((exp->type = getSymbol(6, MS_STRING,MS_EXPRESSION,MS_REGEX,MS_ISTRING,MS_IREGEX,MS_LIST)) == -1) return(-1);
Expand All @@ -2002,6 +2002,7 @@ int loadExpression(expressionObj *exp)
msFree(exp->native_string);
}
exp->string = msStrdup(msyystring_buffer);
exp->native_string = NULL;

if(exp->type == MS_ISTRING) {
exp->flags = exp->flags | MS_EXP_INSENSITIVE;
Expand Down Expand Up @@ -6443,7 +6444,7 @@ mapObj *msLoadMapFromString(char *buffer, char *new_mappath)
MS_CHECK_ALLOC(map, sizeof(mapObj), NULL);

if(initMap(map) == -1) { /* initialize this map */
msFree(map);
msFreeMap(map);
return(NULL);
}

Expand Down Expand Up @@ -6535,7 +6536,7 @@ mapObj *msLoadMap(const char *filename, const char *new_mappath)
MS_CHECK_ALLOC(map, sizeof(mapObj), NULL);

if(initMap(map) == -1) { /* initialize this map */
msFree(map);
msFreeMap(map);
return(NULL);
}

Expand Down
26 changes: 14 additions & 12 deletions mapgml.c
Expand Up @@ -34,7 +34,7 @@
#include "maptime.h"


/* Use only mapgml.c if WMS or WFS is available (with minor exceptions at end)*/
/* Use only mapgml.c if WMS or WFS is available (with minor exceptions at end) */

#if defined(USE_WMS_SVR) || defined (USE_WFS_SVR)

Expand Down Expand Up @@ -1485,23 +1485,25 @@ int msGMLWriteQuery(mapObj *map, char *filename, const char *namespaces)
if(pszOutputSRS == pszMapSRS && msProjectionsDiffer(&(lp->projection), &(map->projection))) {
reprojector = msProjectCreateReprojector(&(lp->projection), &(map->projection));
if( reprojector == NULL ) {
msGMLFreeGroups(groupList);
msGMLFreeConstants(constantList);
msGMLFreeItems(itemList);
msGMLFreeGeometries(geometryList);
return MS_FAILURE;
msGMLFreeGroups(groupList);
msGMLFreeConstants(constantList);
msGMLFreeItems(itemList);
msGMLFreeGeometries(geometryList);
msFree(pszOutputSRS);
return MS_FAILURE;
}
}

for(j=0; j<lp->resultcache->numresults; j++) {
status = msLayerGetShape(lp, &shape, &(lp->resultcache->results[j]));
if(status != MS_SUCCESS) {
msGMLFreeGroups(groupList);
msGMLFreeConstants(constantList);
msGMLFreeItems(itemList);
msGMLFreeGeometries(geometryList);
msProjectDestroyReprojector(reprojector);
return(status);
msGMLFreeGroups(groupList);
msGMLFreeConstants(constantList);
msGMLFreeItems(itemList);
msGMLFreeGeometries(geometryList);
msProjectDestroyReprojector(reprojector);
msFree(pszOutputSRS);
return MS_FAILURE;
}

/* project the shape into the map projection (if necessary), note that this projects the bounds as well */
Expand Down
2 changes: 1 addition & 1 deletion maplabel.c
Expand Up @@ -881,7 +881,7 @@ int msGetTextSymbolSize(mapObj *map, textSymbolObj *ts, rectObj *r) {

pointObj get_metrics(pointObj *p, int position, textPathObj *tp, int ox, int oy, double rotation, int buffer, label_bounds *bounds)
{
pointObj q;
pointObj q = {0,0,0,0}; // initialize
double x1=0, y1=0, x2=0, y2=0;
double sin_a,cos_a;
double w, h, x, y;
Expand Down
10 changes: 7 additions & 3 deletions mapobject.c
Expand Up @@ -47,7 +47,7 @@ void freeLegend(legendObj *legend);

mapObj *msNewMapObj()
{
mapObj *map;
mapObj *map = NULL;

/* create an empty map, no layers etc... */
map = (mapObj *)calloc(sizeof(mapObj),1);
Expand All @@ -57,11 +57,15 @@ mapObj *msNewMapObj()
return NULL;
}

if( initMap( map ) == -1 )
if( initMap( map ) == -1 ) {
msFreeMap(map);
return NULL;
}

if( msPostMapParseOutputFormatSetup( map ) == MS_FAILURE )
if( msPostMapParseOutputFormatSetup( map ) == MS_FAILURE ) {
msFreeMap(map);
return NULL;
}

return map;
}
Expand Down
18 changes: 10 additions & 8 deletions mapows.c
Expand Up @@ -2587,13 +2587,14 @@ char *msOWSGetProjURN(projectionObj *proj, hashTableObj *metadata, const char *n
char **tokens;
int numtokens, i;
size_t bufferSize = 0;
char *oldStyle;
char *oldStyle = NULL;

msOWSGetEPSGProj( proj, metadata, namespaces,
bReturnOnlyFirstOne, &oldStyle );
msOWSGetEPSGProj( proj, metadata, namespaces, bReturnOnlyFirstOne, &oldStyle );

if( oldStyle == NULL || strncmp(oldStyle,"EPSG:",5) != 0 )
if( oldStyle == NULL || strncmp(oldStyle,"EPSG:",5) != 0 ) {
msFree(oldStyle);
return NULL;
}

result = msStrdup("");

Expand Down Expand Up @@ -2647,13 +2648,14 @@ char *msOWSGetProjURI(projectionObj *proj, hashTableObj *metadata, const char *n
char *result;
char **tokens;
int numtokens, i;
char *oldStyle;
char *oldStyle = NULL;

msOWSGetEPSGProj( proj, metadata, namespaces,
bReturnOnlyFirstOne, &oldStyle);
msOWSGetEPSGProj( proj, metadata, namespaces, bReturnOnlyFirstOne, &oldStyle);

if( oldStyle == NULL || !EQUALN(oldStyle,"EPSG:",5) )
if( oldStyle == NULL || !EQUALN(oldStyle,"EPSG:",5) ) {
msFree(oldStyle); // avoid leak
return NULL;
}

result = msStrdup("");

Expand Down
2 changes: 1 addition & 1 deletion mappostgis.c
Expand Up @@ -752,7 +752,7 @@ arcSegmentSide(const pointObj *p1, const pointObj *p2, const pointObj *q)
int
arcCircleCenter(const pointObj *p1, const pointObj *p2, const pointObj *p3, pointObj *center, double *radius)
{
pointObj c;
pointObj c = {0,0,0,0}; // initialize
double dx21, dy21, dx31, dy31, h21, h31, d, r;

/* Circle is closed, so p2 must be opposite p1 & p3. */
Expand Down
2 changes: 1 addition & 1 deletion mapprimitive.c
Expand Up @@ -1133,7 +1133,7 @@ void msTransformPixelToShape(shapeObj *shape, rectObj extent, double cellsize)
*/
static pointObj generateLineIntersection(pointObj a, pointObj b, pointObj c, pointObj d)
{
pointObj p;
pointObj p = {0,0,0,0}; // initialize
double r;
double denominator, numerator;

Expand Down
2 changes: 1 addition & 1 deletion mapproject.c
Expand Up @@ -1211,7 +1211,7 @@ static int msProjectShapeShouldDoLineCutting(reprojectionObj* reprojector)
return MS_FALSE;
}

pointObj p;
pointObj p = {0,0,0,0}; // initialize
double invgt0 = out->gt.need_geotransform ? out->gt.invgeotransform[0] : 0.0;
double invgt1 = out->gt.need_geotransform ? out->gt.invgeotransform[1] : 1.0;
double invgt3 = out->gt.need_geotransform ? out->gt.invgeotransform[3] : 0.0;
Expand Down
6 changes: 3 additions & 3 deletions mapsmoothing.c
Expand Up @@ -142,7 +142,7 @@ static int processShapePathDistance(shapeObj *shape, int force)

while ((res = nextLineWindow(&lw)) != MS_DONE) {
double ratio = 0;
pointObj point;
pointObj point = {0,0,0,0}; // initialize

if (lw.lineIsRing && lw.pos==lw.line->numpoints-1) {
point = newShape->line[i].point[0];
Expand Down Expand Up @@ -261,8 +261,8 @@ shapeObj* msSmoothShapeSIA(shapeObj *shape, int ss, int si, char *preprocessing)

while ((res = nextLineWindow(&lw)) != MS_DONE) {
double sum_x=0, sum_y=0, sum = 0;
pointObj point;
int k = 0;
pointObj point = {0,0,0,0}; // initialize
int k = 0;

if (res == MS_FALSE) { /* invalid window */
msAddPointToLine(&newShape->line[j],
Expand Down
11 changes: 8 additions & 3 deletions maptemplate.c
Expand Up @@ -1728,8 +1728,8 @@ static int processShplabelTag(layerObj *layer, char **line, shapeObj *origshape)
}

if(labelposvalid == MS_TRUE) {
pointObj p1;
pointObj p2;
pointObj p1 = {0,0,0,0}; // initialize
pointObj p2 = {0,0,0,0};
int label_offset_x, label_offset_y;
labelObj *label=NULL;
label_bounds lbounds;
Expand Down Expand Up @@ -3636,12 +3636,15 @@ char *processOneToManyJoin(mapservObj* mapserv, joinObj *join)
while(fgets(line, MS_BUFFER_LENGTH, stream) != NULL) outbuf = msStringConcatenate(outbuf, line);

fclose(stream);
stream = NULL;
}

/* clear any data associated with the join */
msFreeCharArray(join->values, join->numitems);
join->values = NULL;

if(stream) fclose(stream);

return(outbuf);
}

Expand Down Expand Up @@ -4219,8 +4222,10 @@ int msReturnPage(mapservObj *mapserv, char *html, int mode, char **papszBuffer)

if(strchr(line, '[') != NULL) {
tmpline = processLine(mapserv, line, stream, mode);
if(!tmpline)
if(!tmpline) {
fclose(stream);
return MS_FAILURE;
}

if(papszBuffer) {
if(nBufferSize <= (int)(nCurrentSize + strlen(tmpline) + 1)) {
Expand Down
4 changes: 3 additions & 1 deletion mapwcs.c
Expand Up @@ -1514,8 +1514,10 @@ static int msWCSGetCoverage_ImageCRSSetup(
/* -------------------------------------------------------------------- */
char *layer_proj = msGetProjectionString( &(layer->projection) );

if (msLoadProjectionString(&(map->projection), layer_proj) != 0)
if (msLoadProjectionString(&(map->projection), layer_proj) != 0) {
msFree(layer_proj);
return msWCSException( map, NULL, NULL, params->version );
}

free( layer_proj );
layer_proj = NULL;
Expand Down
1 change: 1 addition & 0 deletions mapwcs20.c
Expand Up @@ -3602,6 +3602,7 @@ int msWCSGetCapabilities20(mapObj *map, cgiRequestObj *req,
status = msWCSGetCapabilities20_CoverageSummary(
map, params, psDoc, psNode, layer );
if(status != MS_SUCCESS) {
msFree(validated_language);
xmlFreeDoc(psDoc);
xmlCleanupParser();
return msWCSException(map, "Internal", "mapserv", params->version);
Expand Down
9 changes: 3 additions & 6 deletions mapwfs.c
Expand Up @@ -2202,15 +2202,12 @@ static int msWFSRunBasicGetFeature(mapObj* map,
status = msLoadProjectionString(&(map->projection), pszMapSRS);

if (status != 0) {
msSetError(MS_WFSERR, "msLoadProjectionString() failed: %s",
"msWFSGetFeature()", pszMapSRS);
msSetError(MS_WFSERR, "msLoadProjectionString() failed: %s", "msWFSGetFeature()", pszMapSRS);
msFree(pszMapSRS);
return msWFSException(map, "mapserv", MS_OWS_ERROR_NO_APPLICABLE_CODE,
paramsObj->pszVersion);
return msWFSException(map, "mapserv", MS_OWS_ERROR_NO_APPLICABLE_CODE, paramsObj->pszVersion);
}
msFree(pszMapSRS);

}
msFree(pszMapSRS);

/*make sure that the layer projection is loaded.
It could come from a ows/wfs_srs metadata*/
Expand Down
18 changes: 12 additions & 6 deletions mapwmslayer.c
Expand Up @@ -430,7 +430,7 @@ msBuildWMSLayerURL(mapObj *map, layerObj *lp, int nRequestType,
int bFlipAxisOrder = MS_FALSE;
const char *pszTmp;
int bIsEssential = MS_FALSE;

if (lp->connectiontype != MS_WMS) {
msSetError(MS_WMSCONNERR, "Call supported only for CONNECTIONTYPE WMS",
"msBuildWMSLayerURL()");
Expand Down Expand Up @@ -619,24 +619,30 @@ msBuildWMSLayerURL(mapObj *map, layerObj *lp, int nRequestType,
char* pszEPSGCodeFromLayer = NULL;
msOWSGetEPSGProj(&(lp->projection), NULL, "MO", MS_TRUE, &pszEPSGCodeFromLayer);
if (pszEPSGCodeFromLayer == NULL || strcasecmp(pszEPSG, pszEPSGCodeFromLayer) != 0) {
char *ows_srs;
msOWSGetEPSGProj(NULL,&(lp->metadata), "MO", MS_FALSE, &ows_srs);
char *ows_srs = NULL;
msOWSGetEPSGProj(NULL, &(lp->metadata), "MO", MS_FALSE, &ows_srs);
/* no need to set lp->proj if it is already set and there is only
one item in the _srs metadata for this layer - we will assume
the projection block matches the _srs metadata (the search for ' '
in ows_srs is a test to see if there are multiple EPSG: codes) */
if( lp->projection.numargs == 0 || ows_srs == NULL || (strchr(ows_srs,' ') != NULL) ) {
msFree(ows_srs);
if (strncasecmp(pszEPSG, "EPSG:", 5) == 0) {
char szProj[20];
snprintf(szProj, sizeof(szProj), "init=epsg:%s", pszEPSG+5);
if (msLoadProjectionString(&(lp->projection), szProj) != 0)
if (msLoadProjectionString(&(lp->projection), szProj) != 0) {
msFree(pszEPSGCodeFromLayer);
msFree(ows_srs);
return MS_FAILURE;
}
} else {
if (msLoadProjectionString(&(lp->projection), pszEPSG) != 0)
if (msLoadProjectionString(&(lp->projection), pszEPSG) != 0) {
msFree(pszEPSGCodeFromLayer);
msFree(ows_srs);
return MS_FAILURE;
}
}
}
msFree(ows_srs);
}
msFree(pszEPSGCodeFromLayer);
}
Expand Down

0 comments on commit e864683

Please sign in to comment.