Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounds checking in array functions #212

Open
osa1 opened this issue Oct 9, 2018 · 14 comments
Open

Bounds checking in array functions #212

osa1 opened this issue Oct 9, 2018 · 14 comments

Comments

@osa1
Copy link

osa1 commented Oct 9, 2018

We discovered today that array functions do not do bounds checking, which was surprising to us. The expectation was that unless the function is prefixed with "unsafe" it's supposed to do bounds checking.

After discovering this I checked the haddocks, but there's literally no mention to bounds checking in any of the array modules or at the top level. At the very least we should mention in bold letters that the array modules do not do bounds checking, and are completely unsafe.

For long-time primitive users and maintainers this is probably obvious, but it's far from being obvious from a user's point of view.

I realize that it's hard to change this behavior now, so perhaps we should just update the documentation. Ideally it'd be great to have writeArray that does bounds checking, and unsafeWriteArray that omits it though.

@treeowl
Copy link
Collaborator

treeowl commented Oct 9, 2018

Functions in this package are generally not safe, and are typically named after the underlying primitives. Improving documentation is always helpful. I would oppose changing the semantics now, because packages using this one rely on the efficiency of unchecked operations. I'm not sure if the checked operations belong in this package or not.

@cartazio
Copy link
Contributor

cartazio commented Oct 9, 2018 via email

@cartazio cartazio closed this as completed Oct 9, 2018
@cartazio
Copy link
Contributor

cartazio commented Oct 9, 2018

if you want safe arrays, use vector or array.

@treeowl
Copy link
Collaborator

treeowl commented Oct 9, 2018 via email

@cartazio
Copy link
Contributor

cartazio commented Oct 9, 2018 via email

@treeowl
Copy link
Collaborator

treeowl commented Oct 9, 2018 via email

@cartazio
Copy link
Contributor

true. I'd rather keep primitive a tad simpler in that respect. (eg have a build flag for checked mode or something)

but for now, lets keep this as a backburner todo :)

@cartazio cartazio reopened this Oct 10, 2018
@cartazio
Copy link
Contributor

for now lets approach this as "add a package flag for bounds checking operations "

@chessai
Copy link
Member

chessai commented Oct 10, 2018

@osa1 this isn't precisely what you want (i think), but may be of interest to you: http://hackage.haskell.org/package/primitive-checked

@chessai
Copy link
Member

chessai commented Oct 10, 2018

To clarify, that package mimics the API of primitive, but adds bounds checking and gives useful runtime error messages

@Shimuuar
Copy link

I don't think flag will work well. Every nontrivial program have primitive as dependency which means that every user of primitive will have to pay price of bounds checking even if they do them anyway. So if one package need that flag it will impose performance penalty on all other packages. And AFAIR it's not possible to depend flag selection of package.

It could function as debug flag at best with purpose of catching indexing bug in package that depends on it

@andrewthad
Copy link
Collaborator

I agree that we should be more explicit in the documentation about the lack of bounds-checking. Having a flag would be nice. As @chessai pointed out, http://hackage.haskell.org/package/primitive-checked is useful. It gets me 95% of where I want to be (plus, someone added CallStack everywhere, which makes the error messages pretty good). I would be in favor of pulling this into primitive itself behind a checked flag (similar to the unsafechecks that vector has). This would make it easier to use.

Specifically responding to @Shimuuar's concern:

So if one package needs that flag it will impose a performance penalty on all other packages

The whole purpose of the flag would be for when you are debugging an application. So, it would be running locally or in a staging environment. I've never had the need to perform bounds-checking at runtime in production. If you have an out-of-bounds access in production, your program is will still crash regardless of whether or not you are doing bounds-checking. You just get a better error message if you are bounds checking.

@Shimuuar
Copy link

As debugging flag it would be useful indeed. Also program wouldn't necessarily crash it may just produce garbage if you just have out of bounds reads and never runs into segfault

@cartazio
Copy link
Contributor

cartazio commented Oct 10, 2018 via email

Boarders added a commit to Boarders/primitive that referenced this issue Jun 5, 2020
This adds documentation to some currently undocumented functions.
This also adds notes indicating functions do not perform bounds or
other checks on inputs (haskell#212).
Boarders added a commit to Boarders/primitive that referenced this issue Jun 5, 2020
This adds documentation to some undocumented functions. It also
adds notes that bounds (and other) checks are not performed (haskell#212).
Boarders added a commit to Boarders/primitive that referenced this issue Jun 6, 2020
This adds documentation to some undocumented functions. It also
adds notes that bounds (and other) checks are not performed (haskell#212).
andrewthad pushed a commit that referenced this issue Jun 8, 2020
This adds documentation to some undocumented functions. It also
adds notes that bounds (and other) checks are not performed (#212).
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

No branches or pull requests

6 participants