This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 411
use array interface to hashOf() #1595
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,22 +17,22 @@ version( AnyX86 ) | |
| version = HasUnalignedOps; | ||
|
|
||
|
|
||
| @trusted pure nothrow | ||
| @trusted pure nothrow @nogc | ||
| size_t hashOf( const(void)[] buf, size_t seed = 0 ) | ||
| { | ||
| return hashOf(buf.ptr, buf.length, seed); | ||
| } | ||
|
|
||
| @trusted pure nothrow | ||
| size_t hashOf( const(void)* buf, size_t len, size_t seed = 0 ) | ||
| @system pure nothrow @nogc | ||
| size_t hashOf( const(void)* buf, size_t len, size_t seed ) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. get rid of the default argument, it just causes bugs |
||
| { | ||
| /* | ||
| * This is Paul Hsieh's SuperFastHash algorithm, described here: | ||
| * http://www.azillionmonkeys.com/qed/hash.html | ||
| * It is protected by the following open source license: | ||
| * http://www.azillionmonkeys.com/qed/weblicense.html | ||
| */ | ||
| static uint get16bits( const (ubyte)* x ) pure nothrow | ||
| static uint get16bits( const (ubyte)* x ) pure nothrow @nogc | ||
| { | ||
| // CTFE doesn't support casting ubyte* -> ushort*, so revert to | ||
| // per-byte access when in CTFE. | ||
|
|
@@ -96,14 +96,14 @@ size_t hashOf( const(void)* buf, size_t len, size_t seed = 0 ) | |
| } | ||
|
|
||
| // Check that hashOf works with CTFE | ||
| unittest | ||
| @nogc nothrow pure unittest | ||
| { | ||
| size_t ctfeHash(string x) | ||
| { | ||
| return hashOf(x.ptr, x.length); | ||
| return hashOf(x.ptr, x.length, 0); | ||
| } | ||
|
|
||
| enum test_str = "Sample string"; | ||
| enum size_t hashVal = ctfeHash(test_str); | ||
| assert(hashVal == hashOf(test_str.ptr, test_str.length)); | ||
| assert(hashVal == hashOf(test_str.ptr, test_str.length, 0)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the slicing is an unsafe operation. The older code was unsafe as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can this interface (dereferencing a
void*) even be@safein the first place?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getHashis supposed to take a pointer to the object matching theTypeInfo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"is supposed" is something entirely different than "is guaranteed to". You could of course argue that the interface was broken before, but it still remains a safety violation that should at least be recorded in Bugzilla (and subsequently be deprecated/made un-@safe).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do add a bugzilla issue for it. Such is beyond the scope of this PR, however, and should not impede it.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR adds a
@trustedannotation that is clearly wrong. It's barely acceptable because the code was already broken before, but now it is your responsibility to make sure it is eventually cleaned up. Previously, the place would have been flagged automatically once the mistaken@trustedannotation on hashOf is removed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The @safe was equally wrong. This PR does not address (or change) the interface to getHash(), the change was made to reflect the unsafe operation inside the function. The issue you brought up is with the interface to getHash(), not the interface to hashOf().
It is the responsibility of people who find bugs to post them to bugzilla. The bug you found has nothing to do with this PR, which is to fix the interface to
hashOf(). You should take the credit for finding the issue by posting it to bugzilla.Meanwhile, this PR is only concerned with the interface to
hashOf.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No;
@safecan never be wrong as in breaking the type system, it can just cause code not to compile. You are adding a new, incorrect instance of@trusted, which does add an extra type system violation.As I'm sure you realise, this is a huge deal because instances where things are mistakenly marked as
@safecan be detected automatically, which is impossible for mistaken uses of@trusted. The only way to find those is to painstakingly go through all occurrences of@trustedand manually audit each one of them.Just trust me on that one. I've found a large share of the Phobos safety bugs you went through recently, this PR would add another such bug, I point it out, you fix/document it before it goes in. That's how pull request review works. You can't justify knowingly introducing an issue without any documentation, etc. by pointing out that the code base contained bugs before.