Skip to content

Commit 342b042

Browse files
authored
Fix(parser): Prevent crash with non-existent layer in REQUIRES (MapServer#7334)
* Fix(parser): Prevent crash with non-existent layer in REQUIRES When a REQUIRES or LABELREQUIRES expression contains a reference to a layer that does not exist in the mapfile, the expression parser would treat it as an attribute binding. Since msEvalContext is called without a shape object, this would lead to a NULL pointer dereference and a crash when the parser tried to evaluate the binding. This patch adds a check after tokenizing the expression to ensure no attribute binding tokens are present. If any are found, it indicates a reference to a non-existent layer. An error is reported and the evaluation returns MS_FALSE, preventing the crash. Fix generated by gemini-2.5-pro * Add tests with non existant REQUIRES parameter triggering a failure
1 parent 3c18f7e commit 342b042

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

msautotest/query/context.map

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# RUN_PARMS: context_test001.png [MAP2IMG] -m [MAPFILE] -l "bdry_counpy2" -o [RESULT]
77
# RUN_PARMS: context_test002.png [MAP2IMG] -m [MAPFILE] -l "bdry_counpy2 indx_q100kpy4" -o [RESULT]
88
# RUN_PARMS: context_test003.png [MAP2IMG] -m [MAPFILE] -l "indx_q100kpy4" -o [RESULT]
9+
# RUN_PARMS: context_test004.png [MAP2IMG] -m [MAPFILE] -l "req_fail" -o [RESULT]
10+
# RUN_PARMS: context_test005.png [MAP2IMG] -m [MAPFILE] -l "lreq_fail" -o [RESULT]
911
#
1012
MAP
1113
NAME 'context'
@@ -25,4 +27,16 @@ MAP
2527
REQUIRES '![bdry_counpy2]'
2628
INCLUDE 'include/indx_q100kpy4_shapefile.map'
2729
END
30+
31+
LAYER
32+
NAME 'req_fail'
33+
REQUIRES '[non_existant]'
34+
INCLUDE 'include/bdry_counpy2_shapefile.map'
35+
END
36+
37+
LAYER
38+
NAME 'lreq_fail'
39+
LABELREQUIRES '[non_existant]'
40+
INCLUDE 'include/bdry_counpy2_shapefile.map'
41+
END
2842
END
1.04 KB
Loading
24.5 KB
Loading

src/maputil.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ int msValidateContexts(mapObj *map) {
574574
int msEvalContext(mapObj *map, layerObj *layer, char *context) {
575575
int i, status;
576576
char *tag = NULL;
577+
tokenListNodeObjPtr token = NULL;
577578

578579
expressionObj e;
579580
parseObj p;
@@ -609,6 +610,24 @@ int msEvalContext(mapObj *map, layerObj *layer, char *context) {
609610

610611
msTokenizeExpression(&e, NULL, NULL);
611612

613+
/*
614+
* We'll check for binding tokens in the token list. Since there is no shape
615+
* to bind to, the parser will crash if any are present.
616+
* This is one way to catch references to non-existent layers.
617+
*/
618+
for (token = e.tokens; token; token = token->next) {
619+
if (token->token == MS_TOKEN_BINDING_DOUBLE ||
620+
token->token == MS_TOKEN_BINDING_STRING ||
621+
token->token == MS_TOKEN_BINDING_TIME) {
622+
msSetError(MS_PARSEERR,
623+
"A non-existent layer is referenced in a LAYER REQUIRES or "
624+
"LABELREQUIRES expression: %s",
625+
"msEvalContext()", e.string);
626+
msFreeExpression(&e);
627+
return MS_FALSE;
628+
}
629+
}
630+
612631
p.shape = NULL;
613632
p.expr = &e;
614633
p.expr->curtoken = p.expr->tokens; /* reset */

0 commit comments

Comments
 (0)