From dcd0d7a9d5f81b752376b30d2128e7a681cc9064 Mon Sep 17 00:00:00 2001 From: ibireme Date: Sun, 21 Jul 2024 15:40:40 +0800 Subject: [PATCH] Fix warnings when directly including yyjson.c: #177 --- src/yyjson.c | 44 +++++++++++++++++++++++++--------------- src/yyjson.h | 57 ++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 68 insertions(+), 33 deletions(-) diff --git a/src/yyjson.c b/src/yyjson.c index 16ecbac..5ee0566 100644 --- a/src/yyjson.c +++ b/src/yyjson.c @@ -5875,14 +5875,16 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, val++; \ if (unlikely(val >= val_end)) { \ usize alc_old = alc_len; \ + usize val_ofs = (usize)(val - val_hdr); \ + usize ctn_ofs = (usize)(ctn - val_hdr); \ alc_len += alc_len / 2; \ if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ alc_old * sizeof(yyjson_val), \ alc_len * sizeof(yyjson_val)); \ if ((!val_tmp)) goto fail_alloc; \ - val = val_tmp + (usize)(val - val_hdr); \ - ctn = val_tmp + (usize)(ctn - val_hdr); \ + val = val_tmp + val_ofs; \ + ctn = val_tmp + ctn_ofs; \ val_hdr = val_tmp; \ val_end = val_tmp + (alc_len - 2); \ } \ @@ -6287,14 +6289,16 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, val++; \ if (unlikely(val >= val_end)) { \ usize alc_old = alc_len; \ + usize val_ofs = (usize)(val - val_hdr); \ + usize ctn_ofs = (usize)(ctn - val_hdr); \ alc_len += alc_len / 2; \ if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ alc_old * sizeof(yyjson_val), \ alc_len * sizeof(yyjson_val)); \ if ((!val_tmp)) goto fail_alloc; \ - val = val_tmp + (usize)(val - val_hdr); \ - ctn = val_tmp + (usize)(ctn - val_hdr); \ + val = val_tmp + val_ofs; \ + ctn = val_tmp + ctn_ofs; \ val_hdr = val_tmp; \ val_end = val_tmp + (alc_len - 2); \ } \ @@ -8501,6 +8505,9 @@ static_inline u8 *yyjson_write_minify(const yyjson_val *root, #define incr_len(_len) do { \ ext_len = (usize)(_len); \ if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ + usize ctx_pos = (usize)((u8 *)ctx - hdr); \ + usize cur_pos = (usize)(cur - hdr); \ + ctx_len = (usize)(end - (u8 *)ctx); \ alc_inc = yyjson_max(alc_len / 2, ext_len); \ alc_inc = size_align_up(alc_inc, sizeof(yyjson_write_ctx)); \ if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ @@ -8508,11 +8515,10 @@ static_inline u8 *yyjson_write_minify(const yyjson_val *root, alc_len += alc_inc; \ tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ - ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ - memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ + memmove((void *)ctx_tmp, (void *)(tmp + ctx_pos), ctx_len); \ ctx = ctx_tmp; \ - cur = tmp + (cur - hdr); \ + cur = tmp + cur_pos; \ end = tmp + alc_len; \ hdr = tmp; \ } \ @@ -8685,6 +8691,9 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root, #define incr_len(_len) do { \ ext_len = (usize)(_len); \ if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ + usize ctx_pos = (usize)((u8 *)ctx - hdr); \ + usize cur_pos = (usize)(cur - hdr); \ + ctx_len = (usize)(end - (u8 *)ctx); \ alc_inc = yyjson_max(alc_len / 2, ext_len); \ alc_inc = size_align_up(alc_inc, sizeof(yyjson_write_ctx)); \ if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ @@ -8692,11 +8701,10 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root, alc_len += alc_inc; \ tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ - ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ - memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ + memmove((void *)ctx_tmp, (void *)(tmp + ctx_pos), ctx_len); \ ctx = ctx_tmp; \ - cur = tmp + (cur - hdr); \ + cur = tmp + cur_pos; \ end = tmp + alc_len; \ hdr = tmp; \ } \ @@ -9053,6 +9061,9 @@ static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root, #define incr_len(_len) do { \ ext_len = (usize)(_len); \ if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ + usize ctx_pos = (usize)((u8 *)ctx - hdr); \ + usize cur_pos = (usize)(cur - hdr); \ + ctx_len = (usize)(end - (u8 *)ctx); \ alc_inc = yyjson_max(alc_len / 2, ext_len); \ alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \ if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ @@ -9060,11 +9071,10 @@ static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root, alc_len += alc_inc; \ tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ - ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ - memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ + memmove((void *)ctx_tmp, (void *)(tmp + ctx_pos), ctx_len); \ ctx = ctx_tmp; \ - cur = tmp + (cur - hdr); \ + cur = tmp + cur_pos; \ end = tmp + alc_len; \ hdr = tmp; \ } \ @@ -9243,6 +9253,9 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root, #define incr_len(_len) do { \ ext_len = (usize)(_len); \ if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ + usize ctx_pos = (usize)((u8 *)ctx - hdr); \ + usize cur_pos = (usize)(cur - hdr); \ + ctx_len = (usize)(end - (u8 *)ctx); \ alc_inc = yyjson_max(alc_len / 2, ext_len); \ alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \ if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ @@ -9250,11 +9263,10 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root, alc_len += alc_inc; \ tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ - ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ - memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ + memmove((void *)ctx_tmp, (void *)(tmp + ctx_pos), ctx_len); \ ctx = ctx_tmp; \ - cur = tmp + (cur - hdr); \ + cur = tmp + cur_pos; \ end = tmp + alc_len; \ hdr = tmp; \ } \ diff --git a/src/yyjson.h b/src/yyjson.h index 88cec2b..3941ddd 100644 --- a/src/yyjson.h +++ b/src/yyjson.h @@ -689,6 +689,30 @@ yyjson_api void yyjson_alc_dyn_free(yyjson_alc *alc); +/*============================================================================== + * Text Locating + *============================================================================*/ + +/** + Locate the line and column number for a byte position in a string. + This can be used to get better description for error position. + + @param str The input string. + @param len The byte length of the input string. + @param pos The byte position within the input string. + @param line A pointer to receive the line number, starting from 1. + @param col A pointer to receive the column number, starting from 1. + @param chr A pointer to receive the character index, starting from 0. + @return true on success, false if `str` is NULL or `pos` is out of bounds. + @note Line/column/character are calculated based on Unicode characters for + compatibility with text editors. For multi-byte UTF-8 characters, + the returned value may not directly correspond to the byte position. + */ +yyjson_api bool yyjson_locate_pos(const char *str, size_t len, size_t pos, + size_t *line, size_t *col, size_t *chr); + + + /*============================================================================== * JSON Structure *============================================================================*/ @@ -841,25 +865,9 @@ typedef struct yyjson_read_err { size_t pos; } yyjson_read_err; -/** - Locate the line and column number for a byte position in a string. - This can be used to get better description for error position. - - @param str The input string. - @param len The byte length of the input string. - @param pos The byte position within the input string. - @param line A pointer to receive the line number, starting from 1. - @param col A pointer to receive the column number, starting from 1. - @param chr A pointer to receive the character index, starting from 0. - @return true on success, false if `str` is NULL or `pos` is out of bounds. - @note Line/column/character are calculated based on Unicode characters for - compatibility with text editors. For multi-byte UTF-8 characters, - the returned value may not directly correspond to the byte position. - */ -yyjson_api bool yyjson_locate_pos(const char *str, size_t len, size_t pos, - size_t *line, size_t *col, size_t *chr); +#if !defined(YYJSON_DISABLE_READER) || !YYJSON_DISABLE_READER /** Read JSON with options. @@ -1072,6 +1080,9 @@ yyjson_api_inline const char *yyjson_mut_read_number(const char *dat, return yyjson_read_number(dat, (yyjson_val *)val, flg, alc, err); } +#endif /* YYJSON_DISABLE_READER) */ + + /*============================================================================== * JSON Writer API @@ -1157,6 +1168,8 @@ typedef struct yyjson_write_err { +#if !defined(YYJSON_DISABLE_WRITER) || !YYJSON_DISABLE_WRITER + /*============================================================================== * JSON Document Writer API *============================================================================*/ @@ -1571,6 +1584,8 @@ yyjson_api_inline char *yyjson_mut_val_write(const yyjson_mut_val *val, return yyjson_mut_val_write_opts(val, flg, NULL, len, NULL); } +#endif /* YYJSON_DISABLE_WRITER */ + /*============================================================================== @@ -3810,6 +3825,8 @@ yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, +#if !defined(YYJSON_DISABLE_UTILS) || !YYJSON_DISABLE_UTILS + /*============================================================================== * JSON Pointer API (RFC 6901) * https://tools.ietf.org/html/rfc6901 @@ -4492,6 +4509,8 @@ yyjson_api yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc, yyjson_mut_val *orig, yyjson_mut_val *patch); +#endif /* YYJSON_DISABLE_UTILS */ + /*============================================================================== @@ -7077,6 +7096,8 @@ yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, +#if !defined(YYJSON_DISABLE_UTILS) || !YYJSON_DISABLE_UTILS + /*============================================================================== * JSON Pointer API (Implementation) *============================================================================*/ @@ -7919,6 +7940,8 @@ yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_get_pointer( return unsafe_yyjson_mut_ptr_getx(val, ptr, len, NULL, &err); } +#endif /* YYJSON_DISABLE_UTILS */ + /*==============================================================================