Skip to content

Commit

Permalink
Rust: Include index in Format.getArgument
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Jan 8, 2025
1 parent bca5f4b commit 033cd17
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 42 deletions.
8 changes: 4 additions & 4 deletions rust/ql/.generated.list

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ final class FormatArgsExprCfgNode extends Nodes::FormatArgsExprCfgNode {
/** Gets a format argument of the `i`th format of this format arguments expression (0-based). */
FormatTemplateVariableAccessCfgNode getFormatTemplateVariableAccess(int i) {
exists(FormatTemplateVariableAccess v |
v.getArgument() = node.getFormat(i).getArgument() and
v.getArgument() = node.getFormat(i).getArgument(_) and
result.getFormatTemplateVariableAccess() = v and
any(ChildMapping mapping).hasCfgChild(node, v, this, result)
)
Expand Down
10 changes: 10 additions & 0 deletions rust/ql/lib/codeql/rust/elements/internal/FormatArgumentImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ module Impl {

override Format getParent() { result = Synth::TFormat(parent, index, _, _) }

/** Gets the position of this argument. */
int getPosition() {
this =
rank[result + 1](FormatArgument f, int offs |
f = Synth::TFormatArgument(parent, index, _, _, _, offs)
|
f order by offs
)
}

override FormatTemplateVariableAccess getVariable() { result.getArgument() = this }
}

Expand Down
4 changes: 3 additions & 1 deletion rust/ql/lib/codeql/rust/elements/internal/FormatImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ module Impl {

override int getIndex() { result = index }

override FormatArgument getArgument() { result.getParent() = this }
override FormatArgument getArgument(int i) {
result.getParent() = this and i = result.getPosition()
}

/**
* Gets the name or position reference of this format, if any. For example `name` and `0` in:
Expand Down
13 changes: 9 additions & 4 deletions rust/ql/lib/codeql/rust/elements/internal/generated/Format.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
| gen_format.rs:5:21:5:22 | {} | getParent: | gen_format.rs:5:14:5:32 | FormatArgsExpr | getIndex: | 1 | hasArgument: | no |
| gen_format.rs:7:21:7:46 | {value:#width$.precision$} | getParent: | gen_format.rs:7:14:7:47 | FormatArgsExpr | getIndex: | 1 | hasArgument: | yes |
| gen_format_args_arg.rs:5:26:5:27 | {} | getParent: | gen_format_args_arg.rs:5:17:5:39 | FormatArgsExpr | getIndex: | 1 | hasArgument: | no |
| gen_format_args_expr.rs:6:19:6:20 | {} | getParent: | gen_format_args_expr.rs:6:17:6:37 | FormatArgsExpr | getIndex: | 1 | hasArgument: | no |
| gen_format_args_expr.rs:6:26:6:29 | {:?} | getParent: | gen_format_args_expr.rs:6:17:6:37 | FormatArgsExpr | getIndex: | 3 | hasArgument: | no |
| gen_format_args_expr.rs:7:19:7:21 | {b} | getParent: | gen_format_args_expr.rs:7:17:7:43 | FormatArgsExpr | getIndex: | 1 | hasArgument: | yes |
| gen_format_args_expr.rs:7:27:7:31 | {a:?} | getParent: | gen_format_args_expr.rs:7:17:7:43 | FormatArgsExpr | getIndex: | 3 | hasArgument: | yes |
| gen_format_args_expr.rs:9:19:9:21 | {x} | getParent: | gen_format_args_expr.rs:9:17:9:28 | FormatArgsExpr | getIndex: | 1 | hasArgument: | yes |
| gen_format_args_expr.rs:9:24:9:26 | {y} | getParent: | gen_format_args_expr.rs:9:17:9:28 | FormatArgsExpr | getIndex: | 3 | hasArgument: | yes |
| gen_format_argument.rs:5:21:5:46 | {value:#width$.precision$} | getParent: | gen_format_argument.rs:5:14:5:47 | FormatArgsExpr | getIndex: | 1 | hasArgument: | yes |
| gen_format_argument.rs:7:21:7:30 | {0:#1$.2$} | getParent: | gen_format_argument.rs:7:14:7:56 | FormatArgsExpr | getIndex: | 1 | hasArgument: | yes |
| gen_format.rs:5:21:5:22 | {} | getParent: | gen_format.rs:5:14:5:32 | FormatArgsExpr | getIndex: | 1 | getNumberOfArguments: | 0 |
| gen_format.rs:7:21:7:46 | {value:#width$.precision$} | getParent: | gen_format.rs:7:14:7:47 | FormatArgsExpr | getIndex: | 1 | getNumberOfArguments: | 3 |
| gen_format_args_arg.rs:5:26:5:27 | {} | getParent: | gen_format_args_arg.rs:5:17:5:39 | FormatArgsExpr | getIndex: | 1 | getNumberOfArguments: | 0 |
| gen_format_args_expr.rs:6:19:6:20 | {} | getParent: | gen_format_args_expr.rs:6:17:6:37 | FormatArgsExpr | getIndex: | 1 | getNumberOfArguments: | 0 |
| gen_format_args_expr.rs:6:26:6:29 | {:?} | getParent: | gen_format_args_expr.rs:6:17:6:37 | FormatArgsExpr | getIndex: | 3 | getNumberOfArguments: | 0 |
| gen_format_args_expr.rs:7:19:7:21 | {b} | getParent: | gen_format_args_expr.rs:7:17:7:43 | FormatArgsExpr | getIndex: | 1 | getNumberOfArguments: | 1 |
| gen_format_args_expr.rs:7:27:7:31 | {a:?} | getParent: | gen_format_args_expr.rs:7:17:7:43 | FormatArgsExpr | getIndex: | 3 | getNumberOfArguments: | 1 |
| gen_format_args_expr.rs:9:19:9:21 | {x} | getParent: | gen_format_args_expr.rs:9:17:9:28 | FormatArgsExpr | getIndex: | 1 | getNumberOfArguments: | 1 |
| gen_format_args_expr.rs:9:24:9:26 | {y} | getParent: | gen_format_args_expr.rs:9:17:9:28 | FormatArgsExpr | getIndex: | 3 | getNumberOfArguments: | 1 |
| gen_format_argument.rs:5:21:5:46 | {value:#width$.precision$} | getParent: | gen_format_argument.rs:5:14:5:47 | FormatArgsExpr | getIndex: | 1 | getNumberOfArguments: | 3 |
| gen_format_argument.rs:7:21:7:30 | {0:#1$.2$} | getParent: | gen_format_argument.rs:7:14:7:56 | FormatArgsExpr | getIndex: | 1 | getNumberOfArguments: | 3 |

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
| gen_format.rs:7:21:7:46 | {value:#width$.precision$} | gen_format.rs:7:22:7:26 | value |
| gen_format.rs:7:21:7:46 | {value:#width$.precision$} | gen_format.rs:7:29:7:33 | width |
| gen_format.rs:7:21:7:46 | {value:#width$.precision$} | gen_format.rs:7:36:7:44 | precision |
| gen_format_args_expr.rs:7:19:7:21 | {b} | gen_format_args_expr.rs:7:20:7:20 | b |
| gen_format_args_expr.rs:7:27:7:31 | {a:?} | gen_format_args_expr.rs:7:28:7:28 | a |
| gen_format_args_expr.rs:9:19:9:21 | {x} | gen_format_args_expr.rs:9:20:9:20 | x |
| gen_format_args_expr.rs:9:24:9:26 | {y} | gen_format_args_expr.rs:9:25:9:25 | y |
| gen_format_argument.rs:5:21:5:46 | {value:#width$.precision$} | gen_format_argument.rs:5:22:5:26 | value |
| gen_format_argument.rs:5:21:5:46 | {value:#width$.precision$} | gen_format_argument.rs:5:29:5:33 | width |
| gen_format_argument.rs:5:21:5:46 | {value:#width$.precision$} | gen_format_argument.rs:5:36:5:44 | precision |
| gen_format_argument.rs:7:21:7:30 | {0:#1$.2$} | gen_format_argument.rs:7:22:7:22 | 0 |
| gen_format_argument.rs:7:21:7:30 | {0:#1$.2$} | gen_format_argument.rs:7:25:7:25 | 1 |
| gen_format_argument.rs:7:21:7:30 | {0:#1$.2$} | gen_format_argument.rs:7:28:7:28 | 2 |
| gen_format.rs:7:21:7:46 | {value:#width$.precision$} | 0 | gen_format.rs:7:22:7:26 | value |
| gen_format.rs:7:21:7:46 | {value:#width$.precision$} | 1 | gen_format.rs:7:29:7:33 | width |
| gen_format.rs:7:21:7:46 | {value:#width$.precision$} | 2 | gen_format.rs:7:36:7:44 | precision |
| gen_format_args_expr.rs:7:19:7:21 | {b} | 0 | gen_format_args_expr.rs:7:20:7:20 | b |
| gen_format_args_expr.rs:7:27:7:31 | {a:?} | 0 | gen_format_args_expr.rs:7:28:7:28 | a |
| gen_format_args_expr.rs:9:19:9:21 | {x} | 0 | gen_format_args_expr.rs:9:20:9:20 | x |
| gen_format_args_expr.rs:9:24:9:26 | {y} | 0 | gen_format_args_expr.rs:9:25:9:25 | y |
| gen_format_argument.rs:5:21:5:46 | {value:#width$.precision$} | 0 | gen_format_argument.rs:5:22:5:26 | value |
| gen_format_argument.rs:5:21:5:46 | {value:#width$.precision$} | 1 | gen_format_argument.rs:5:29:5:33 | width |
| gen_format_argument.rs:5:21:5:46 | {value:#width$.precision$} | 2 | gen_format_argument.rs:5:36:5:44 | precision |
| gen_format_argument.rs:7:21:7:30 | {0:#1$.2$} | 0 | gen_format_argument.rs:7:22:7:22 | 0 |
| gen_format_argument.rs:7:21:7:30 | {0:#1$.2$} | 1 | gen_format_argument.rs:7:25:7:25 | 1 |
| gen_format_argument.rs:7:21:7:30 | {0:#1$.2$} | 2 | gen_format_argument.rs:7:28:7:28 | 2 |

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/schema/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,7 @@ class Format(Locatable):
"""
parent: FormatArgsExpr
index: int
argument: optional["FormatArgument"] | child
argument: list["FormatArgument"] | child


@synth.on_arguments(parent=FormatArgsExpr, index=int, kind=int, name=string, positional=boolean, offset=int)
Expand Down

0 comments on commit 033cd17

Please sign in to comment.