Skip to content

Fix NonNull::sub and NonNull::copy_* postconditions for zero-byte cases - #810

Open
fromtaoyuanhsinchuuuu wants to merge 1 commit into
model-checking:mainfrom
fromtaoyuanhsinchuuuu:fix/nonnull-sub-copy-postconditions
Open

fromtaoyuanhsinchuuuu wants to merge 1 commit into
model-checking:mainfrom
fromtaoyuanhsinchuuuu:fix/nonnull-sub-copy-postconditions

Conversation

@fromtaoyuanhsinchuuuu

Copy link
Copy Markdown

Fixes two specification gaps in core::ptr::NonNull where #[ensures] postconditions failed on inputs admitted by their #[requires] preconditions and handled correctly by the method bodies:

  • NonNull::sub: when T is zero-sized, #[requires] permits any count: usize, but #[ensures] evaluated self.as_ptr().offset(-(count as isize)), overflowing when count == isize::MIN as usize. Changed the postcondition to use self.as_ptr().wrapping_sub(count) (matching NonNull::offset's wrapping_offset postcondition).
  • NonNull::{copy_to, copy_to_nonoverlapping, copy_from, copy_from_nonoverlapping}: #[ensures] unconditionally asserted ub_checks::can_dereference(... as *const u8) on both pointers even when count == 0 (e.g., one-past-the-end pointers) or size_of::<T>() == 0 (ZSTs). Guarded the 1-byte dereferenceability assertion with count == 0 || core::mem::size_of::<T>() == 0.
  • Added regression harnesses in ptr::non_null::verify covering ZST NonNull::sub (including isize::MIN as usize) as well as zero-count (one-past-the-end) and ZST calls for all four NonNull::copy_* methods, verified with Kani (106 harnesses passing, 0 failures).

Resolves #801

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@fromtaoyuanhsinchuuuu
fromtaoyuanhsinchuuuu requested a review from a team as a code owner December 2, 2026 18:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NonNull::sub and NonNull::copy_*: postconditions fail for inputs the preconditions admit (zero-sized sub overflow, 0-byte copies)

1 participant