Skip to content

Commit 3a9df7a

Browse files
oslfmtsmoelius
authored andcommitted
change recommended to recommended-2
1 parent 8fb7a49 commit 3a9df7a

File tree

10 files changed

+84
-27
lines changed

10 files changed

+84
-27
lines changed

crate/diffs/type_cosplay.diff

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,17 @@ Only in ../../../../lints/type_cosplay/ui/insecure/src: lib.stderr
1717
Only in ../../../../lints/type_cosplay/ui: insecure-2
1818
Only in ../../../../lints/type_cosplay/ui: insecure-3
1919
Only in ../../../../lints/type_cosplay/ui: insecure-anchor
20-
diff -r ./recommended/Cargo.toml ../../../../lints/type_cosplay/ui/recommended/Cargo.toml
21-
19c19
22-
< anchor-lang = "0.20.1"
23-
---
24-
> anchor-lang = "0.25.0"
2520
diff -r ./recommended/src/lib.rs ../../../../lints/type_cosplay/ui/recommended/src/lib.rs
26-
10c10,17
21+
10c10,12
2722
< pub fn update_user(ctx: Context<UpdateUser>) -> ProgramResult {
2823
---
2924
> pub fn update_user(
3025
> ctx: Context<UpdateUser>,
3126
> ) -> anchor_lang::solana_program::entrypoint::ProgramResult {
32-
> let account_info: &AccountInfo = ctx.accounts.user.as_ref();
33-
> let mut data = &*account_info.data.take();
34-
> let user = User::try_deserialize(&mut data).unwrap();
35-
>
36-
> msg!("User: {:?}", user.authority);
37-
31a39,40
27+
31a34,35
3828
>
3929
> fn main() {}
40-
Only in ../../../../lints/type_cosplay/ui/recommended/src: lib.stderr
30+
Only in ../../../../lints/type_cosplay/ui: recommended-2
4131
diff -r ./secure/src/lib.rs ../../../../lints/type_cosplay/ui/secure/src/lib.rs
4232
10c10,12
4333
< pub fn update_user(ctx: Context<UpdateUser>) -> ProgramResult {

lints/type_cosplay/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ path = "ui/insecure-anchor/src/lib.rs"
2929
name = "recommended"
3030
path = "ui/recommended/src/lib.rs"
3131

32+
[[example]]
33+
name = "recommended-2"
34+
path = "ui/recommended-2/src/lib.rs"
35+
3236
[[example]]
3337
name = "secure"
3438
path = "ui/secure/src/lib.rs"

lints/type_cosplay/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ deserialize without any problem into a `User` struct, leading to a type-cosplay
8181

8282
### recommended
8383

84+
The recommended way to address the type-cosplay issue. It adds an `#[account]` macro to each
85+
struct, which adds a discriminant to each struct. It doesn't actually perform any deserializations,
86+
which is why the `recommended-2` was created.
87+
88+
### recommended-2
89+
8490
This is secure code because all structs have an `#[account]` macro attributed
8591
on them, thus deriving the `Discriminator` trait for each. Further, unlike the insecure-anchor
8692
example, the program uses the proper deserialization method, `try_deserialize`, to deserialize

lints/type_cosplay/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ extern crate rustc_middle;
99
extern crate rustc_span;
1010

1111
use clippy_utils::{
12-
diagnostics::span_lint_and_help, get_trait_def_id, match_def_path, ty::{match_type, implements_trait},
12+
diagnostics::span_lint_and_help,
13+
get_trait_def_id, match_def_path,
14+
ty::{implements_trait, match_type},
1315
};
1416
use rustc_data_structures::fx::FxHashMap;
1517
use rustc_hir::{def::Res, Expr, ExprKind, QPath, TyKind};
@@ -64,18 +66,12 @@ struct TypeCosplay {
6466
deser_types: FxHashMap<AdtKind, Vec<(DefId, Span)>>,
6567
}
6668

67-
// get type X
68-
// check if implements Discriminator
69-
// check corresponding function call type:
70-
// if !try_deserialize
71-
// emit lint with warning to use try_deserialize (because any types deriving Discriminator should since it guaranteed to check discrim)
72-
7369
impl<'tcx> LateLintPass<'tcx> for TypeCosplay {
7470
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
7571
if_chain! {
7672
if !expr.span.from_expansion();
7773
if let ExprKind::Call(fnc_expr, args_exprs) = expr.kind;
78-
// TODO: recommended case will exit early since it contains a reference to AccountInfo.data,
74+
// TODO: recommended-2 case will exit early since it contains a reference to AccountInfo.data,
7975
// not a direct argument. In general, any references will fail
8076
if args_exprs.iter().any(|arg| {
8177
visit_expr_no_bodies(arg, |expr| contains_data_field_reference(cx, expr))
@@ -259,3 +255,8 @@ fn secure_two() {
259255
fn recommended() {
260256
dylint_testing::ui_test_example(env!("CARGO_PKG_NAME"), "recommended");
261257
}
258+
259+
#[test]
260+
fn recommended_2() {
261+
dylint_testing::ui_test_example(env!("CARGO_PKG_NAME"), "recommended-2");
262+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "type-cosplay-recommended"
3+
version = "0.1.0"
4+
description = "Created with Anchor"
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "type_cosplay_recommended"
10+
11+
[features]
12+
no-entrypoint = []
13+
no-idl = []
14+
no-log-ix-name = []
15+
cpi = ["no-entrypoint"]
16+
default = []
17+
18+
[dependencies]
19+
anchor-lang = "0.25.0"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.bpfel-unknown-unknown.dependencies.std]
2+
features = []
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use anchor_lang::prelude::*;
2+
use borsh::{BorshDeserialize, BorshSerialize};
3+
4+
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
5+
6+
#[program]
7+
pub mod type_cosplay_recommended {
8+
use super::*;
9+
10+
pub fn update_user(
11+
ctx: Context<UpdateUser>,
12+
) -> anchor_lang::solana_program::entrypoint::ProgramResult {
13+
let account_info: &AccountInfo = ctx.accounts.user.as_ref();
14+
let mut data = &*account_info.data.take();
15+
let user = User::try_deserialize(&mut data).unwrap();
16+
17+
msg!("User: {:?}", user.authority);
18+
msg!("GM {}", ctx.accounts.user.authority);
19+
Ok(())
20+
}
21+
}
22+
23+
#[derive(Accounts)]
24+
pub struct UpdateUser<'info> {
25+
#[account(has_one = authority)]
26+
user: Account<'info, User>,
27+
authority: Signer<'info>,
28+
}
29+
30+
#[account]
31+
pub struct User {
32+
authority: Pubkey,
33+
}
34+
35+
#[account]
36+
pub struct Metadata {
37+
account: Pubkey,
38+
}
39+
40+
fn main() {}

lints/type_cosplay/ui/recommended-2/src/lib.stderr

Whitespace-only changes.

lints/type_cosplay/ui/recommended/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ cpi = ["no-entrypoint"]
1616
default = []
1717

1818
[dependencies]
19-
anchor-lang = "0.25.0"
19+
anchor-lang = "0.20.1"

lints/type_cosplay/ui/recommended/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ pub mod type_cosplay_recommended {
1010
pub fn update_user(
1111
ctx: Context<UpdateUser>,
1212
) -> anchor_lang::solana_program::entrypoint::ProgramResult {
13-
let account_info: &AccountInfo = ctx.accounts.user.as_ref();
14-
let mut data = &*account_info.data.take();
15-
let user = User::try_deserialize(&mut data).unwrap();
16-
17-
msg!("User: {:?}", user.authority);
1813
msg!("GM {}", ctx.accounts.user.authority);
1914
Ok(())
2015
}

0 commit comments

Comments
 (0)