Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42220x 42220x 42220x 42220x 42220x 42220x 42220x 42220x 42220x 42220x 42220x 42220x 42220x 42220x 42220x 42220x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21111x 21111x 21111x 21111x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 103514x 21111x 21111x 20205x 20205x 21111x 21111x 385x 385x 21111x 21111x 450x 450x 450x 13x 13x 437x 437x 450x 142x 142x 142x 4x 4x 138x 142x 793x 793x 712x 793x 2x 81x 79x 6x 6x 73x 793x 20x 20x 53x 53x 53x 53x 138x 437x 437x 437x 21111x 21111x 21111x 21111x 793x 793x 793x 793x 793x 51x 793x 7x 742x 11x 735x 9x 724x 7x 7x 7x 793x 708x 708x 85x 793x 78x 78x 78x 78x 85x 85x 85x 85x 793x 85x 85x 85x 85x 85x 2x 2x 83x 85x 2x 2x 81x 81x 793x 81x 81x 81x 793x 81x 81x 81x 81x 81x 81x 21111x 21111x 21111x 73x 73x 2x 2x 71x 73x 749x 2x 2x 749x 69x 69x 73x 69x 69x 73x 224x 224x 224x 224x 224x 72x 373x 72x 72x 301x 301x 224x 7x 7x 3x 7x 1x 1x 152x 1x 1x 1x 145x 224x 64x 64x 64x 21111x 21111x 21111x 622x 311x 311x 311x 311x 311x 622x 240x 240x 3x 240x 3x 237x 2x 234x 9x 9x 223x 223x 71x 71x 622x 66x 66x 66x 66x 3x 3x 63x 66x 61x 61x 2x 2x 66x 622x 2x 2x 1x 1x 2x 2x 3x 3x 3x 21111x 21111x 10x 10x 10x 10x 10x 10x 10x 21111x 21111x 21111x 73x 73x 73x 75x 75x 75x 72x 72x 75x 1x 1x 21111x 21111x 72x 72x 69x 69x 3x 72x 72x 21111x 21111x 680x 680x 680x 21111x 21111x 21111x 680x 415x 415x 265x 680x 2x 2x 680x 101x 101x 101x 263x 680x 680x 21111x 21111x 232x 44x 44x 9x 9x 44x 223x 223x 21111x 21111x 21111x 234x 234x 234x 223x 223x 234x 1x 234x 10x 9x 9x 9x 9x 9x 9x 1x 1x 21111x 21111x 21111x 9x 9x 9x 21111x 21111x 21111x 68x 68x 68x 21111x 1x 11x 11x 11x | import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import * as Structures from "../abap/3_structures/structures";
import {IStatement, MacroCall, MacroContent, MacroRecursion, Unknown} from "../abap/2_statements/statements/_statement";
import {ExpressionNode, StatementNode, StructureNode, TokenNode} from "../abap/nodes";
import {isComponentName} from "./_type_lookup";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {AbstractToken} from "../abap/1_lexer/tokens/abstract_token";
import {condImpliesNotInitial, negationImpliesNotInitial} from "./_initial_condition";
import {structurePath} from "./_structure_path";
export class DynamicWhereEmptyConf extends BasicRuleConfig {
/** Check SELECT and SELECT loops */
public select: boolean = true;
/** Check OPEN CURSOR */
public openCursor: boolean = true;
/** Check DELETE FROM dbtab */
public deleteDatabase: boolean = true;
/** Check UPDATE dbtab SET */
public updateDatabase: boolean = true;
/** Check MODIFY dbtab FROM ( SELECT ... ) */
public modifyDatabase: boolean = true;
/** Database tables not checked, eg. customizing tables a full read of which is fine, case insensitive */
public ignoreTables: string[] = [];
/** Skip statements with UP TO n ROWS, the read is bounded */
public skipUpTo: boolean = true;
}
// statements after which the rest of the block is not executed
const LEAVING: (new () => IStatement)[] = [Statements.Return, Statements.Exit, Statements.Continue,
Statements.Raise, Statements.Leave, Statements.Stop, Statements.Reject];
// accepted in the body of IF lv IS INITIAL
const GUARD_EXITS: (new () => IStatement)[] = [...LEAVING, Statements.Message, Statements.Check];
// statements which can change the variable without it being a target
const ESCAPING: (new () => IStatement)[] = [Statements.Assign, Statements.GetReference,
Statements.Perform, Statements.CallFunction];
interface IDynamicWhere {
/** the WHERE operand, a single variable */
token: AbstractToken;
name: string;
table: string | undefined;
upTo: boolean;
}
export class DynamicWhereEmpty extends ABAPRule {
private conf = new DynamicWhereEmptyConf();
public getMetadata(): IRuleMetadata {
return {
key: "dynamic_where_empty",
title: "Dynamic WHERE can be empty",
shortDescription: `Reports a dynamic WHERE (lv) whose variable can reach the statement empty`,
extendedInformation: `An empty dynamic WHERE is no condition: SELECT and OPEN CURSOR read the whole table,
DELETE FROM, UPDATE and MODIFY FROM ( SELECT ... ) change every row.
Checked are SELECT, OPEN CURSOR, DELETE FROM, UPDATE and MODIFY FROM ( SELECT ... ) whose WHERE is a single
dynamic token (lv), lv being a local variable of the method, form or function module, declared by DATA without
VALUE (or with VALUE IS INITIAL or an empty literal).
The statement is reported when no path through the procedure before the statement definitely gives lv a value,
ie. every assignment to lv sits in a conditional block (IF without ELSE, a branch of an IF or CASE where not all
branches assign, a LOOP, WHILE, DO or SELECT loop body, which can run zero times, a TRY block).
Not reported:
* a WHERE with a static condition besides the dynamic token, eg. WHERE mandt = @sy-mandt AND (lv)
* an unconditional assignment to lv before the statement, other than CLEAR or an empty literal
* a guard on lv: IF lv IS INITIAL with RETURN, EXIT, RAISE etc. before the statement,
CHECK lv IS NOT INITIAL, ASSERT lv IS NOT INITIAL, or the statement inside IF lv IS NOT INITIAL
* UP TO n ROWS, see option skipUpTo
* lv being a parameter, attribute or call result, or lv passed to PERFORM, CALL FUNCTION, ASSIGN or a reference,
as the value is not known
* procedures containing macros
Subqueries are not checked.`,
tags: [RuleTag.SingleFile],
badExample: `METHOD read.
DATA lv_where TYPE string.
IF iv_matnr IS NOT INITIAL.
lv_where = |MATNR = @IV_MATNR|.
ENDIF.
SELECT * FROM marc WHERE (lv_where) INTO TABLE @rt_marc.
ENDMETHOD.`,
goodExample: `METHOD read.
DATA lv_where TYPE string.
IF iv_matnr IS NOT INITIAL.
lv_where = |MATNR = @IV_MATNR|.
ENDIF.
IF lv_where IS INITIAL.
RETURN.
ENDIF.
SELECT * FROM marc WHERE (lv_where) INTO TABLE @rt_marc.
ENDMETHOD.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: DynamicWhereEmptyConf) {
this.conf = conf;
}
public runParsed(file: ABAPFile): Issue[] {
const issues: Issue[] = [];
const structure = file.getStructure();
if (structure === undefined) {
return [];
}
const procedures = structure.findAllStructuresMulti([Structures.Method, Structures.Form, Structures.FunctionModule]);
for (const procedure of procedures) {
const statements = procedure.findAllStatementNodes();
if (statements.some(s => s.get() instanceof MacroCall || s.get() instanceof MacroContent
|| s.get() instanceof MacroRecursion || s.get() instanceof Unknown)) {
continue;
}
for (const statement of statements) {
const found = this.findDynamicWhere(statement);
if (found === undefined) {
continue;
} else if (found.upTo === true && this.conf.skipUpTo === true) {
continue;
} else if (found.table !== undefined
&& (this.conf.ignoreTables || []).some(t => t.toUpperCase() === found.table)) {
continue;
}
if (this.canBeEmpty(procedure, statements, statement, found.name) === false) {
continue;
}
const message = `Dynamic WHERE (${found.token.getStr()}) can be empty here, the statement then works on every row`;
issues.push(Issue.atToken(file, found.token, message, this.getMetadata().key, this.conf.severity));
}
}
return issues;
}
/////////////////////////////////
private findDynamicWhere(statement: StatementNode): IDynamicWhere | undefined {
const type = statement.get();
let top: StatementNode | ExpressionNode | undefined = undefined;
let table: string | undefined = undefined;
if ((type instanceof Statements.Select || type instanceof Statements.SelectLoop) && this.conf.select === true) {
top = statement.findDirectExpression(Expressions.Select);
} else if (type instanceof Statements.OpenCursor && this.conf.openCursor === true) {
top = statement.findDirectExpression(Expressions.Select);
} else if (type instanceof Statements.DeleteDatabase && this.conf.deleteDatabase === true) {
top = statement;
} else if (type instanceof Statements.UpdateDatabase && this.conf.updateDatabase === true) {
top = statement;
} else if (type instanceof Statements.ModifyDatabase && this.conf.modifyDatabase === true) {
top = statement.findDirectExpression(Expressions.Select);
table = statement.findDirectExpression(Expressions.DatabaseTable)?.concatTokens().toUpperCase();
}
if (top === undefined) {
return undefined;
}
if (table === undefined) {
const from = top.findDirectExpression(Expressions.SQLFrom);
table = (from ? from.findFirstExpression(Expressions.DatabaseTable) : top.findDirectExpression(Expressions.DatabaseTable))
?.concatTokens().toUpperCase();
}
const children = top.getChildren();
const whereIndex = children.findIndex(c => c instanceof TokenNode && c.getFirstToken().getStr().toUpperCase() === "WHERE");
let operand = children[whereIndex + 1];
if (whereIndex < 0 || !(operand instanceof ExpressionNode)) {
return undefined;
}
if (operand.get() instanceof Expressions.SQLCond) {
const compare = operand.getChildren();
if (compare.length !== 1 || !(compare[0] instanceof ExpressionNode)
|| !(compare[0].get() instanceof Expressions.SQLCompare)) {
return undefined;
}
const dynamic = compare[0].getChildren();
if (dynamic.length !== 1 || !(dynamic[0] instanceof ExpressionNode)) {
return undefined;
}
operand = dynamic[0];
}
if (!(operand instanceof ExpressionNode) || !(operand.get() instanceof Expressions.Dynamic)) {
return undefined;
}
const chain = operand.findDirectExpression(Expressions.FieldChain);
if (chain === undefined || chain.getChildren().length !== 1
|| !(chain.getFirstChild()?.get() instanceof Expressions.SourceField)) {
return undefined;
}
const token = chain.getFirstToken();
const upTo = top.findDirectExpression(Expressions.SQLUpTo) !== undefined;
return {token, name: token.getStr().toUpperCase(), table, upTo};
}
/** true if lv can be empty when the statement is reached */
private canBeEmpty(procedure: StructureNode, statements: StatementNode[], statement: StatementNode, name: string): boolean {
const declaration = this.findDeclaration(procedure, name);
if (declaration === undefined || this.isEmptyDeclaration(declaration) === false) {
return false;
}
for (const s of statements) {
if (s !== statement && this.escapes(s, name)) {
return false;
}
}
const path = structurePath(procedure, statement);
if (path === undefined) {
return false;
}
let assigned = false;
for (let i = 0; i < path.length; i++) {
const container = path[i];
const child = path[i + 1] ?? statement;
const type = container.get();
if (type instanceof Structures.Body) {
for (const sibling of container.getChildren()) {
if (sibling === child) {
break;
}
assigned = this.apply(assigned, sibling as StructureNode | StatementNode, name);
}
} else if (type instanceof Structures.If) {
const cond = container.findDirectStatement(Statements.If);
if (child.get() instanceof Structures.Body && this.isPositive(cond, name)) {
return false;
} else if (child.get() instanceof Structures.Else && this.isNegative(cond, name)) {
return false;
}
} else if (type instanceof Structures.ElseIf) {
if (child.get() instanceof Structures.Body && this.isPositive(container.findDirectStatement(Statements.ElseIf), name)) {
return false;
}
} else if (type instanceof Structures.While) {
if (child.get() instanceof Structures.Body && this.isPositive(container.findDirectStatement(Statements.While), name)) {
return false;
}
}
}
return assigned === false;
}
/** the state "lv is definitely set, or the statement is not reached" after the node */
private apply(assigned: boolean, node: StructureNode | StatementNode, name: string): boolean {
if (node instanceof StructureNode && node.get() instanceof Structures.Normal) {
const first = node.getChildren()[0];
if (first === undefined) {
return assigned;
}
return this.apply(assigned, first as StructureNode | StatementNode, name);
}
if (node instanceof StatementNode) {
const type = node.get();
if (LEAVING.some(l => type instanceof l)) {
return true;
} else if ((type instanceof Statements.Check || type instanceof Statements.Assert) && this.isPositive(node, name)) {
return true;
} else if (this.isEmptyWrite(node, name)) {
return false;
} else if (this.isWrite(node, name)) {
return true;
}
return assigned;
}
const type = node.get();
if (type instanceof Structures.If) {
const cond = node.findDirectStatement(Statements.If);
const body = node.findDirectStructure(Structures.Body);
if (this.isNegative(cond, name)
&& (this.evalBody(body, name) || GUARD_EXITS.some(g => body?.findFirstStatement(g) !== undefined))) {
return true;
}
const elseNode = node.findDirectStructure(Structures.Else);
if (elseNode === undefined) {
return assigned;
}
const bodies = [body, ...node.findDirectStructures(Structures.ElseIf).map(e => e.findDirectStructure(Structures.Body)),
elseNode.findDirectStructure(Structures.Body)];
return bodies.every(b => this.evalBody(b, name)) ? true : assigned;
} else if (type instanceof Structures.Case) {
const whens = node.findDirectStructures(Structures.When);
if (whens.some(w => w.findDirectStatement(Statements.WhenOthers) !== undefined) === false) {
return assigned;
}
return whens.every(w => this.evalBody(w.findDirectStructure(Structures.Body), name)) ? true : assigned;
}
return assigned;
}
private evalBody(body: StructureNode | undefined, name: string): boolean {
if (body === undefined) {
return false;
}
let assigned = false;
for (const child of body.getChildren()) {
assigned = this.apply(assigned, child as StructureNode | StatementNode, name);
}
return assigned;
}
/** DATA lv in the procedure, not a component of a DATA BEGIN OF structure */
private findDeclaration(procedure: StructureNode, name: string): StatementNode | undefined {
const components = new Set<StatementNode>();
for (const s of procedure.findAllStructuresRecursive(Structures.Data)) {
for (const d of s.findAllStatements(Statements.Data)) {
components.add(d);
}
}
for (const data of procedure.findAllStatements(Statements.Data)) {
if (components.has(data)) {
continue;
}
const defName = data.findDirectExpression(Expressions.DataDefinition)?.findDirectExpression(Expressions.DefinitionName);
if (defName?.concatTokens().toUpperCase() === name) {
return data;
}
}
return undefined;
}
private isEmptyDeclaration(data: StatementNode): boolean {
const value = data.findDirectExpression(Expressions.DataDefinition)?.findDirectExpression(Expressions.Value);
if (value === undefined) {
return true;
}
const concat = value.concatTokens().toUpperCase().replace(/\s+/g, " ");
return concat === "VALUE IS INITIAL" || isEmptyLiteral(concat.substring("VALUE ".length));
}
private nameTokens(statement: StatementNode, name: string) {
const tokens = statement.getTokens();
return tokens.filter((t, i) => t.getStr().toUpperCase() === name && !isComponentName(tokens, i));
}
/** lv can be changed through a reference, field symbol or a call where it is not a target */
private escapes(statement: StatementNode, name: string): boolean {
if (this.nameTokens(statement, name).length === 0) {
return false;
}
const type = statement.get();
if (ESCAPING.some(e => type instanceof e)) {
return true;
}
for (const source of statement.findAllExpressionsRecursive(Expressions.Source)) {
if (source.getFirstToken().getStr().toUpperCase() === "REF"
&& source.getTokens().some(t => t.getStr().toUpperCase() === name)) {
return true;
}
}
return statement.findTokenSequencePosition("REFERENCE", "INTO") !== undefined
|| statement.findFirstExpression(Expressions.FSTarget) !== undefined;
}
private isWrite(statement: StatementNode, name: string): boolean {
for (const target of statement.findAllExpressionsRecursive(Expressions.Target)) {
if (target.getFirstChild()?.get() instanceof Expressions.TargetField
&& target.getFirstToken().getStr().toUpperCase() === name) {
return true;
}
}
return false;
}
/** CLEAR lv, FREE lv, REFRESH lv, lv = '' and the like */
private isEmptyWrite(statement: StatementNode, name: string): boolean {
const type = statement.get();
const targets = statement.findDirectExpressions(Expressions.Target);
if (targets.length !== 1 || targets[0].concatTokens().toUpperCase() !== name) {
return false;
}
if (type instanceof Statements.Clear) {
return statement.findDirectTokenByText("WITH") === undefined;
} else if (type instanceof Statements.Free || type instanceof Statements.Refresh) {
return true;
} else if (type instanceof Statements.Move) {
const source = statement.findDirectExpression(Expressions.Source);
const tokens = statement.getTokens();
if (source === undefined || tokens[1]?.getStr() !== "=") {
return false;
}
const concat = source.concatTokens().toUpperCase().replace(/\s+/g, " ");
return isEmptyLiteral(concat) || concat === "VALUE #( )";
}
return false;
}
/** the condition holds only if lv is not initial */
private isPositive(statement: StatementNode | undefined, name: string): boolean {
const cond = statement?.findDirectExpression(Expressions.Cond);
return cond !== undefined && condImpliesNotInitial(cond, name);
}
/** the condition fails only if lv is not initial */
private isNegative(statement: StatementNode | undefined, name: string): boolean {
const cond = statement?.findDirectExpression(Expressions.Cond);
return cond !== undefined && negationImpliesNotInitial(cond, name);
}
}
function isEmptyLiteral(text: string): boolean {
return /^'\s*'$/.test(text) || text === "``" || text === "||";
}
|