summaryrefslogtreecommitdiffstats
path: root/parse.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2017-09-06 23:05:35 -0400
committerTavian Barnes <tavianator@tavianator.com>2017-09-06 23:16:20 -0400
commite8557caace66b058e3048de7eff954baaa6c0493 (patch)
treecc1ee8f30b4b44bced71ce6a61dd500d2b45c049 /parse.c
parent4504dd3599404a916f104e0c715a9bcf3fb0062b (diff)
downloadbfs-e8557caace66b058e3048de7eff954baaa6c0493.tar.xz
parse: Don't reorder or remove tests with potential side effects
-empty and -xtype may have side effects like reporting permission errors, which even affect the exit status of bfs. We shouldn't remove these effects without -Ofast.
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/parse.c b/parse.c
index 811ec5a..f6cde7a 100644
--- a/parse.c
+++ b/parse.c
@@ -1003,10 +1003,20 @@ static struct expr *parse_depth_limit(struct parser_state *state, int is_min, in
*/
static struct expr *parse_empty(struct parser_state *state, int arg1, int arg2) {
struct expr *expr = parse_nullary_test(state, eval_empty);
- if (expr) {
- expr->cost = 2000.0;
- expr->probability = 0.01;
+ if (!expr) {
+ return NULL;
+ }
+
+ expr->cost = 2000.0;
+ expr->probability = 0.01;
+
+ if (state->cmdline->optlevel < 4) {
+ // Since -empty attempts to open and read directories, it may
+ // have side effects such as reporting permission errors, and
+ // thus shouldn't be re-ordered without aggressive optimizations
+ expr->pure = false;
}
+
return expr;
}
@@ -2103,6 +2113,14 @@ static struct expr *parse_type(struct parser_state *state, int x, int arg2) {
expr->idata = types;
expr->probability = probability;
+
+ if (x && state->cmdline->optlevel < 4) {
+ // Since -xtype dereferences symbolic links, it may have side
+ // effects such as reporting permission errors, and thus
+ // shouldn't be re-ordered without aggressive optimizations
+ expr->pure = false;
+ }
+
return expr;
fail: