Skip to content

Commit

Permalink
Fix warnings when directly including yyjson.c: #177
Browse files Browse the repository at this point in the history
  • Loading branch information
ibireme committed Jul 21, 2024
1 parent 4caf677 commit dcd0d7a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 33 deletions.
44 changes: 28 additions & 16 deletions src/yyjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
} \
Expand Down Expand Up @@ -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); \
} \
Expand Down Expand Up @@ -8501,18 +8505,20 @@ 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)) \
goto fail_alloc; \
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; \
} \
Expand Down Expand Up @@ -8685,18 +8691,20 @@ 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)) \
goto fail_alloc; \
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; \
} \
Expand Down Expand Up @@ -9053,18 +9061,20 @@ 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)) \
goto fail_alloc; \
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; \
} \
Expand Down Expand Up @@ -9243,18 +9253,20 @@ 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)) \
goto fail_alloc; \
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; \
} \
Expand Down
57 changes: 40 additions & 17 deletions src/yyjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*============================================================================*/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1157,6 +1168,8 @@ typedef struct yyjson_write_err {



#if !defined(YYJSON_DISABLE_WRITER) || !YYJSON_DISABLE_WRITER

/*==============================================================================
* JSON Document Writer API
*============================================================================*/
Expand Down Expand Up @@ -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 */



/*==============================================================================
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */



/*==============================================================================
Expand Down Expand Up @@ -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)
*============================================================================*/
Expand Down Expand Up @@ -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 */



/*==============================================================================
Expand Down

0 comments on commit dcd0d7a

Please sign in to comment.