Skip to content

Handle StringValue unint parsing when input is invalid (usually becau… #199

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 3 commits into from
Sep 8, 2019
Merged

Handle StringValue unint parsing when input is invalid (usually becau… #199

merged 3 commits into from
Sep 8, 2019

Conversation

kwhopper
Copy link
Collaborator

…se of encoding)

Copy link
Owner

@drewnoakes drewnoakes left a comment

Choose a reason for hiding this comment

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

Can you provide details of a scenario that benefits from this? It'd be good to include such information in a comment too.

{
return uint.Parse(ToString());
}
catch(Exception)
Copy link
Owner

Choose a reason for hiding this comment

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

Can we capture a more specific kind of exception here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I could have it capture the three exceptions that uint.Parse can throw - ArgumentNullException, FormatException, OverflowException

foreach (var b in Bytes)
{
val = val << 8;
val += b;
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not this approach makes sense if there are more than four bytes (or perhaps also when fewer).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was copied from ToInt32, which was copied from Java. I could try to run more tests here but this code has been in both libraries for a while.

@@ -87,7 +87,23 @@ int IConvertible.ToInt32(IFormatProvider provider)

ushort IConvertible.ToUInt16(IFormatProvider provider) => ushort.Parse(ToString());

uint IConvertible.ToUInt32(IFormatProvider provider) => uint.Parse(ToString());
uint IConvertible.ToUInt32(IFormatProvider provider)
Copy link
Owner

Choose a reason for hiding this comment

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

Why only uint? Why not all the other supported integral types as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I haven't come across an issue that involved the other types so decided to leave them be for now.

@kwhopper
Copy link
Collaborator Author

This is a rare exception, but this image from the images repo exhibits the problem:

jpg/Issue 122.jpg

IPTC tag "File Format" (0114) was blank in .NET. The reason is the bytes in this StringValue are set to decode using UTF-8 and the result string has encoding characters in it: \0\u0003
... which can't be parsed by uint

Java catches the parsing exception but implements something similar to this PR to overcome it manually; .NET simply swallows the exception.

This is a very rare situation in the entire images repo. I figured since a work-around has been in the Java library for so long and this is effectively a port, it was reasonable. But, I didn't exhaustively test whether the Java port is 100% correct for .NET.

@drewnoakes drewnoakes merged commit 613f7f9 into drewnoakes:master Sep 8, 2019
@kwhopper kwhopper deleted the stringvalue-touint32-exception branch September 8, 2019 14:57
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.

2 participants