Skip to content

Commit 2057c1e

Browse files
authored
fix(SECFIND-513): prevent NEXT_NOTE_ID to wrap around (#183)
Prevent NEXT_NOTE_ID to wrap around by using a checked addition instead of an unchecked one. If the checked addition fails, then creating a new note simply fails, making the approach safe so that no data is lost. The overflow issue does not affect [the respective Motoko code](https://github.com/dfinity/vetkeys/blob/main/examples/encrypted_notes_dapp_vetkd/motoko/backend/main.mo#L135), because `nextNoteId` is of type `Nat` which has [infinite precision](https://internetcomputer.org/docs/motoko/base/Nat).
1 parent 9cad577 commit 2057c1e

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

  • examples/encrypted_notes_dapp_vetkd/rust/backend/src

examples/encrypted_notes_dapp_vetkd/rust/backend/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,12 @@ fn create_note() -> NoteId {
276276
assert_eq!(id_to_note.insert(new_note.id, new_note), None);
277277

278278
NEXT_NOTE_ID.with_borrow_mut(|next_note_id| {
279+
let id_plus_one = next_note_id
280+
.get()
281+
.checked_add(1)
282+
.expect("failed to increase NEXT_NOTE_ID: reached the maximum");
279283
next_note_id
280-
.set(next_note_id.get() + 1)
284+
.set(id_plus_one)
281285
.unwrap_or_else(|_e| ic_cdk::trap("failed to set NEXT_NOTE_ID"))
282286
});
283287
next_note_id

0 commit comments

Comments
 (0)