Skip to content

Qprintf Rewrite - #97

Open
Lightning11wins wants to merge 156 commits into
masterfrom
qprintf-rewrite2
Open

Qprintf Rewrite#97
Lightning11wins wants to merge 156 commits into
masterfrom
qprintf-rewrite2

Conversation

@Lightning11wins

@Lightning11wins Lightning11wins commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

qprintf.c had some code that confused me a lot. For example, there was a switch statement inside an if statement inside another switch statement inside a loop inside another if statement inside the else block of different if statement inside another if statement inside another loop... causing my brain to throw a stack overflow exception.

After understanding the code (which will be useful for the apos_autoscale proejct), I did some refactoring, and now it's much easier to read, in my opinion. Also, I added some tools for improved error handling, as usual.

GitHub Relationships

Greptile Summary

…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.
@Lightning11wins Lightning11wins self-assigned this Apr 13, 2026
@Lightning11wins Lightning11wins added enhancement ai-review Request AI review for PRs. documentation Changes, improvements, or fixes to documentation files. labels Apr 13, 2026
@Lightning11wins

Copy link
Copy Markdown
Contributor Author

@greptileai Please review this PR.

@greptile-apps

greptile-apps Bot commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR substantially rewrites QPrintf and expands its tests, documentation, error reporting, and supporting library utilities.

  • Refactors format parsing, conversion, quoting, growth, and overflow handling.
  • Adds detailed QPrintf error tracking and extensive focused test coverage.
  • Adds reusable checking, range, timer, string-formatting, and utility helpers.
  • Updates the library build and parallel test infrastructure.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
centrallix-lib/src/qprintf.c Rewrites QPrintf parsing and output handling; the previously reported predicate, initialization, no-grow, quote-boundary, syntax, and capacity issues are addressed at current HEAD.
centrallix-lib/include/qprintf.h Expands the public QPrintf API and session error metadata to support the rewritten implementation.
centrallix-lib/tests/test_qprintf_69.c Verifies that quoted overflow in a two-byte destination retains a quote and null terminator.
centrallix-lib/tests/test_qprintf_70.c Covers the one-byte quoted-output boundary without permitting an out-of-bounds quote write.
centrallix-sysdoc/QPrintf.md Documents QPrintf behavior and the expanded formatting interface.
centrallix-lib/Makefile.in Adds new library objects and parallelized test and Valgrind execution infrastructure.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Parse format string] --> B[Resolve source specifier]
  B --> C[Apply length and conversion filters]
  C --> D{Quoted output?}
  D -- Yes --> E[Reserve and write opening quote]
  D -- No --> F[Translate or copy content]
  E --> F
  F --> G[Reserve and write closing quote]
  G --> H[Null-terminate destination]
  H --> I[Return copied length and session errors]
Loading

Reviews (17): Last reviewed commit: "Merge branch 'add-utils' into qprintf-re..." | Re-trigger Greptile

Comment thread centrallix-lib/src/qprintf.c Outdated
Comment thread centrallix-lib/src/qprintf.c Outdated
Comment thread centrallix-lib/src/qprintf.c
Comment thread centrallix-lib/src/qprintf.c
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.
@Lightning11wins

Copy link
Copy Markdown
Contributor Author

@greptileai I think I resolved your comments. Please rereview. Are there any other concerns you have?

Comment thread centrallix-lib/src/qprintf.c Outdated
@Lightning11wins

Copy link
Copy Markdown
Contributor Author

@greptileai I think I resolved your comments. Please rereview. Are there any other concerns you have?

Comment thread centrallix-lib/src/qprintf.c Outdated
@greptile-apps

greptile-apps Bot commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

The root cause is that min_room = 2 only establishes the 2-byte reserve after Translate writes something. When no_grow is already true on entry, Translate's hard route short-circuits and writes nothing — dest_offset is unchanged, so the reserve is never created.

Instead of making the write conditional (which breaks your tests), clamp dest_offset right before the unconditional write:

			    no_grow = true;
			    }
			
			/** Write the closing quote, even if a buffer overflow occurred. **/
			/** We passed min_room = 2, so there will be enough space for it. **/
			/** However, if Translate wrote nothing (e.g. no_grow was already  **/
			/** true on entry), the reserve may not have been established — so  **/
			/** clamp dest_offset to guarantee room for quote + null terminator. **/
			if (dest_offset + 2 > *dest_size)
			    dest_offset = *dest_size - 2;
			(*dest)[dest_offset++] = quote;

The clamp is a no-op in the normal case — when Translate respected min_room = 2, dest_offset + 2 <= *dest_size already holds. It only activates when the buffer was already full on entry to the Translate call, ensuring the closing quote and null terminator always fit. This preserves the contract test_qprintf_51 and test_qprintf_52 rely on.

@Lightning11wins

Copy link
Copy Markdown
Contributor Author

@greptileai I've made the changes you've requested but I haven't tested or pushed my code yet. Could you write a test case, following the same style established in test_qprintf_51.c that will ensure that will verify that this issue is fixed? Thus, it should fail on the previous implementation (the one that did not move the pointer AND the one that did not check no_grow), but it should pass on the current implementation that you showed me, which handles the situation correctly.

@greptile-apps

greptile-apps Bot commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Here's the test following the exact style of test_qprintf_51.c/test_qprintf_52.c.

Why this reproduces the bug: A 2-byte buffer for %STR&QUOT with input "ab" forces the exact path in question.

  1. Opening quote written at buf[4], dest_offset = 1.
  2. Translate writes nothing (no_grow triggered — no room for content + min_room = 2).
  3. Closing-quote path: dest_offset + 2 = 3 > 2.
    • Without dest_offset clamp: closing quote written at buf[5], dest_offset = 2. Null-term guard 2 > 2 is false → no null written. buf[5] == '\''.
    • Without no_grow guard: grow_fn is called on a static buffer, returns false, falls through to same unconditional write → same result.
    • With the full fix: dest_offset clamped to *dest_size - 2 = 0, closing quote at buf[4], null at buf[5]. ✓
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include "qprintf.h"
#include <assert.h>

long long
test(char** tname)
    {
    int i, rval;
    int iter;
    unsigned char buf[16];

	*tname = "qprintf-53 %STR&QUOT with buffer too small for content (null termination)";
	iter = 200000;
	for(i=0;i<iter;i++)
	    {
	    buf[9]  = '\n';
	    buf[8]  = '\0';
	    buf[7]  = 0xff;
	    buf[6]  = 0xff;  /* sentinel: byte immediately after 2-byte buffer -- must not be clobbered */
	    /* buf+4 is the 2-byte buffer under test: occupies buf[4] and buf[5] */
	    buf[3]  = '\n';
	    buf[2]  = '\0';
	    buf[1]  = 0xff;
	    buf[0]  = '\0';
	    rval = qpfPrintf(NULL, buf+4, 2, "%STR&QUOT", "ab");
	    /* The 2-byte buffer must be null-terminated within its own bounds.
	     * Without dest_offset clamped before the closing-quote write,
	     * buf[5] holds '\'' and no null terminator is ever written. */
	    assert(buf[5] == '\0');
	    /* The byte immediately after the buffer must not be overwritten.
	     * Without the no_grow guard, an out-of-bounds null-terminator write
	     * could clobber this sentinel. */
	    assert(buf[6] == 0xff);
	    /* Bytes before the buffer must be untouched */
	    assert(buf[3] == '\n');
	    assert(buf[2] == '\0');
	    assert(buf[1] == 0xff);
	    assert(buf[0] == '\0');
	    /* Return value must indicate overflow: full output 'ab' + NUL = 5 bytes */
	    assert(rval == 5);
	    }

    return iter;
    }

Failure summary by variant:

Variant buf[5] after call assert(buf[5] == '\0')
No no_grow guard (original) '\'' — closing quote, no null FAILS
Guard added, no dest_offset clamp '\'' — same FAILS
Full fix (guard + clamp) '\0' passes

assert(buf[6] == 0xff) is a secondary tripwire: if a null terminator were written one byte past the buffer instead of being omitted, that sentinel catches it regardless.

One caveat: I inferred rval == 5 from the pattern in tests 51/52 (return value = bytes needed including NUL, for 'ab' that's 5). If the actual semantics differ, substitute assert(rval > 2).

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.
@Lightning11wins

Copy link
Copy Markdown
Contributor Author

@greptileai I think I resolved your comments, and I also added your test case with some clean up, bug fixing (on the return value), and other improvements. Please rereview. Are there any other concerns you have?

Comment thread centrallix-lib/src/qprintf.c Outdated
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. documentation Changes, improvements, or fixes to documentation files. size: medium Might be hard to review, usually less than ~5000 lines. testing Includes testing, either new tests or updates to existing tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add %BOOL to qprintf() qprintf() prints % and & inside skipped conditionals

2 participants