Skip to content

Wrap ctype.h functions to avoid array subscript warnings#2306

Merged
jkbonfield merged 5 commits into
samtools:developfrom
daviesrob:wrap_ctype_funcs
Feb 23, 2026
Merged

Wrap ctype.h functions to avoid array subscript warnings#2306
jkbonfield merged 5 commits into
samtools:developfrom
daviesrob:wrap_ctype_funcs

Conversation

@daviesrob

Copy link
Copy Markdown
Member

Copy HTSlib's internal wrappers for interfaces in <ctype.h> into sam_utils.h (as it's widely included already) and use them on cases where the input has char type. Fixes "array subscript has type 'char'" warnings on platforms that still implement them as a macro around an array look-up.

The ctype interface wrappers were originally authored by John Marshall in HTSlib commit fc9aeb6f7.

Copy HTSlib's internal wrappers for interfaces in <ctype.h> into
sam_utils.h (as it's widely included already) and use them on
cases where the input has `char` type.  Fixes "array subscript
has type 'char'" warnings on platforms that still implement
them as a macro around an array look-up.

The ctype interface wrappers were originally authored by
John Marshall in HTSlib commit fc9aeb6
@jkbonfield

jkbonfield commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

There are a couple more, which this PR doesn't fix.

Edit: approved on the other changes though. Thanks

Need to cast to unsigned to ensure all possible values are +ve.
This function takes data from various sources that may not
have been well-filtered for unexpected character values.  It
therefore needs to be able to handle them, at least to the
extent of not producing undefined behaviour.

Rename variable `c` to `chr` to make it easier to find.

Change type of `chr` and `rb` to char, to match the type of
the data sources they may be set from.

Use toupper_c() wrapper due to the change of type above.

In case char is signed, cast `rb` to uint8_t before using it to
index seq_nt16_table.  Similarly, cast `chr` to uint8_t before
passing the value to tv->my_mvaddch() (which takes int) to
ensure the result it always positive.
Avoids sign-conversion warnings should they be enabled.
char should be cast to uint8_t, not int, to prevent sign extension.
@jkbonfield jkbonfield merged commit de008f1 into samtools:develop Feb 23, 2026
6 checks passed
@daviesrob daviesrob deleted the wrap_ctype_funcs branch February 23, 2026 09:48
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