Skip to content

Commit 526cc5a

Browse files
committed
new restriction lint: pointer_format
1 parent b363499 commit 526cc5a

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6162,6 +6162,7 @@ Released 2018-09-13
61626162
[`pathbuf_init_then_push`]: https://rust-lang.github.io/rust-clippy/master/index.html#pathbuf_init_then_push
61636163
[`pattern_type_mismatch`]: https://rust-lang.github.io/rust-clippy/master/index.html#pattern_type_mismatch
61646164
[`permissions_set_readonly_false`]: https://rust-lang.github.io/rust-clippy/master/index.html#permissions_set_readonly_false
6165+
[`pointer_format`]: https://rust-lang.github.io/rust-clippy/master/index.html#pointer_format
61656166
[`pointers_in_nomem_asm_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#pointers_in_nomem_asm_block
61666167
[`positional_named_format_parameters`]: https://rust-lang.github.io/rust-clippy/master/index.html#positional_named_format_parameters
61676168
[`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
165165
crate::floating_point_arithmetic::SUBOPTIMAL_FLOPS_INFO,
166166
crate::format::USELESS_FORMAT_INFO,
167167
crate::format_args::FORMAT_IN_FORMAT_ARGS_INFO,
168+
crate::format_args::POINTER_FORMAT_INFO,
168169
crate::format_args::TO_STRING_IN_FORMAT_ARGS_INFO,
169170
crate::format_args::UNINLINED_FORMAT_ARGS_INFO,
170171
crate::format_args::UNNECESSARY_DEBUG_FORMATTING_INFO,

clippy_lints/src/format_args.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use arrayvec::ArrayVec;
22
use clippy_config::Conf;
3-
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
3+
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
44
use clippy_utils::macros::{
55
FormatArgsStorage, FormatParamUsage, MacroCall, find_format_arg_expr, format_arg_removal_span,
66
format_placeholder_format_span, is_assert_macro, is_format_macro, is_panic, matching_root_macro_call,
@@ -194,12 +194,33 @@ declare_clippy_lint! {
194194
"use of a format specifier that has no effect"
195195
}
196196

197+
declare_clippy_lint! {
198+
/// ### What it does
199+
/// Detects [pointer format].
200+
///
201+
/// ### Why restrict this?
202+
/// In kernel context, this might be vulnerable to misuse for exfiltrating
203+
/// stack or kernel function addresses.
204+
///
205+
/// ### Example
206+
/// ```no_run
207+
/// println!("{:p}", foo);
208+
/// ```
209+
///
210+
/// [pointer format]: https://doc.rust-lang.org/std/fmt/index.html#formatting-traits
211+
#[clippy::version = "1.88.0"]
212+
pub POINTER_FORMAT,
213+
restriction,
214+
"use of a pointer format specifier"
215+
}
216+
197217
impl_lint_pass!(FormatArgs<'_> => [
198218
FORMAT_IN_FORMAT_ARGS,
199219
TO_STRING_IN_FORMAT_ARGS,
200220
UNINLINED_FORMAT_ARGS,
201221
UNNECESSARY_DEBUG_FORMATTING,
202222
UNUSED_FORMAT_SPECS,
223+
POINTER_FORMAT,
203224
]);
204225

205226
#[allow(clippy::struct_field_names)]
@@ -280,6 +301,12 @@ impl<'tcx> FormatArgsExpr<'_, 'tcx> {
280301
let name = self.cx.tcx.item_name(self.macro_call.def_id);
281302
self.check_unnecessary_debug_formatting(name, arg_expr);
282303
}
304+
305+
if placeholder.format_trait == FormatTrait::Pointer
306+
&& let Some(span) = placeholder.span
307+
{
308+
span_lint(self.cx, POINTER_FORMAT, span, "pointer formatting detected");
309+
}
283310
}
284311
}
285312
}

tests/ui/pointer_format.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![warn(clippy::pointer_format)]
2+
3+
fn main() {
4+
let m = &(main as fn());
5+
let g = &0;
6+
let o = &format!("{m:p}");
7+
//~^ pointer_format
8+
println!("{g:p}");
9+
//~^ pointer_format
10+
panic!("{o:p}");
11+
//~^ pointer_format
12+
}

tests/ui/pointer_format.stderr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: pointer formatting detected
2+
--> tests/ui/pointer_format.rs:6:23
3+
|
4+
LL | let o = &format!("{m:p}");
5+
| ^^^^^
6+
|
7+
= note: `-D clippy::pointer-format` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::pointer_format)]`
9+
10+
error: pointer formatting detected
11+
--> tests/ui/pointer_format.rs:8:15
12+
|
13+
LL | println!("{g:p}");
14+
| ^^^^^
15+
16+
error: pointer formatting detected
17+
--> tests/ui/pointer_format.rs:10:13
18+
|
19+
LL | panic!("{o:p}");
20+
| ^^^^^
21+
22+
error: aborting due to 3 previous errors
23+

0 commit comments

Comments
 (0)