Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

using random pointers, but only guarded by an if comparison #27

Open
@nikomatsakis

Description

@nikomatsakis

In #26, @arielb1 gave this example code. It takes a random *mut and dereferences it -- but only after double checking that it is a valid value:

fn example(foo_addr: *mut usize) -> usize {
    let mut data = 0;
    if pointers_equal(&mut data as *mut _, foo_addr) {
        unsafe { *foo_addr = 42; }
    }
    data
}

The challenge here is that we are using a *mut -- but only after (dynamically) comparing it for correctness. This seems to get at a key question: the extent to which users are permitted to think of the actions of the code as a kind of "turing machine".

Many legit (or potentially legit) uses of uninitialized memory have this general feeling.

Random but maybe unrelated example: I have at times used sets that require only O(1) initialization. For example, a set consisting of the integers 0..N might work like:

  • index: [usize; N] // uninitialized
  • members: [usize; N] // uninitialized
  • count: usize

To add a new item i, you do:

  • self.index[i] = self.count; self.members[self.count] = i; self.count += 1

To check if item i is present you do:

  • let m = self.index[i]; m < self.count && self.members[m] == i

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions