Skip to content

Commit

Permalink
Fix various memory leaks found when running msautotest
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Sep 11, 2019
1 parent 41b9be3 commit 836b4a7
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 184 deletions.
4 changes: 3 additions & 1 deletion mapcopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ int msCopyFontSet(fontSetObj *dst, fontSetObj *src, mapObj *map)

int msCopyExpression(expressionObj *dst, expressionObj *src)
{
if((dst->type == MS_REGEX) && dst->compiled) ms_regfree(&(dst->regex));
dst->compiled = MS_FALSE;

MS_COPYSTRING(dst->string, src->string);
MS_COPYSTELEM(type);
MS_COPYSTELEM(flags);
dst->compiled = MS_FALSE;

return MS_SUCCESS;
}
Expand Down
2 changes: 2 additions & 0 deletions mapfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6529,6 +6529,7 @@ static int loadMapInternal(mapObj *map)
if(getInteger(&(map->imagequality)) == -1) return MS_FAILURE;
break;
case(IMAGETYPE):
msFree(map->imagetype);
map->imagetype = getToken();
break;
case(INTERLACE):
Expand Down Expand Up @@ -6896,6 +6897,7 @@ int msUpdateMapFromURL(mapObj *map, char *variable, char *string)

/* TODO: should validate or does msPostMapParseOutputFormatSetup() do enough? */

msFree(map->imagetype);
map->imagetype = getToken();
msPostMapParseOutputFormatSetup( map );
break;
Expand Down
5 changes: 4 additions & 1 deletion maplabel.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,11 @@ int msAddLabelGroup(mapObj *map, imageObj *image, layerObj* layer, int classinde
msPopulateTextSymbolForLabelAndString(ts,lbl,annotext,layerPtr->scalefactor,image->resolutionfactor, 1);

if(annotext && *annotext && lbl->autominfeaturesize && featuresize > 0) {
if(UNLIKELY(MS_FAILURE == msComputeTextPath(map,ts)))
if(UNLIKELY(MS_FAILURE == msComputeTextPath(map,ts))) {
freeTextSymbol(ts);
free(ts);
return MS_FAILURE;
}
if(featuresize < (ts->textpath->bounds.bbox.maxx - ts->textpath->bounds.bbox.minx)) {
/* feature is too big to be drawn, skip it */
freeTextSymbol(ts);
Expand Down
2 changes: 1 addition & 1 deletion maplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,7 @@ msINLINELayerInfo;

int msINLINELayerIsOpen(layerObj *layer)
{
if (layer->currentfeature)
if (layer->layerinfo)
return(MS_TRUE);
else
return(MS_FALSE);
Expand Down
284 changes: 162 additions & 122 deletions mapmetadata.c

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions mapogcsld.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ static inline void IGUR_sizet(size_t ignored) { (void)ignored; } /* Ignore GCC
/* on the map. Layer name and Named Layer's name parameter are */
/* used to do the match. */
/************************************************************************/
int msSLDApplySLDURL(mapObj *map, char *szURL, int iLayer,
char *pszStyleLayerName, char **ppszLayerNames)
int msSLDApplySLDURL(mapObj *map, const char *szURL, int iLayer,
const char *pszStyleLayerName, char **ppszLayerNames)
{
#ifdef USE_OGR

Expand Down Expand Up @@ -116,6 +116,8 @@ int msSLDApplySLDURL(mapObj *map, char *szURL, int iLayer,
}
}

msFree(pszSLDbuf);

return nStatus;

#else
Expand Down Expand Up @@ -143,7 +145,7 @@ int msSLDApplySLDURL(mapObj *map, char *szURL, int iLayer,
/* they have the same name, copy the classes asscoaited with */
/* the SLD layers onto the map layers. */
/************************************************************************/
int msSLDApplySLD(mapObj *map, char *psSLDXML, int iLayer, char *pszStyleLayerName, char **ppszLayerNames)
int msSLDApplySLD(mapObj *map, const char *psSLDXML, int iLayer, const char *pszStyleLayerName, char **ppszLayerNames)
{
#if defined(USE_WMS_SVR) || defined (USE_WFS_SVR) || defined (USE_WCS_SVR) || defined(USE_SOS_SVR)

Expand Down Expand Up @@ -486,7 +488,7 @@ int msSLDApplySLD(mapObj *map, char *psSLDXML, int iLayer, char *pszStyleLayerNa
/* Returns an array of mapserver layers. The pnLayres if */
/* provided will indicate the size of the returned array. */
/************************************************************************/
layerObj *msSLDParseSLD(mapObj *map, char *psSLDXML, int *pnLayers)
layerObj *msSLDParseSLD(mapObj *map, const char *psSLDXML, int *pnLayers)
{
CPLXMLNode *psRoot = NULL;
CPLXMLNode *psSLD, *psNamedLayer, *psChild, *psName;
Expand Down Expand Up @@ -908,6 +910,7 @@ int msSLDParseNamedLayer(CPLXMLNode *psRoot, layerObj *psLayer)
char* pszExpression = msSLDGetCommonExpressionFromFilter(psFilter,
psLayer);
if (pszExpression) {
msFreeExpression(&psLayer->filter);
msInitExpression(&psLayer->filter);
psLayer->filter.string = pszExpression;
psLayer->filter.type = MS_EXPRESSION;
Expand Down Expand Up @@ -1283,6 +1286,7 @@ int msSLDParseOgcExpression(CPLXMLNode *psRoot, void *psObj, int binding,
literalString = msStringConcatenate(literalString, "(");
literalString = msStringConcatenate(literalString, psRoot->pszValue);
literalString = msStringConcatenate(literalString, ")");
msFreeExpression(&(exprBindings[binding]));
msInitExpression(&(exprBindings[binding]));
exprBindings[binding].string = literalString;
exprBindings[binding].type = MS_STRING;
Expand Down
10 changes: 5 additions & 5 deletions mapogcsld.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
#include "mapserver.h"

MS_DLL_EXPORT char *msSLDGenerateSLD(mapObj *map, int iLayer, const char *pszVersion);
MS_DLL_EXPORT int msSLDApplySLDURL(mapObj *map, char *szURL, int iLayer,
char *pszStyleLayerName, char **ppszLayerNames);
MS_DLL_EXPORT int msSLDApplySLD(mapObj *map, char *psSLDXML, int iLayer,
char *pszStyleLayerName, char **ppszLayerNames);
MS_DLL_EXPORT int msSLDApplySLDURL(mapObj *map, const char *szURL, int iLayer,
const char *pszStyleLayerName, char **ppszLayerNames);
MS_DLL_EXPORT int msSLDApplySLD(mapObj *map, const char *psSLDXML, int iLayer,
const char *pszStyleLayerName, char **ppszLayerNames);

#ifdef USE_OGR

Expand All @@ -44,7 +44,7 @@ enum objType { MS_OBJ_STYLE, MS_OBJ_LABEL };
/* -------------------------------------------------------------------- */
/* prototypes. */
/* -------------------------------------------------------------------- */
layerObj *msSLDParseSLD(mapObj *map, char *psSLDXML, int *pnLayers);
layerObj *msSLDParseSLD(mapObj *map, const char *psSLDXML, int *pnLayers);
int msSLDParseNamedLayer(CPLXMLNode *psRoot, layerObj *layer);
int msSLDParseRule(CPLXMLNode *psRoot, layerObj *psLayer);
int msSLDParseStroke(CPLXMLNode *psStroke, styleObj *psStyle,
Expand Down
33 changes: 24 additions & 9 deletions mapogcsos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1713,10 +1713,8 @@ int msSOSGetObservation(mapObj *map, sosParamsObj *sosparams, cgiRequestObj *req
xmlNodePtr psObservationNode = NULL, psResultNode=NULL;
const char *pszProcedure = NULL;
const char *pszBlockSep=NULL;
char *pszResult=NULL;
int nDiffrentProc = 0;
SOSProcedureNode *paDiffrentProc = NULL;
char *pszProcedureValue = NULL;
int iItemPosition, status;
shapeObj sShape;
char* pszEscapedStr = NULL;
Expand Down Expand Up @@ -2301,6 +2299,7 @@ this request. Check sos/ows_enable_request settings.", "msSOSGetObservation()",
if (tokens==NULL || n != 4) {
msSetError(MS_SOSERR, "Wrong number of arguments for sos_offering_extent.",
"msSOSGetCapabilities()");
msFree(script_url);
return msSOSException(map, "sos_offering_extent", "InvalidParameterValue");
}

Expand All @@ -2312,6 +2311,7 @@ this request. Check sos/ows_enable_request settings.", "msSOSGetObservation()",
if (map && msProjectionsDiffer(&map->projection, &lp->projection) == MS_TRUE) {
if (msProjectRect(&lp->projection, &map->projection, &envelope) == MS_FAILURE) {
msSetError(MS_SOSERR, "Coordinates transformation failed. Raised in msProjectRect() of file %s line %d", "msSOSGetCapabilities()", __FILE__, __LINE__);
msFree(script_url);
return msSOSException(map, "sos_offering_extent", "InvalidParameterValue");
}
}
Expand Down Expand Up @@ -2350,6 +2350,14 @@ this request. Check sos/ows_enable_request settings.", "msSOSGetObservation()",
msSetError(MS_SOSERR, "resultModel should be om:Measurement or om:Observation", "msSOSGetObservation()");
free(xsi_schemaLocation);
free(schemalocation);
msFree(script_url);
xmlFreeNs(psNsSos);
xmlFreeNs(psNsGml);
xmlFreeNs(psNsOm);
xmlFreeNs(psNsSwe);
xmlFreeNs(psNsXLink);
xmlFreeNs(psNsMs);
xmlFreeDoc(psDoc);
return msSOSException(map, "resultModel", "InvalidParameterValue");
}

Expand Down Expand Up @@ -2389,10 +2397,12 @@ this request. Check sos/ows_enable_request settings.", "msSOSGetObservation()",
else
xmlNodeAddContent(psResultNode, BAD_CAST "\n");
}
pszResult = msSOSReturnMemberResult((GET_LAYER(map, i)), j, NULL);
if (pszResult) {
xmlNodeAddContent(psResultNode, BAD_CAST pszResult);
msFree(pszResult);
{
char* pszResult = msSOSReturnMemberResult((GET_LAYER(map, i)), j, NULL);
if (pszResult) {
xmlNodeAddContent(psResultNode, BAD_CAST pszResult);
msFree(pszResult);
}
}
}
}
Expand All @@ -2402,9 +2412,14 @@ this request. Check sos/ows_enable_request settings.", "msSOSGetObservation()",
else {

for(j=0; j<GET_LAYER(map, i)->resultcache->numresults; j++) {
pszResult = msSOSReturnMemberResult((GET_LAYER(map, i)), j, &pszProcedureValue);
char* pszProcedureValue = NULL;
char* pszResult = msSOSReturnMemberResult((GET_LAYER(map, i)), j, &pszProcedureValue);
if (!pszProcedureValue || !pszResult)
{
msFree(pszProcedureValue);
msFree(pszResult);
continue;
}
for (k=0; k<nDiffrentProc; k++) {
if (strcasecmp(paDiffrentProc[k].pszProcedure, pszProcedureValue) == 0) {
pszBlockSep = msOWSLookupMetadata(&(map->web.metadata), "S",
Expand All @@ -2430,14 +2445,14 @@ this request. Check sos/ows_enable_request settings.", "msSOSGetObservation()",
psObservationNode = msSOSAddMemberNodeObservation(psNsGml, psNsSos, psNsOm, psNsSwe, psNsXLink, psRootNode, map,
(GET_LAYER(map, i)),
pszProcedureValue);
msFree(pszProcedureValue);

paDiffrentProc[nDiffrentProc-1].psResultNode =
xmlNewChild(psObservationNode, NULL, BAD_CAST "result", NULL);

xmlNodeAddContent(paDiffrentProc[nDiffrentProc-1].psResultNode, BAD_CAST pszResult);
msFree(pszResult);
}
msFree(pszProcedureValue);
msFree(pszResult);
}
if (paDiffrentProc) {
for (k=0; k<nDiffrentProc; k++)
Expand Down
17 changes: 16 additions & 1 deletion mapogroutput.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,8 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
"STORAGE=%s value not supported.",
"msOGRWriteFromQuery()",
storage );
CSLDestroy(layer_options);
CSLDestroy(ds_options);
return MS_FAILURE;
}

Expand Down Expand Up @@ -821,6 +823,9 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
"Attempt to create directory '%s' failed.",
"msOGRWriteFromQuery()",
dir_to_create );
msFree(request_dir);
CSLDestroy(layer_options);
CSLDestroy(ds_options);
return MS_FAILURE;
}
}
Expand Down Expand Up @@ -848,6 +853,9 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
"Invalid value for FILENAME option. "
"It must not contain any directory information.",
"msOGRWriteFromQuery()" );
msFree(request_dir);
CSLDestroy(layer_options);
CSLDestroy(ds_options);
return MS_FAILURE;
}

Expand Down Expand Up @@ -908,6 +916,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
"msOGRWriteFromQuery()",
datasource_name,
format->driver+4 );
CSLDestroy(layer_options);
return MS_FAILURE;
}

Expand Down Expand Up @@ -1015,6 +1024,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
"msOGRWriteFromQuery()",
layer->name,
format->driver+4 );
CSLDestroy(layer_options);
return MS_FAILURE;
}

Expand Down Expand Up @@ -1083,6 +1093,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
OGR_DS_Destroy( hDS );
msOGRCleanupDS( datasource_name );
msGMLFreeItems(item_list);
CSLDestroy(layer_options);
return MS_FAILURE;
}

Expand All @@ -1103,6 +1114,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
OGR_DS_Destroy( hDS );
msOGRCleanupDS( datasource_name );
msGMLFreeItems(item_list);
CSLDestroy(layer_options);
return status;
}
}
Expand Down Expand Up @@ -1133,6 +1145,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
msOGRCleanupDS( datasource_name );
msGMLFreeItems(item_list);
msFreeShape(&resultshape);
CSLDestroy(layer_options);
return status;
}
}
Expand Down Expand Up @@ -1184,6 +1197,7 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
msOGRCleanupDS( datasource_name );
msGMLFreeItems(item_list);
msFreeShape(&resultshape);
CSLDestroy(layer_options);
return status;
}
}
Expand All @@ -1197,6 +1211,8 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )
/* -------------------------------------------------------------------- */
OGR_DS_Destroy( hDS );

CSLDestroy( layer_options );

/* -------------------------------------------------------------------- */
/* Get list of resulting files. */
/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -1398,7 +1414,6 @@ int msOGRWriteFromQuery( mapObj *map, outputFormatObj *format, int sendheaders )

msOGRCleanupDS( datasource_name );

CSLDestroy( layer_options );
CSLDestroy( file_list );

return MS_SUCCESS;
Expand Down
6 changes: 5 additions & 1 deletion mapows.c
Original file line number Diff line number Diff line change
Expand Up @@ -2002,13 +2002,16 @@ int msOWSPrintEncodeMetadataList(FILE *stream, hashTableObj *metadata,
{
const char *value;
char *encoded;
size_t default_value_len = 0;

value = msOWSLookupMetadata(metadata, namespaces, name);

if(value == NULL) {
value = default_value;
default_value = NULL;
}
if( default_value )
default_value_len = strlen(default_value);

if(value != NULL) {
char **keywords;
Expand All @@ -2020,8 +2023,9 @@ int msOWSPrintEncodeMetadataList(FILE *stream, hashTableObj *metadata,
if(startTag) msIO_fprintf(stream, "%s", startTag);
for(kw=0; kw<numkeywords; kw++) {
if (default_value != NULL
&& default_value_len > 8
&& strncasecmp(keywords[kw],default_value,strlen(keywords[kw])) == 0
&& strncasecmp("_exclude",default_value+strlen(default_value)-8,8) == 0)
&& strncasecmp("_exclude",default_value+default_value_len-8,8) == 0)
continue;

encoded = msEncodeHTMLEntities(keywords[kw]);
Expand Down
1 change: 1 addition & 0 deletions mapquery.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ int msQueryByFilter(mapObj *map)
*/
lp->filteritem = map->query.filteritem; /* re-point lp->filteritem */
if(old_filter.string != NULL) { /* need to merge filters to create one logical expression */
msFreeExpression(&lp->filter);
lp->filter = mergeFilters(&map->query.filter, map->query.filteritem, &old_filter, old_filteritem);
if(!lp->filter.string) {
msSetError(MS_MISCERR, "Filter merge failed, able to process query.", "msQueryByFilter()");
Expand Down
Loading

0 comments on commit 836b4a7

Please sign in to comment.