Skip to content

Fix Issue 11338 - std.uri URIerror should be an Exception #1659

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

Merged
merged 4 commits into from
Nov 1, 2013
Merged

Fix Issue 11338 - std.uri URIerror should be an Exception #1659

merged 4 commits into from
Nov 1, 2013

Conversation

DannyArends
Copy link
Contributor

  • URIerror renamed to URIException
  • URIException class inherits from Exception
  • Introduces human readable errors for URI parsing
  • What to do when alloca returns NULL
throw new OutOfMemoryError("Alloca failure");

- URIerror renamed to URIexception
- URIexception class inherits from Exception
@@ -116,7 +116,7 @@ private string URI_Encode(dstring string, uint unescapedSet)
{
R2 = cast(char *)alloca(Rsize * char.sizeof);
if (!R2)
Copy link

Choose a reason for hiding this comment

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

Uhm.. As far as I can tell alloca doesn't return 0 like a regular malloc call, it's usually documented as:

alloca() returns a pointer to the beginning of the allocated space. If the allocation causes stack overflow, program behaviour is undefined.

So I'm not sure why there was a check here. But in any case, this is an error condition, not a regular exception.

Copy link
Member

Choose a reason for hiding this comment

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

To be perfectly pedantic, if alloca returns null then the appropriate exception should be something like AllocaError (rather plain Error to be realistic), as it has no defined cross-platform meaning. However, it's likely that the platforms that do return null from alloca mean it to be OOM, so calling core.exception.onOutOfMemoryError might be the right move, even though in certain programs it might result in misguided attempts at freeing heap memory then retrying, which may or may not run out of stack space again.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed. if alloca returns 0, then an error needs to be thrown. I think it would be wrong to call onOutOfMemoryError, since we aren't strictly dealing with memory, but rather, throw a straight up throw new OutOfMemoryError("Alloca Failure"). But I'm not certain about this.

Rest of the pull looks good (only gave it a quick glance though).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

a quick grep -r "alloca(" in phobos shows that in general the return value (0/NULL) is ignored:
stream.d: No check for NULL return value
process.d: No check for NULL return value
typecons.d: No check for NULL return value

Copy link

Choose a reason for hiding this comment

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

MSDN says they use SEH exceptions when alloca fails, whereas for DMC/Posix I'm not really sure what alloca does if it fails.

@DannyArends
Copy link
Contributor Author

True, didn't spot the allocation.

Should it be separated then into an Error on allocation failure
and an Exception then for the checking of the input URI ?

@ghost
Copy link

ghost commented Oct 24, 2013

Should it be separated then into an Error on allocation failure
and an Exception then for the checking of the input URI ?

I think an OutOfMemoryError should be thrown (from core.exception). I'd like to wait for others' input on this, @monarchdodra @alexrp etc.

As for the exception, it should also give some information as to what went wrong , "URI error" gives no information on what went wrong.

@@ -36,7 +36,7 @@ private import std.c.stdlib;
private import std.utf;
import std.exception;

class URIerror : Error
class URIexception : Exception
Copy link
Member

Choose a reason for hiding this comment

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

I believe the correct name would be URIException, i.e. with a capital "E". (the old name was wrong too)

@DannyArends
Copy link
Contributor Author

OK

Thanks for your time,
I'm more then willing to revise the pull based on the outcome of your discussion.

ps. I made a forum post also: http://forum.dlang.org/thread/xpiewrackieqynsmxmlb@forum.dlang.org

@ghost
Copy link

ghost commented Oct 24, 2013

Yep, saw it. Thanks.

- Now call the onOutOfMemoryError() function when alloca returns NULL
- Fixed the URIexception name to URIException
- Added a string constructor for the URIException class
- Added more informative messages to some of the errors
- Now all thrown URIException have human readable text
- throw new OutOfMemoryError("Alloca Failure"); // Make code look more consistant
@DannyArends
Copy link
Contributor Author

Hey people,

Is there yet any consensus on how to handle the alloca errors ?

The behaviour on an alloca failure only changes slightly with this fix.. The function used to throw an
URIerror which was/is obviously wrong,

I changed it to throw OutOfMemoryError("Alloca Failure"), which is more descriptive then the previously thrown URIerror

I think this is more or less the best we can do now (seeing as there is no default way to handle alloca failure under posix/unix like systems.

if (!isHexDigit(s[k + 1]) || !isHexDigit(s[k + 2]))
goto LthrowURIerror;
throw new URIException("Expected HexDigit After '%'");
Copy link
Collaborator

Choose a reason for hiding this comment

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

"Expected two HexDigits After '%'" also, english words: "Expected two hexadecimal digits After '%'"

@monarchdodra
Copy link
Collaborator

LGTM appart from those two last nitpicks. Will pull after you fix and it greens.

@yazd
Copy link
Contributor

yazd commented Nov 1, 2013

This is also a nitpick, but wouldn't sentence casing be better for the messages rather than the capitalization of each word?
e.g. UTF-32 Code Point Size Too Large => UTF-32 code point size too large

@DannyArends
Copy link
Contributor Author

There is a failure in the autotester (FreeBSD_64):

Testing std.parallelism: FAIL
core.thread.ThreadException@src/core/thread.d(935): Unable to set thread priority

Is this related to my commits ? Seems hardly possible

@monarchdodra
Copy link
Collaborator

Is this related to my commits ? Seems hardly possible

Nope, it's a failure that happens from time to time. It's not your fault. Don't worry about, we know about it.

@ghost
Copy link

ghost commented Nov 1, 2013

Yes, LGTM too. We'll have to wait for the next test-run cycle so it greens.

Sorry for the delay @DannyArends, we'll merge your first pull soon enough. :)

monarchdodra added a commit that referenced this pull request Nov 1, 2013
Fix Issue 11338 - std.uri URIerror should be an Exception
@monarchdodra monarchdodra merged commit 42eec50 into dlang:master Nov 1, 2013
@monarchdodra
Copy link
Collaborator

TYVM for putting up with us :)

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.

4 participants