libucl icon indicating copy to clipboard operation
libucl copied to clipboard

lookup or insert functions not work correctly

Open wxie7 opened this issue 8 months ago • 0 comments

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;
}

wxie7 avatar May 30 '24 12:05 wxie7