Skip to content

unsigned integers for sizes of things #62

Closed
@andrewrk

Description

@andrewrk

Arguments in favor of signed integers for array sizes:

  • Fix underflow in core::str::Searcher::new rust-lang/rust#16590 (comment) in short, if the array size is 10 and you subtract 20 you get undefined behavior.
  • Many POSIX functions return ssize_t (such as read) because they want -1 to mean an error. This means that all that space needed by unsigned isn't needed for these. Why stop there? How about the maximum size of things is @max_value(isize) rather than @max_value(usize).
  • POSIX printf returns int. Negative is error, positive is byte count printed. We want a similar thing, but we'd probably use isize.
  • Iterating backwards to 0 for unsigned integers is tricky.

Arguments in favor of unsigned integers for array sizes:

  • Array lengths don't really have a sign. They can't be negative. So unsigned is more correct.
  • 2x the maximum value. On a 32-bit system, byte arrays would be limited to 2GB in length for signed size.
  • Manual assertions/checks needed when an unsigned number is expected. (assert(num_bytes_to_print >= 0);)
  • Passing a signed number to an unsigned type in C (e.g. size_t) requires a cast.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions