# Guard functions should not be allowed in init, pre\_upgrade, and post\_upgrade

**URL:** <https://forum.dfinity.org/t/guard-functions-should-not-be-allowed-in-init-pre-upgrade-and-post-upgrade/21731>\
**Category:** Developers\
**Created:** [July 26, 2023, 5:52pm UTC](https://forum.dfinity.org/t/guard-functions-should-not-be-allowed-in-init-pre-upgrade-and-post-upgrade/21731 "2023-07-26T17:52:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [July 26, 2023, 5:52pm UTC](https://forum.dfinity.org/t/guard-functions-should-not-be-allowed-in-init-pre-upgrade-and-post-upgrade/21731/1 "2023-07-26T17:52:18Z")

</div>

I think we’ve discovered a bug in the Rust CDK. Currently ic\_cdk\_macros allows guard functions on `init`, `pre_upgrade`, and `post_upgrade`. But the expanded code for the guard function macro results in a call to `ic0.msg_reject`, which is not allowed from `canister_init`, `canister_post_upgrade`, or `canister_pre_upgrade` according to the current interface spec. We also see this error in our CI tests for guard functions in Azle.

Here’s the expanded code for a guard function:

```rust
#[export_name = "canister_pre_upgrade"]
fn pre_upgrade_4_() {
    ic_cdk::setup();
    let r: Result<(), String> = _cdk_user_defined_preventUpgrades();
    if let Err(e) = r {
        ic_cdk::api::call::reject(&e);
        return;
    }
    ic_cdk::spawn(async {
        let result = pre_upgrade();
    });
}

```

And here’s where the interface spec describes where `ic0.msg_reject` can be called from:

ic0.msg\_reject : (src : i32, size : i32) → (); // U Q CQ Ry Rt CRy CRt

Seems like the Rust CDK is incorrect in allowing guard functions for `init`, `pre_upgrade` and `post_upgrade`.

---

<div class="post-metadata">

**Author:** ![lwshang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lwshang/32/3394_2.png) [@lwshang](https://forum.dfinity.org/u/lwshang)\
**Post date:** [July 27, 2023, 7:05pm UTC](https://forum.dfinity.org/t/guard-functions-should-not-be-allowed-in-init-pre-upgrade-and-post-upgrade/21731/2 "2023-07-27T19:05:47Z")

</div>

Thanks for reporting the issue.

`ic-cdk-macros` [v0.7.1](https://github.com/dfinity/cdk-rs/blob/main/src/ic-cdk-macros/CHANGELOG.md#071---2023-07-27) was published to fix it.

Now, only `#[update]` and `#[query]` can take guard function.
