All files / src/rules changing_sorted_line.ts

98.85% Statements 173/175
86.15% Branches 56/65
100% Functions 9/9
98.85% Lines 173/175

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 1761x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21099x 21099x 21099x 21099x 21099x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 63059x 21099x 21099x 20205x 20205x 21099x 21099x 395x 395x 395x 21099x 21099x 374x 374x 21099x 21099x 605x 229x 229x 376x 376x 376x 376x 605x 386x 386x 367x 367x 19x 19x 19x 19x 19x 19x 19x 386x 74x 74x 74x 231x 19x 19x 231x 231x 74x 19x 376x 376x 376x 21099x 21099x 21099x 21099x 19x 19x 38x 38x 18x 18x 18x 18x 2x 2x 16x 18x 18x 8x 8x 18x 18x 18x 18x 18x 18x 38x 38x 19x 21099x 21099x 21099x 231x 231x 231x 231x 231x 210x 210x 21x 21x 21x 21x 231x 6x 231x 10x 15x 2x 5x 3x 3x 3x 21x 231x 231x 231x 15x 231x 6x 6x 231x 21099x 21099x 21099x 21099x 20x 20x 20x     20x 20x 1x 1x 19x 19x 19x 21099x 21099x  
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 {BasicRuleConfig} from "./_basic_rule_config";
import {Issue} from "../issue";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {IObject} from "../objects/_iobject";
import {ABAPObject} from "../objects/_abap_object";
import {IRegistry} from "../_iregistry";
import {TableAccessType, TableType} from "../abap/types/basic";
import {AbstractType} from "../abap/types/basic/_abstract_type";
import {ExpressionNode, StatementNode} from "../abap/nodes";
import {ABAPFile} from "../abap/abap_file";
import {ISpaghettiScope} from "../abap/5_syntax/_spaghetti_scope";
import {INode} from "../abap/nodes/_inode";
import {chainType, findTypedIdentifier, operandChain} from "./_type_lookup";
 
export class ChangingSortedLineConf extends BasicRuleConfig {
}
 
export class ChangingSortedLine implements IRule {
  private reg: IRegistry;
  private conf = new ChangingSortedLineConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "changing_sorted_line",
      title: "Line of a sorted or hashed table passed to CHANGING",
      shortDescription: `A field symbol on a whole line of a sorted or hashed table, passed to a CHANGING parameter of a method`,
      extendedInformation: `The key fields of a sorted or hashed table line are write-protected, so the line as a whole is
write-protected. Binding it to a CHANGING parameter activates, but the call fails at runtime with
CX_SY_DYN_CALL_ILLEGAL_TYPE, "the actual parameter for ... is write-protected", even when the method does not
change the key.
 
Only field symbols assigned in the same method, form or function module are checked, via READ TABLE ... ASSIGNING,
LOOP AT ... ASSIGNING, INSERT ... INTO TABLE ... ASSIGNING and ASSIGN itab[ ... ] TO.
 
Fix: read the line into a work area, pass the work area and MODIFY TABLE ... FROM it afterwards,
or pass only the non-key fields the method needs.`,
      tags: [RuleTag.SingleFile],
      badExample: `READ TABLE lt_sum WITH TABLE KEY app = lv_app ASSIGNING FIELD-SYMBOL(<sum>).
add( CHANGING cs_sum = <sum> ).`,
      goodExample: `READ TABLE lt_sum WITH TABLE KEY app = lv_app INTO DATA(ls_sum).
add( CHANGING cs_sum = ls_sum ).
MODIFY TABLE lt_sum FROM ls_sum.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public setConfig(conf: ChangingSortedLineConf): void {
    this.conf = conf;
  }
 
  public run(obj: IObject): Issue[] {
    if (!(obj instanceof ABAPObject) || obj.getType() === "INTF") {
      return [];
    }
 
    const issues: Issue[] = [];
    let spaghetti: ISpaghettiScope | undefined = undefined;
 
    for (const file of obj.getABAPFiles()) {
      const stru = file.getStructure();
      if (stru === undefined || stru.findFirstExpression(Expressions.MethodParameters) === undefined) {
        continue;
      }
      if (spaghetti === undefined) {
        spaghetti = new SyntaxLogic(this.reg, obj).run().spaghetti;
      }
 
      const blocks = [...stru.findAllStructures(Structures.Method),
        ...stru.findAllStructures(Structures.Form),
        ...stru.findAllStructures(Structures.FunctionModule)];
      for (const block of blocks) {
        // field symbol name -> name of the sorted or hashed table it points into
        const lines = new Map<string, string>();
        for (const statement of block.findAllStatementNodes()) {
          for (const p of statement.findAllExpressions(Expressions.MethodParameters)) {
            this.checkChanging(p, lines, spaghetti, file, issues);
          }
          this.updateAssigned(statement, lines, spaghetti, file);
        }
      }
    }
 
    return issues;
  }
 
////////////////
 
  private checkChanging(params: ExpressionNode, lines: Map<string, string>, spaghetti: ISpaghettiScope, file: ABAPFile, issues: Issue[]) {
    let changing = false;
    for (const child of params.getChildren()) {
      const str = child.concatTokens().toUpperCase();
      if (child instanceof ExpressionNode && changing === true && child.get() instanceof Expressions.ParameterListT) {
        for (const p of child.findDirectExpressions(Expressions.ParameterT)) {
          const target = p.findDirectExpression(Expressions.Target);
          const fs = target?.getChildren().length === 1 ? target.findDirectExpression(Expressions.TargetFieldSymbol) : undefined;
          if (fs === undefined) {
            continue;
          }
          const table = lines.get(fs.concatTokens().toUpperCase());
          const type = findTypedIdentifier(fs.getFirstToken().getStart(), spaghetti, file)?.getType();
          if (table === undefined || type === undefined || type.isGeneric()) {
            continue;
          }
          const name = p.findDirectExpression(Expressions.ParameterName)?.concatTokens();
          const message = `Line of sorted or hashed table ${table} passed to CHANGING ${name} is write-protected, ` +
            `the call fails at runtime. Use a work area and MODIFY TABLE ... FROM, or pass only non-key fields`;
          issues.push(Issue.atToken(file, fs.getFirstToken(), message, this.getMetadata().key, this.conf.severity));
        }
      }
      changing = str === "CHANGING";
    }
  }
 
  /** Tracks which field symbols the last assignment pointed to a whole line of a sorted or hashed table */
  private updateAssigned(statement: StatementNode, lines: Map<string, string>, spaghetti: ISpaghettiScope, file: ABAPFile) {
    const get = statement.get();
    const fs = get instanceof Statements.Unassign
      ? statement.findDirectExpression(Expressions.TargetFieldSymbol)
      : statement.findFirstExpression(Expressions.FSTarget)?.findFirstExpression(Expressions.TargetFieldSymbol);
    if (fs === undefined) {
      return;
    }
    const name = fs.concatTokens().toUpperCase();
 
    let table: INode | undefined = undefined;
    let isTableExpression = false;
    if (get instanceof Statements.ReadTable) {
      table = statement.findDirectExpression(Expressions.SimpleSource2) ?? statement.findDirectExpression(Expressions.Source);
    } else if (get instanceof Statements.Loop) {
      table = statement.findDirectExpression(Expressions.LoopSource)?.getFirstChild();
    } else if (get instanceof Statements.InsertInternal && statement.findDirectTokenByText("TABLE") !== undefined) {
      table = statement.findDirectExpression(Expressions.Target);
    } else if (get instanceof Statements.Assign) {
      table = statement.findDirectExpression(Expressions.AssignSource)?.getFirstChild();
      isTableExpression = true;
    }
 
    const tableType = table instanceof ExpressionNode ? this.findTableType(table, isTableExpression, spaghetti, file) : undefined;
    if (table instanceof ExpressionNode && tableType instanceof TableType
        && (tableType.getAccessType() === TableAccessType.sorted || tableType.getAccessType() === TableAccessType.hashed)) {
      lines.set(name, table.concatTokens().replace(/\[.*$/, "").trim());
    } else {
      lines.delete(name);
    }
  }
 
  /** The type of a table given by a plain field chain of names, components and attributes, else undefined.
   *  With tableExpression, the chain must end with a table expression, and the type of the chain before it is returned */
  private findTableType(node: ExpressionNode, tableExpression: boolean,
                        spaghetti: ISpaghettiScope, file: ABAPFile): AbstractType | undefined {
    const chain = operandChain(node);
    if (chain === undefined) {
      return undefined;
    }
    const children = [...chain.getChildren()];
    if (tableExpression === true && !(children.pop()?.get() instanceof Expressions.TableExpression)) {
      return undefined;
    }
    const scope = spaghetti.lookupPosition(chain.getFirstToken().getStart(), file.getFilename());
    return chainType(children, scope);
  }
 
}