|
| 1 | +/** |
| 2 | + * @id c/misra/repeated-initialization-of-aggregate-object-element |
| 3 | + * @name RULE-9-4: An element of an object shall not be initialized more than once |
| 4 | + * @description Repeated initialization of an element in an object can lead to side-effects or may |
| 5 | + * signal a logic error. |
| 6 | + * @kind problem |
| 7 | + * @precision high |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/misra/id/rule-9-4 |
| 10 | + * correctness |
| 11 | + * maintainability |
| 12 | + * readability |
| 13 | + * external/misra/obligation/required |
| 14 | + */ |
| 15 | + |
| 16 | +import cpp |
| 17 | +import codingstandards.c.misra |
| 18 | +import codingstandards.cpp.enhancements.AggregateLiteralEnhancements |
| 19 | + |
| 20 | +/** |
| 21 | + * Gets the `n`th parent of `e`. |
| 22 | + * If `n` is zero, the result is `e`. |
| 23 | + */ |
| 24 | +Expr getNthParent(Expr e, int n) { |
| 25 | + if n = 0 then result = e else result = getNthParent(e.getParent(), n - 1) |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Returns a string representation of the index of `e` relative |
| 30 | + * to the nested array aggregate literal structure it is contained in. |
| 31 | + */ |
| 32 | +string getNestedArrayIndexString(Expr e) { |
| 33 | + result = |
| 34 | + concat(int depth | |
| 35 | + depth = [0 .. getMaxDepth(getRootAggregate(e.getParent()))] |
| 36 | + | |
| 37 | + "[" + |
| 38 | + any(int elementIndex | |
| 39 | + exists(ArrayAggregateLiteral parent | |
| 40 | + parent = getNthParent(e, pragma[only_bind_into](depth + 1)) and |
| 41 | + parent.getElementExpr(elementIndex) = getNthParent(e, pragma[only_bind_into](depth)) |
| 42 | + ) |
| 43 | + | |
| 44 | + elementIndex |
| 45 | + ).toString() + "]" |
| 46 | + order by |
| 47 | + depth desc |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Returns the number of levels of nested `ArrayAggregateLiteral`s in `al`. |
| 53 | + * If there are no nested array aggregate literals, the max depth of the `ArrayAggregateLiteral` is `0`. |
| 54 | + */ |
| 55 | +language[monotonicAggregates] |
| 56 | +int getMaxDepth(ArrayAggregateLiteral al) { |
| 57 | + if not exists(al.getElementExpr(_).(ArrayAggregateLiteral)) |
| 58 | + then result = 0 |
| 59 | + else result = 1 + max(Expr child | child = al.getElementExpr(_) | getMaxDepth(child)) |
| 60 | +} |
| 61 | + |
| 62 | +// internal recursive predicate for `hasMultipleInitializerExprsForSameIndex` |
| 63 | +predicate hasMultipleInitializerExprsForSameIndexInternal( |
| 64 | + ArrayAggregateLiteral al1, ArrayAggregateLiteral al2, Expr out_al1_expr, Expr out_al2_expr |
| 65 | +) { |
| 66 | + exists(int shared_index, Expr al1_expr, Expr al2_expr | |
| 67 | + // an `Expr` initializing an element of the same index in both `al1` and `al2` |
| 68 | + shared_index = [0 .. al1.getArraySize() - 1] and |
| 69 | + al1_expr = al1.getElementExpr(shared_index) and |
| 70 | + al2_expr = al2.getElementExpr(shared_index) and |
| 71 | + // but not the same `Expr` |
| 72 | + not al1_expr = al2_expr and |
| 73 | + ( |
| 74 | + // case A - the children are not aggregate literals |
| 75 | + // holds if `al1` and `al2` both hold for .getElement[sharedIndex] |
| 76 | + not al1_expr instanceof ArrayAggregateLiteral and |
| 77 | + out_al1_expr = al1_expr and |
| 78 | + out_al2_expr = al2_expr |
| 79 | + or |
| 80 | + // case B - `al1` and `al2` both have an aggregate literal child at the same index, so recurse |
| 81 | + hasMultipleInitializerExprsForSameIndexInternal(al1_expr, al2_expr, out_al1_expr, out_al2_expr) |
| 82 | + ) |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Holds if `expr1` and `expr2` both initialize the same array element of `root`. |
| 88 | + */ |
| 89 | +predicate hasMultipleInitializerExprsForSameIndex(ArrayAggregateLiteral root, Expr expr1, Expr expr2) { |
| 90 | + hasMultipleInitializerExprsForSameIndexInternal(root, root, expr1, expr2) |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Holds if `expr1` and `expr2` both initialize the same field of `root`. |
| 95 | + * |
| 96 | + * The dbschema keyset for `aggregate_field_init` prevents referencing multiple `Expr` |
| 97 | + * that initialize the same Field and are part of the same `ClassAggregateLiteral`. |
| 98 | + * This predicate is therefore unable to distinguish the individual duplicate expressions. |
| 99 | + */ |
| 100 | +predicate hasMultipleInitializerExprsForSameField(ClassAggregateLiteral root, Field f) { |
| 101 | + count(root.getFieldExpr(f)) > 1 |
| 102 | +} |
| 103 | + |
| 104 | +from |
| 105 | + AggregateLiteral root, Expr e1, Expr e2, string elementDescription, string rootType, |
| 106 | + string clarification |
| 107 | +where |
| 108 | + not isExcluded(e1, Memory1Package::repeatedInitializationOfAggregateObjectElementQuery()) and |
| 109 | + exists(Initializer init | init.getExpr() = root) and |
| 110 | + ( |
| 111 | + hasMultipleInitializerExprsForSameIndex(root, e1, e2) and |
| 112 | + elementDescription = getNestedArrayIndexString(e1) and |
| 113 | + rootType = "Array aggregate literal" and |
| 114 | + clarification = ", which is already initialized $@." |
| 115 | + or |
| 116 | + exists(Field f | |
| 117 | + // we cannot distinguish between different aggregate field init expressions. |
| 118 | + // therefore, we only report the root aggregate rather than any child init expr. |
| 119 | + // see `hasMultipleInitializerExprsForSameField` documentation. |
| 120 | + hasMultipleInitializerExprsForSameField(root, f) and |
| 121 | + e1 = root and |
| 122 | + e2 = root and |
| 123 | + elementDescription = f.getQualifiedName() and |
| 124 | + rootType = "Structure aggregate literal" and |
| 125 | + clarification = "." |
| 126 | + ) |
| 127 | + ) and |
| 128 | + // de-duplicate the results by excluding permutations of `e1` and `e2` |
| 129 | + e1.getLocation().toString() <= e2.getLocation().toString() |
| 130 | +select e1, "$@ repeats initialization of element " + elementDescription + clarification, root, |
| 131 | + rootType, e2, "here" |
0 commit comments