This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Fix ssize test that made an assumption about ptrdiff_t being 'long'
ClosedPublic

Authored by ldionne on Nov 24 2021, 1:43 PM.

Details

Summary

On some platforms like armv7m, the size() method of containers returns
unsigned long, while ptrdiff_t is just int. Hence, std::ssize_t ends up
being long, which is not the same as ptrdiff_t. This is usually not an
issue because std::ptrdiff_t is long, so everything works out, but it
breaks on some more exotic architectures.

Diff Detail

Event Timeline

ldionne created this revision.Nov 24 2021, 1:43 PM
ldionne requested review of this revision.Nov 24 2021, 1:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 24 2021, 1:43 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript

returns a type that is larger than ptrdiff_t,

where ptrdiff_t is int, but size_t is still unsigned long. As a result, std::ssize returns long

Unless this is a _really_ special platform, isn't long just 32 bits in this setup? I.e. sizeof(ptrdiff_t) == sizeof(ssize_t), (and sizeof(int) == sizeof(long)), but the root cause is that int and long are two distinct different types even if their sizes happen to match?

returns a type that is larger than ptrdiff_t,

where ptrdiff_t is int, but size_t is still unsigned long. As a result, std::ssize returns long

Unless this is a _really_ special platform, isn't long just 32 bits in this setup? I.e. sizeof(ptrdiff_t) == sizeof(ssize_t), (and sizeof(int) == sizeof(long)), but the root cause is that int and long are two distinct different types even if their sizes happen to match?

You are right! I'm editing my commit message.

ldionne updated this revision to Diff 389611.Nov 24 2021, 2:03 PM
ldionne retitled this revision from [libc++] Fix ssize test that made an assumption about ptrdiff_t to [libc++] Fix ssize test that made an assumption about ptrdiff_t being 'long'.
ldionne edited the summary of this revision. (Show Details)

Amend commit message.

Mordante accepted this revision as: Mordante.Nov 25 2021, 10:08 AM
Mordante added a subscriber: Mordante.

LGTM!

ldionne accepted this revision as: Restricted Project.Nov 25 2021, 10:12 AM
This revision is now accepted and ready to land.Nov 25 2021, 10:12 AM