Skip to content

fix some unsafe behavior in std.format#4427

Merged
schveiguy merged 1 commit intodlang:masterfrom
WalterBright:formatPtrDiff
Jun 14, 2016
Merged

fix some unsafe behavior in std.format#4427
schveiguy merged 1 commit intodlang:masterfrom
WalterBright:formatPtrDiff

Conversation

@WalterBright
Copy link
Member

No description provided.

std/format.d Outdated
* the difference between the starts of the arrays
*/
@trusted private pure nothrow @nogc
size_t arrayPtrDiff(const void[] array1, const void[] array2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this return ptrdiff_t or at least assert that array1.ptr >= array2.ptr?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed

@burner
Copy link
Member

burner commented Jun 14, 2016

What is made safe by this and where is the @safe unittest that shows that.

@PetarKirov
Copy link
Member

PetarKirov commented Jun 14, 2016

@burner Walter is fixing various safety related bugs. In this case, the next version of dmd will disallow accessing .ptr in @safe code (which is currently allowed, IIRC). In order for the DMD PR to pass on the autotester, accessing .ptr should first be encapsulated throughout druntime and phobos.

See also dlang/dmd#5860

@burner
Copy link
Member

burner commented Jun 14, 2016

@ZombineDev thanks. @WalterBright I would have liked this link to the dmd PR in the original PR description. Figuring out what the purpose of this PR is would have been easier this way.

@burner
Copy link
Member

burner commented Jun 14, 2016

LGTM

enforceFmt(tmp.length,
text("Incorrect format specifier %", trailing[i .. $]));
i = tmp.ptr - trailing.ptr;
i = arrayPtrDiff(tmp, trailing);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we could use a more general ptrValue safe function:
i = tmp.ptrValue - trailing.ptrValue;
See dlang/druntime#1590 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ntrel see new PR I added to try and make a nice wrapper for this: dlang/druntime#1592

@schveiguy
Copy link
Member

Auto-merge toggled on

@CyberShadow
Copy link
Member

This pull request introduced a regression:
https://issues.dlang.org/show_bug.cgi?id=16676

MartinNowak added a commit to MartinNowak/phobos that referenced this pull request Nov 16, 2016
- fix incorrect pointer diff computation introduced by PR dlang#4427
  commit 2be0355
John-Colvin pushed a commit to John-Colvin/phobos that referenced this pull request Dec 8, 2016
- fix incorrect pointer diff computation introduced by PR dlang#4427
  commit 2be0355
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.

6 participants