Skip to content

Amend utf8.c: Add null pointer safeguards - #294

Merged
jquast merged 5 commits into
masterfrom
pr293-cleanup
Oct 9, 2026
Merged

jquast merged 5 commits into
masterfrom
pr293-cleanup

Conversation

@jquast

@jquast jquast commented Oct 6, 2026 •

Copy link
Copy Markdown
Owner

utf8.c: non-null contract, calloc empty results, stamp 0.9.3, add similar "null pointer safeguards" for alignment

Adds the utf8 C tests and the 0.9.3 history entry.

Explorer09 and others added 2 commits October 5, 2026 16:58
Make utf8 encode and decode functions check for null pointers for every
pointer argument passed to them. If the output pointer argument is NULL,
redirect the output to a temporary variable, which would be discardrd.

Also adjust wcwidth_decode_u32() and wcwidth_encode_u32() behaviors to
align with wcwidth_decode_u32_heap() behavior.

Signed-off-by: Kang-Che Sung <[email protected]>
Adds the utf8 C tests and the 0.9.3 history entry.
@codspeed

codspeed Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 13.39%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 120 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_ljust_udhr_lines 33.7 ms 38.9 ms -13.39%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing pr293-cleanup (fdb24b3) with master (f1793ab)

Open in CodSpeed

@codecov

codecov Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.82%. Comparing base (f1793ab) to head (fdb24b3).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #294      +/-   ##
==========================================
+ Coverage   87.76%   87.82%   +0.05%     
==========================================
  Files          41       41              
  Lines        5617     5628      +11     
  Branches     1504     1511       +7     
==========================================
+ Hits         4930     4943      +13     
  Misses        319      319              
+ Partials      368      366       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Though to me, this seems superflous -- I would want any program that
tries to "align" text with "NULL padding" to fail immediately, but maybe
this is my preference for higher level languages like python showing
through?
@Explorer09

Copy link
Copy Markdown
Contributor

Excuse me. I don't know the reason for requiring out_len of wcwidth_encode_u32 to be non-null, and similarly, why count of wcwidth_decode_u32 is required to be non-null.

I don't think the non-null requirement makes sense.

@BenBE

BenBE commented Oct 7, 2026

Copy link
Copy Markdown

Excuse me. I don't know the reason for requiring out_len of wcwidth_encode_u32 to be non-null, and similarly, why count of wcwidth_decode_u32 is required to be non-null.

I don't think the non-null requirement makes sense.

Quick answer: Given the input semantics, except for the decode-single case, just receiving the result pointer won'T allow you to know how long the allocated buffer, that was returned, is. Thus logically you want to ensure a caller is forced to receive both the buffer AND its length.

@jquast

jquast commented Oct 7, 2026 •

Copy link
Copy Markdown
Owner Author

Excuse me. I don't know the reason for requiring out_len of wcwidth_encode_u32 to be non-null, and similarly, why count of wcwidth_decode_u32 is required to be non-null.

I don't think the non-null requirement makes sense.

I'm not sure if you saw my reply earlier? #285 (comment)

In this library strings may contain NULL. This is used by python wcwidth and could be used by other languages where strings may contain NULL, or use with automation that expects to measure terminal output which certainly may contain null bytes.

>>> wcwidth.width('\x00\x00\x00123')
3
>>> wcwidth.wcswidth('\x00\x00\x00123')
3
>>> print('\x00\x00\x00123')
123

I did try to make this clear in the documentation in #286 in response to your PR #285 let me know if you read this and it is still not clear, I'm trying to make it clear!

@jquast

jquast commented Oct 7, 2026

Copy link
Copy Markdown
Owner Author

From the original 2007 wcswidth() by Markus Khun, https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c

/* The following two functions define the column width of an ISO 10646
 * character as follows:
 *
 *    - The null character (U+0000) has a column width of 0.
 * ..

@jquast
jquast merged commit 0614f42 into master Oct 9, 2026
37 of 38 checks passed
@jquast
jquast deleted the pr293-cleanup branch October 9, 2026 13:10
@Explorer09

Copy link
Copy Markdown
Contributor

Excuse me. I don't know the reason for requiring out_len of wcwidth_encode_u32 to be non-null, and similarly, why count of wcwidth_decode_u32 is required to be non-null.
I don't think the non-null requirement makes sense.

I'm not sure if you saw my reply earlier? #285 (comment)

In this library strings may contain NULL. This is used by python wcwidth and could be used by other languages where strings may contain NULL, or use with automation that expects to measure terminal output which certainly may contain null bytes.

I'm sorry, but I was referring to null pointers, not NUL bytes or NUL code point.

@Explorer09

Copy link
Copy Markdown
Contributor

Quick answer: Given the input semantics, except for the decode-single case, just receiving the result pointer won'T allow you to know how long the allocated buffer, that was returned, is. Thus logically you want to ensure a caller is forced to receive both the buffer AND its length.

@BenBE
Some use cases with C strings do not require the string lengths to explicitly tracked. For example, I might do a string conversion only to pass the string to printf later, and then I free the string immediately after.
The memory allocator might internally track the allocated buffer size, but not every use case repuires such size to be handled by the user/caller.

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.

3 participants