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

Reject bad hex values in xar checksums #2479

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gperciva
Copy link
Contributor

@gperciva gperciva commented Jan 8, 2025

Hex values should be A to F (and lower-case); if there's any other letters, reject them.

Hex values should be A to F (and lower-case); if there's any other
letters, reject them.
@gperciva
Copy link
Contributor Author

gperciva commented Jan 8, 2025

If any hex strings contain letters above 'f' (or 'F'), we get an overflow. So for example,

unsigned char x;
p[0] = 'g';

x = (p[0] - 'a' + 10) << 4;

x would be 272, but it has a max of 256, so it ends up with a value of 16. Since x is unsigned char, that's not a problem in terms of C programming, but I think it's not the intended behaviour.

Given the existing code, I think the best thing to do in this case is to reject the string by having atohex return (-1).

@gperciva gperciva changed the title Reject bad hex values in xar Reject bad hex values in xar checksums Jan 8, 2025
@gperciva
Copy link
Contributor Author

gperciva commented Jan 8, 2025

Technically, the modified code still contains a bug -- some of those left-shifts need a cast in order to avoid undefined behaviour.

However, I think the git history will be much more clear if we make that change later (along with fixing other such problems). This particular PR changes program behaviour (if given a badly-formed checksum); the upcoming cast is merely addressing a theoretical problem (because the C standard doesn't require implementations to use two's complement for negative numbers ).

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.

1 participant