Skip to content

Log qprintf Errors in xstring.c#103

Open
Lightning11wins wants to merge 25 commits into
masterfrom
log-qprintf-errors-xstring
Open

Log qprintf Errors in xstring.c#103
Lightning11wins wants to merge 25 commits into
masterfrom
log-qprintf-errors-xstring

Conversation

@Lightning11wins
Copy link
Copy Markdown
Contributor

Quick change that improves error handling of qprintf errors in xstring.c.

Blocked behind #97, which will obviously need to be merged before this PR is reviewed.

…branch).

Fix a bug in qprintf() that caused % and & characters inside conditional printing areas to always print, regardless of the condition.
Improve documentation for qpfPrintf_va_internal() and qpf_grow_fn_t().
Clean up.
Reorder some code to improve clarity.
Add various comments.
Fix spacing mistakes.
Add void to function signatures that do not take parameters.
Clean up some overly cleaver code.
…d more comments.

Overhaul qpf_internal_Translate() to improve readability.
Improve readability of qpf_internal_SetupTable().
Improve code in multiple functions that ensures the module is initialized.
Clean up and improve function doc comments.
Add line numbers to errors.
Add qpfLogErrors().
Add qpf_internal_getErrorName().
Add qpf_internal_count_zeros().
Add QPF_ERR_T_NO_ERRORS.
Add QPF_ERR_COUNT.
Improve formating of QPF_ERR_T defines.
Add docs for session functions.
Add license.
Clarify how to use source and filter format specifiers.
Simplify wording for filter specifiers to improve readability.
Fix is_only_followed_by_nlen using || instead of &&.
Fix closing quote space check didn't handle buffer overflows very well.
Fix null_session error line numbers not being initialized.
Fix qpfClearErrors() being lazy about clearing error line numbers.
Fix qpf_internal_count_zeros(0) edge case.
Fix a quote buffer overflow comment from Greptile.
Add test_qprintf_69.c to validate that the "fixed" code now works properly.

Note: Honestly, I'm so in the weeds that I only understand about 80% of what's going on here, but it works, and this probably isn't worth a ton of additional effort.
Remove an error check that always passed.
Add more explicit typecasts.
Update qpf_internal_itoa() to return size_t because it cannot error.
Add an UNLIKELY() to speed up qpf_internal_itoa().
Fix missing info in the qpf_internal_itoa() doc comment.
Add a test case to verify that the buffer underflow is fixed.
Clean up and improve correctness of test cases added on this branch.
…unsigned ints.

Add an error message if a line number overflows USHRT_MAX.
Add a doc comment to error sessions.
Update copyright notice dates and descriptions.
Clean up and improve some spacing.
Add doc comment to QPSession.
Add qpfNoGrow() and qpfSysMallocGrow() grow functions for use when calling qpfPrint_g*() functions.
Add qpfPrintf_g() and qpfPrintf_gva() qprintf variants that allow passing a grow function.
Improve doc comments for all qpfPrintf*() functions.
Add code to use a pQPSession to record errors.
Add call to qpfLogErrors() when an error occurs.
Update copyright notice.
Clean up.
@Lightning11wins Lightning11wins self-assigned this May 14, 2026
@Lightning11wins Lightning11wins added ai-review Request AI review for PRs. size: trivial Easy to review, probably ~100 lines or fewer. labels May 14, 2026
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 14, 2026

Greptile Summary

This PR improves error reporting in xs_internal_QPrintf within xstring.c by creating a QPSession before each qpfPrintf_va_internal call so that per-error-type line numbers can be captured and logged via the new qpfLogErrors() helper. A large portion of the diff is a significant refactor of qprintf.c and an expansion of QPSession to store per-error line numbers in ErrorLines[QPF_ERR_COUNT].

  • xstring.c: Switches from a bare printf to fprintf(stderr, ...) on failure and introduces a QPSession to call qpfLogErrors(), providing richer diagnostics.
  • qprintf.h / qprintf.c: Adds ErrorLines[QPF_ERR_COUNT] to QPSession, rewrites the QPERR macro to record the triggering line, adds qpfLogErrors(), and fixes a %SSYB filter that was silently falling to NOTIMPL.
  • Tests: Two new regression tests cover small-buffer quoting correctness and a buffer-underflow bug fix for %STR&QUOT.

Confidence Score: 5/5

Safe to merge; the session is always closed on all reachable paths and the fallback to NULL-session behavior is preserved when qpfOpenSession fails.

The xstring.c change is small and well-bounded: a session is opened after existing safety checks, used exactly once, and closed before the function returns. The large qprintf.c refactor is largely readability/doc improvement with one genuine fix (SSYB filter). No correctness issues found in the new error-tracking paths.

No files require special attention; the observations in qprintf.c are stylistic.

Important Files Changed

Filename Overview
centrallix-lib/src/xstring.c Core change: xs_internal_QPrintf now opens/closes a QPSession around each qpfPrintf_va_internal call for richer error logging; session cleanup is always reached, no leaks.
centrallix-lib/src/qprintf.c Large refactor: adds ErrorLines tracking via updated QPERR macro, qpfLogErrors(), qpfSysMallocGrow, qpfPrintf_g/gva; fixes missing QPF_SPEC_T_SSYB in filter dispatch.
centrallix-lib/include/qprintf.h QPSession gains ErrorLines[QPF_ERR_COUNT]; error constants rewritten as bit-shift literals; new public API declarations added.
centrallix-lib/tests/test_qprintf_69.c New regression test verifying %STR&QUOT null-termination in a 2-byte buffer; assertions are correct.
centrallix-lib/tests/test_qprintf_70.c New regression test verifying the buffer-underflow fix for %STR&QUOT with a 1-byte buffer; assertions are correct.
centrallix-sysdoc/QPrintf.md Documentation updated to describe qpfLogErrors and the new grow-function APIs; copyright year updated.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant xstring as xs_internal_QPrintf
    participant qprintf as qprintf.c

    Caller->>xstring: xsQPrintf / xsConcatQPrintf
    xstring->>qprintf: qpfOpenSession()
    qprintf-->>xstring: error_session (or NULL on OOM)
    xstring->>qprintf: qpfPrintf_va_internal(error_session, ...)
    note over qprintf: On error: QPERR sets Errors & ErrorLines[i]
    qprintf-->>xstring: rval
    alt "rval < 0"
        xstring->>qprintf: qpfLogErrors(error_session)
        qprintf-->>xstring: stderr output per error bit
    else "rval >= 0"
        xstring->>xstring: "this->Length += rval"
    end
    xstring->>qprintf: qpfCloseSession(error_session)
    xstring-->>Caller: rval
Loading

Reviews (2): Last reviewed commit: "Fix invalid use of check_ptr()." | Re-trigger Greptile

Comment thread centrallix-lib/src/xstring.c Outdated
@Lightning11wins
Copy link
Copy Markdown
Contributor Author

This PR is ready for human review.

@Lightning11wins Lightning11wins requested review from gbeeley and nboard May 14, 2026 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-review Request AI review for PRs. size: trivial Easy to review, probably ~100 lines or fewer.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant