Skip to content

refactor: build the librrd argument list without the dummy slot - #16

Open
somethingwithproof wants to merge 17 commits into
php:masterfrom
somethingwithproof:refactor/argv-builder
Open

refactor: build the librrd argument list without the dummy slot#16
somethingwithproof wants to merge 17 commits into
php:masterfrom
somethingwithproof:refactor/argv-builder

Conversation

@somethingwithproof

@somethingwithproof somethingwithproof commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

rrd_args_init_by_phparray() is the shared path for all twelve functions, and two of the bugs in the branches below it lived here. The structure is the reason.

Slot 0 held a literal nothing ever read:

result->args[0] = "dummy";          /* string literal into a char * */
result->args[1] = estrdup(command_name);
...
rrd_fetch(argv->count - 1, &argv->args[1], ...);   /* every call site compensates */

void rrd_args_free(rrd_args *args) {
    for (i = 1; i < args->count; i++)   /* ownership encoded as a bare 1 */
        efree(args->args[i]);
}

So ownership was positional and unwritten: slot 0 borrowed, the rest owned, and nothing enforced it. This drops the slot, gives every element the same lifetime, and lets callers pass the array as it stands.

Also in here, since they were all consequences of the same shape:

  • zend_hash_internal_pointer_reset / move_forward against a pre-captured count becomes ZEND_HASH_FOREACH_VAL, bounded by what was actually allocated.
  • Each option was built into a smart_string, estrdup'd out of it, then the smart_string freed. One estrndup from the zend_string does it.
  • Eleven call sites reported "cannot allocate arguments options" for what is nearly always a validation failure. The builder now says what went wrong, and php_error_docref names the function: RRDCreator::save(): options array must not be empty.
  • The same char *argv[3] / "dummy" fossil in rrd_info() and rrd_lastupdate() is gone.

No behaviour change beyond the diagnostic text, which two tests are updated for.

Validation

33/33 on PHP 8.1, 8.4, 8.5 and 8.6.0-dev, zero compiler warnings on librrd 1.7.2 and 1.11.0, and clean under valgrind --leak-check=full.


Stack order. This is part of a series, each building on the one before. Merge order:
#11 (test suite) → #12 (memory safety) → #13 (open_basedir, value semantics) → #14 (php-src master) → #15 (librrd argv) → #16 (argument builder) → #17 (autoconf). Commits below this PR's own belong to the ones underneath it.

Without tests/data the SKIPIF blocks skip 16 of the 22 tests, so CI reported green while most of the suite never ran.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Both were written against an older librrd and fail against 1.7.2 and 1.11.0 alike; they were never noticed because the fixtures were missing.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Called with one argument, zend_parse_parameters never writes start_time, so the following `if (start_time)` dereferenced whatever the stack happened to hold and copied it into the --start option.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
An integer key gave RRDUpdater::update() a NULL zend_string, save() on a bare RRDCreator merged undefined zvals, and every method assumed __construct had run; all three reached librrd through a NULL pointer.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
ZEND_LTOA writes its terminator at the return value of snprintf, so an 11-byte buffer overflowed once a timestamp needed more than ten digits, and rrd_fetch's 32-bit loop counter wrapped past the end of librrd's data.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
A second __construct kept the previous step, data sources and archives, so the new file was written from the old object's definition.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Both write a file the caller names, and both skipped the check that rrd_create() and RRDGraph::save() already perform.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
convert_to_string() edited the array in place, so a non-string option changed the caller's variable and every by-value copy of it.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
zval_get_string() hands back an empty string with an exception already pending, so the original Error was masked by whatever librrd then reported.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Converting a value runs __toString, which can call __construct to repoint the object past the open_basedir check, or setOptions to free the array being walked.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
…t list

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The "was not constructed" guards test for NULL, and rrd_fetch keyed its per-source arrays by name, so duplicate names left the walk short of ds_cnt entries.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
… objects

The duplicate-name case is a crash on master: renaming one ds_nam over another in the header leaves rrd_fetch walking past the end of the array it built.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
ZEND_LTOA, zval_dtor and XtOffsetOf are all gone from php-src master, so the extension no longer compiles on 8.6.0-dev.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Keeps the 8.6 build from breaking again without letting an unrelated upstream change gate pull requests.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
librrd took char ** before 1.9 and const char ** from 1.9 on, so the call sites warned on exactly one of the two and there was no spelling that suited both.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Slot 0 held an unused literal that every caller skipped with &args[1] and count - 1, and rrd_args_free() encoded that as a bare i = 1, so ownership was positional and unwritten.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
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.

1 participant