Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lookup or insert functions not work correctly #308

Open
wxie7 opened this issue May 30, 2024 · 0 comments
Open

lookup or insert functions not work correctly #308

wxie7 opened this issue May 30, 2024 · 0 comments

Comments

@wxie7
Copy link

wxie7 commented May 30, 2024

Hello, maybe there exist a bug. ucl_parser_add_string and ucl_object_insert_key work normally, but ucl_object_lookup can't find the previous value, so hit __builtin_trap

void check_and_trap_if_missing(const ucl_object_t *obj, const char *key, const char *expected_value) {
    const ucl_object_t *found_obj = ucl_object_lookup(obj, key);
    if (!found_obj) {
        __builtin_trap();
    }
    ucl_object_t *expected_obj = ucl_object_fromstring(expected_value);
    const char *old_str = ucl_object_tostring(found_obj);
    const char *expected_str = ucl_object_tostring(expected_obj);
    if (old_str == NULL || expected_str == NULL) {
        __builtin_trap();
    }
    if (strcmp(old_str, expected_str) != 0) {
        __builtin_trap();
    }
    ucl_object_unref(expected_obj);
}

int main(int argc, char **argv)
{
    struct ucl_parser *parser;
    parser = ucl_parser_new(0);
    const char *data = "sectrueion foo bar {\n\t  keyiK : value\n}\n";
    ucl_parser_add_string(parser, data, strlen(data));
	
    if (ucl_parser_get_error(parser) != NULL) {
        return 1;
    }
    const char key_buf[] = {"sectrueion"};
    const char value_buf[] = {" foo bar {\n\t  keyiK "};

    ucl_object_t *root_obj = ucl_parser_get_object(parser);
    if (!root_obj) {
        ucl_parser_free(parser);
        return 1;
    }

    ucl_object_t* new_objs = ucl_object_fromstring(value_buf);
    if (new_objs == NULL) {
        ucl_parser_free(parser);
        ucl_object_unref(root_obj);
        return 1;
    }

    if (!ucl_object_insert_key(root_obj, new_objs, key_buf, strlen(key_buf), true)){
        ucl_parser_free(parser);
        ucl_object_unref(root_obj);
        ucl_object_unref(new_objs);
        return 1;
    }

    // Check if the inserted or replaced objects exist
    check_and_trap_if_missing(root_obj, key_buf, value_buf);

    ucl_parser_free (parser);
    ucl_object_unref(root_obj);
    return 0;
}
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

No branches or pull requests

1 participant