Skip to content

out BUGFIX ly_out_filepath() error value for a handler of another type - #2646

Open
GeekQAQ wants to merge 1 commit into
CESNET:develfrom
GeekQAQ:out-filepath-argret
Open

GeekQAQ wants to merge 1 commit into
CESNET:develfrom
GeekQAQ:out-filepath-argret

Conversation

@GeekQAQ

@GeekQAQ GeekQAQ commented Nov 19, 2026

Copy link
Copy Markdown

ly_out_filepath() documents "NULL if changing filepath succeeds and ((void *)-1) otherwise". When the handler is not an LY_OUT_FILEPATH one (or is NULL), the argument check logs "Invalid argument" but returns NULL for a change and (void *)-1 for a query, the opposite of the documented values. The caller is told that the change succeeded, while the following output still goes to the previous target and the new file is not created.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <libyang/libyang.h>

int
main(void)
{
    struct ly_out *out;
    const char *ret;
    char *mem = NULL;

    ly_out_new_memory(&mem, 0, &out);

    /* the handler is not of the LY_OUT_FILEPATH type, the call is refused */
    ret = ly_out_filepath(out, "new.txt");
    printf("ly_out_filepath() returned %s\n", !ret ? "NULL (success)" : (ret == (void *)-1) ? "(void *)-1" : ret);
    ly_print(out, "data\n");
    printf("memory: \"%s\", new.txt %s\n", mem, access("new.txt", F_OK) ? "does not exist" : "exists");

    ly_out_free(out, NULL, 1);
    return 0;
}

On devel f45939c:

libyang[0]: Invalid argument out->type == LY_OUT_FILEPATH (ly_out_filepath()).
ly_out_filepath() returned NULL (success)
memory: "data
", new.txt does not exist

The same happens for handlers created by ly_out_new_fd(), ly_out_new_file() and ly_out_new_clb(), and with a NULL handler. The documentation does not say which handler types ly_out_filepath() accepts; the function treats other types as an invalid argument, and for that the documented value is (void *)-1.

Fix: return (void *)-1 from the argument check in both cases. A query on such a handler already returned it, so only the change case differs. The new cases in test_output_filepath fail on the current tree and pass with the fix.

Tested on devel f45939c with clang 21, -fsanitize=address,undefined, ENABLE_TESTS=ON: ctest passes (71 tests, including the format check).

When the handler was NULL or not of the LY_OUT_FILEPATH type, the
argument check returned NULL, the documented success value, for a
change of the file path. Return ((void *)-1) for both a change and
a query.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 21:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

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