Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Serialization of unicode characters #179

@j-moeller

Description

@j-moeller

Hello,

I believe, we found an issue when handling (undefined) unicode characters that are encoded using the 12-character sequence (starting from \uDBC0\uDFFF). In the program, we use a string containing "\uDBFF\uDFFF" (U+10ffff) which is then incorrectly encoded as 0xf3bfbfbf (U+FFFFF) instead of 0xf48fbfbf:

#include "json-builder/json-builder.h"
#include "json-parser/json.h"

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

int main()
{
    // Starting from \\uDBC0\\uDFFF
    char buf[] = "{\"\\uDBFF\\uDFFF\": 0}";
    size_t size = sizeof(buf) / sizeof(*buf);

    json_settings settings = {};
    settings.value_extra = json_builder_extra; /* space for json-builder state */

    char error[128];
    json_value* json = json_parse_ex(&settings, buf, size, error);
    if (json == NULL) {
        return -1;
    }

    json_serialize_opts serialize_opts = {};
    serialize_opts.mode = json_serialize_mode_packed;
    serialize_opts.opts = json_serialize_opt_pack_brackets |
                          json_serialize_opt_no_space_after_comma |
                          json_serialize_opt_no_space_after_colon;

    size_t required_size = json_measure_ex(json, serialize_opts);

    char* json_buf;
    if (NULL == (json_buf = malloc(required_size + 1))) {
        return -1;
    }

    json_serialize_ex(json_buf, json, serialize_opts);
    json_buf[required_size] = 0;

    for (int i = 0; i < required_size; i++) {
        if (0x20 <= json_buf[i] && json_buf[i] < 0x7f) {
            printf("%d\n", json_buf[i]);
        } else {
            printf("0x%x\n", json_buf[i] & 0xff);
        }
    }

    free(json_buf);
    json_builder_free(json);

    return 0;
}

Output:

123
34
0xf3
0xbf
0xbf
0xbf
34
58
48
125
0x0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions