Skip to content

in BUGFIX return the previous start address when changing memory input - #2641

Open
GeekQAQ wants to merge 1 commit into
CESNET:develfrom
GeekQAQ:in-memory-start-return
Open

GeekQAQ wants to merge 1 commit into
CESNET:develfrom
GeekQAQ:in-memory-start-return

Conversation

@GeekQAQ

@GeekQAQ GeekQAQ commented Nov 19, 2026

Copy link
Copy Markdown

ly_in_memory() and ly_in_memory_chunk() are documented to return the "Previous starting address to read data from", and "the caller is responsible to free the data in case of changing string pointer". Once something has been read from the input (ly_in_read(), or a parser such as lyd_parse_data()), they return the current read position instead, so freeing the returned pointer as documented is an invalid free.

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

#include <libyang/libyang.h>

int
main(void)
{
    struct ly_in *in;
    char *data = strdup("abcdef"), buf[2];
    const char *prev;

    ly_in_new_memory(data, &in);
    ly_in_read(in, buf, 2);

    /* documented to return the previous starting address, which the caller frees */
    prev = ly_in_memory(in, "xyz");
    printf("returned data + %d\n", (int)(prev - data));
    fflush(stdout);
    free((char *)prev);

    ly_in_free(in, 0);
    return 0;
}

On devel f45939c this prints returned data + 2 and then ASan reports attempting free on address which was not malloc()-ed, 2 bytes inside the 7-byte block; a build without sanitizers aborts with free(): invalid pointer. After a successful lyd_parse_data() the returned pointer is at the end of the parsed data.

Cause: both functions return in->current instead of in->start.

Fix: return in->start. Before anything is read the two are equal, so the existing cases do not change. The query form (str or mem NULL) now also returns the start of the data. The new cases in test_input_mem read two bytes before changing the input; they 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 (70 tests).

ly_in_memory() and ly_in_memory_chunk() are documented to return the
previous starting address, which the caller is responsible to free,
but they returned the current read position. After some data were
read or parsed, the returned pointer pointed inside the previous data
and freeing it was an invalid free.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 21:50

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