Skip to content

Cast lexer bytes to unsigned char before ctype - #144

Open
kavyabhand wants to merge 1 commit into
codeplea:masterfrom
kavyabhand:fix/ctype-unsigned-char
Open

Cast lexer bytes to unsigned char before ctype#144
kavyabhand wants to merge 1 commit into
codeplea:masterfrom
kavyabhand:fix/ctype-unsigned-char

Conversation

@kavyabhand

Copy link
Copy Markdown

Summary

Casts s->next[0] to unsigned char before isalpha / isdigit in next_token.

Why

#105 (OPEN). On signed-char platforms, input byte 0x80 reaches isalpha(-128), which is undefined under C99 7.4p1. Stock glibc/Darwin often mask it; a trapping ctype aborts.

Test plan

  • make smoke / make smoke_pr
  • On a signed-char target, te_interp of a single 0x80 byte should still reject without UBSan/trap from ctype

C99 7.4 requires ctype arguments to be EOF or representable as
unsigned char. Plain char may be signed; high-bit bytes then become
negative ints. Addresses issue codeplea#105.
Copilot AI review requested due to automatic review settings August 2, 2026 10:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes undefined behavior in the lexer by ensuring bytes passed to <ctype.h> classification functions are cast to unsigned char, avoiding negative char values on signed-char platforms.

Changes:

  • Cast s->next[0] to (unsigned char) before calling isalpha.
  • Cast s->next[0] to (unsigned char) before calling isdigit in the identifier scan loop.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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