Skip to content

Commit 2ca659e

Browse files
committed
reduce warnings, fix some 32 bit overflow concerns + handle 32bit array capa
1 parent 33e4283 commit 2ca659e

22 files changed

+291
-245
lines changed

fio-stl.h

+134-113
Large diffs are not rendered by default.

fio-stl.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -7069,7 +7069,7 @@ Returns the current, temporary, array capacity (it's dynamic).
70697069
#### `ARY_reserve`
70707070

70717071
```c
7072-
uint32_t ARY_reserve(ARY_s * ary, int32_t capa);
7072+
uint32_t ARY_reserve(ARY_s * ary, int64_t capa);
70737073
```
70747074

70757075
Reserves capacity for new members to be added to the array.
@@ -7094,7 +7094,7 @@ Always returns the destination array (`dest`).
70947094

70957095
```c
70967096
FIO_ARRAY_TYPE * ARY_set(ARY_s * ary,
7097-
int32_t index,
7097+
int64_t index,
70987098
FIO_ARRAY_TYPE data,
70997099
FIO_ARRAY_TYPE *old);
71007100
```
@@ -7110,7 +7110,7 @@ Returns a pointer to the new object, or NULL on error.
71107110
#### `ARY_get`
71117111

71127112
```c
7113-
FIO_ARRAY_TYPE ARY_get(ARY_s * ary, int32_t index);
7113+
FIO_ARRAY_TYPE ARY_get(ARY_s * ary, int64_t index);
71147114
```
71157115

71167116
Returns the value located at `index` (no copying is performed).
@@ -7122,16 +7122,18 @@ If `index` is negative, it will be counted from the end of the Array (-1 == last
71227122
#### `ARY_find`
71237123

71247124
```c
7125-
int32_t ARY_find(ARY_s * ary, FIO_ARRAY_TYPE data, int32_t start_at);
7125+
uint32_t ARY_find(ARY_s * ary, FIO_ARRAY_TYPE data, int64_t start_at);
7126+
/* When an object can't be founds, this is the returned value. */
7127+
#define FIO_ARRAY_NOT_FOUND ((uint32_t)-1)
71267128
```
71277129

7128-
Returns the index of the object or -1 if the object wasn't found.
7130+
Returns the index of the object or `FIO_ARRAY_NOT_FOUND` (`(uint32_t)-1`) if the object wasn't found.
71297131

71307132
If `start_at` is negative (i.e., -1), than seeking will be performed in reverse, where -1 == last index (-2 == second to last, etc').
71317133

71327134
#### `ARY_remove`
71337135
```c
7134-
int ARY_remove(ARY_s * ary, int32_t index, FIO_ARRAY_TYPE *old);
7136+
int ARY_remove(ARY_s * ary, int64_t index, FIO_ARRAY_TYPE *old);
71357137
```
71367138

71377139
Removes an object from the array, MOVING all the other objects to prevent "holes" in the data.
@@ -7217,7 +7219,7 @@ Returns -1 on error (Array is empty) and 0 on success.
72177219
uint32_t ARY_each(ARY_s * ary,
72187220
int (*task)(ARY_each_s * info),
72197221
void *arg,
7220-
int32_t start_at);
7222+
int64_t start_at);
72217223
```
72227224

72237225
Iteration using a callback for each entry in the array.
@@ -8526,7 +8528,7 @@ typedef struct {
85268528
/** Connection protocol (once connection established). */
85278529
fio_protocol_s *protocol;
85288530
/** Called in case of a failed connection, use for cleanup. */
8529-
void (*on_failed)(void *udata);
8531+
void (*on_failed)(fio_protocol_s *protocol, void *udata);
85308532
/** Opaque user data (set only once connection was established). */
85318533
void *udata;
85328534
/** TLS builder object for TLS connections. */
@@ -8541,7 +8543,7 @@ SFUNC fio_s *fio_srv_connect(fio_srv_connect_args_s args);
85418543

85428544
Connects to a remote URL (accepting TLS hints in the URL query and scheme). The protocol is only attached if the connection was established.
85438545

8544-
**Note**: use the `on_failed` callback if cleanup is required after a failed connection. The `on_close` callback is only called if connection was successful.
8546+
**Note**: use the `on_failed` callback if cleanup is required after a failed connection. The protocol's `on_close` callback is only called if connection was successful.
85458547

85468548
`fio_srv_connect` adds some overhead in parsing the URL for TLS hints and for wrapping the connection protocol for timeout and connection validation before calling the `on_attached`. If these aren't required, it's possible to simply open a socket and attach it like so:
85478549

fio-stl/000 core.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -1263,15 +1263,15 @@ UTF-8 Support (basic)
12631263
#endif
12641264

12651265
/* Returns the number of bytes required to UTF-8 encoded a code point `u` */
1266-
FIO_IFUNC size_t fio_utf8_code_len(uint32_t u) {
1266+
FIO_IFUNC unsigned fio_utf8_code_len(uint32_t u) {
12671267
uint32_t len = (1U + ((uint32_t)(u) > 127) + ((uint32_t)(u) > 2047) +
12681268
((uint32_t)(u) > 65535));
12691269
len &= (uint32_t)((uint32_t)(u) > ((1U << 21) - 1)) - 1;
12701270
return len;
12711271
}
12721272

12731273
/** Returns 1-4 (UTF-8 char length), 8 (middle of a char) or 0 (invalid). */
1274-
FIO_IFUNC size_t fio_utf8_char_len_unsafe(uint8_t c) {
1274+
FIO_IFUNC unsigned fio_utf8_char_len_unsafe(uint8_t c) {
12751275
/* Ruby script for map:
12761276
map = [];
12771277
32.times { | i |
@@ -1290,8 +1290,8 @@ FIO_IFUNC size_t fio_utf8_char_len_unsafe(uint8_t c) {
12901290
}
12911291

12921292
/** Returns the number of valid UTF-8 bytes used by first char at `str`. */
1293-
FIO_IFUNC size_t fio_utf8_char_len(const void *str_) {
1294-
size_t r, tst;
1293+
FIO_IFUNC unsigned fio_utf8_char_len(const void *str_) {
1294+
unsigned r, tst;
12951295
const uint8_t *s = (uint8_t *)str_;
12961296
r = fio_utf8_char_len_unsafe(*s) & 7;
12971297
#if FIO_UTF8_ALLOW_IF
@@ -1315,7 +1315,7 @@ FIO_IFUNC size_t fio_utf8_char_len(const void *str_) {
13151315
}
13161316

13171317
/** Writes code point to `dest` using UFT-8. Returns number of bytes written. */
1318-
FIO_IFUNC size_t fio_utf8_write(void *dest_, uint32_t u) {
1318+
FIO_IFUNC unsigned fio_utf8_write(void *dest_, uint32_t u) {
13191319
const uint8_t len = fio_utf8_code_len(u);
13201320
uint8_t *dest = (uint8_t *)dest_;
13211321
#if FIO_UTF8_ALLOW_IF
@@ -1892,8 +1892,8 @@ typedef struct fio_index8_node_s {
18921892
register const size_t n__ = (i); \
18931893
(root)[n__].node_name.prev = (root)[(head)].node_name.prev; \
18941894
(root)[n__].node_name.next = (head); \
1895-
(root)[(root)[(head)].node_name.prev].node_name.next = n__; \
1896-
(root)[(head)].node_name.prev = n__; \
1895+
(root)[(root)[(head)].node_name.prev].node_name.next = (n__); \
1896+
(root)[(head)].node_name.prev = (n__); \
18971897
} while (0)
18981898

18991899
/** UNSAFE macro for adding a node to the begging of the list. */
@@ -1902,9 +1902,9 @@ typedef struct fio_index8_node_s {
19021902
register const size_t n__ = (i); \
19031903
(root)[n__].node_name.next = (root)[(head)].node_name.next; \
19041904
(root)[n__].node_name.prev = (head); \
1905-
(root)[(root)[(head)].node_name.next].node_name.prev = n__; \
1906-
(root)[(head)].node_name.next = n__; \
1907-
(head) = n__; \
1905+
(root)[(root)[(head)].node_name.next].node_name.prev = (n__); \
1906+
(root)[(head)].node_name.next = (n__); \
1907+
(head) = (n__); \
19081908
} while (0)
19091909

19101910
/** UNSAFE macro for removing a node from a list. */
@@ -1925,7 +1925,7 @@ typedef struct fio_index8_node_s {
19251925
(root)[n__].node_name.next; \
19261926
(root)[(root)[n__].node_name.next].node_name.prev = \
19271927
(root)[n__].node_name.prev; \
1928-
(root)[n__].node_name.next = (root)[n__].node_name.prev = n__; \
1928+
(root)[n__].node_name.next = (root)[n__].node_name.prev = (n__); \
19291929
} while (0)
19301930

19311931
/** Loops through every index in the indexed list, assuming `head` is valid. */

fio-stl/004 sock.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ FIO_IFUNC int fio_sock_dup(int original) {
9090
#define FIO_SOCK_FD_ISVALID(fd) ((int)fd != (int)-1)
9191
#endif
9292
/** Acts as POSIX write. Use this macro for portability with WinSock2. */
93-
#define fio_sock_write(fd, data, len) write((fd), (data), (len))
93+
#define fio_sock_write(fd, data, len) write((fd), (data), (len))
9494
/** Acts as POSIX read. Use this macro for portability with WinSock2. */
95-
#define fio_sock_read(fd, buf, len) read((fd), (buf), (len))
95+
#define fio_sock_read(fd, buf, len) read((fd), (buf), (len))
9696
/** Acts as POSIX dup. Use this macro for portability with WinSock2. */
97-
#define fio_sock_dup(fd) dup(fd)
97+
#define fio_sock_dup(fd) dup(fd)
9898
/** Acts as POSIX close. Use this macro for portability with WinSock2. */
99-
#define fio_sock_close(fd) close(fd)
99+
#define fio_sock_close(fd) close(fd)
100+
/** Acts as POSIX accept. Use this macro for portability with WinSock2. */
101+
#define fio_sock_accept(fd, addr, addrlen) accept(fd, addr, addrlen)
100102
#else
101103
#error FIO_SOCK requires a supported OS (Windows / POSIX).
102104
#endif

fio-stl/005 cli.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,19 @@ FIO_SFUNC uint32_t fio___cli_ary_new_index(fio___cli_ary_s *a) {
300300
if (!a->ary)
301301
FIO_LEAK_COUNTER_ON_ALLOC(fio_cli_ary);
302302
size_t new_capa = a->capa + 8;
303+
FIO_ASSERT(new_capa < 0xFFFFFFFFU, "fio_cli data overflow");
303304
fio_cli_str_s *tmp =
304305
(fio_cli_str_s *)FIO_MEM_REALLOC_(a->ary,
305306
sizeof(*a->ary) * a->capa,
306307
sizeof(*a->ary) * new_capa,
307308
a->capa);
308309
FIO_ASSERT_ALLOC(tmp);
309310
a->ary = tmp;
310-
a->capa = new_capa;
311+
a->capa = (uint32_t)new_capa;
311312
if (!(FIO_MEM_REALLOC_IS_SAFE_))
312313
FIO_MEMSET(a->ary + a->w, 0, sizeof(*a->ary) * (new_capa - a->w));
313314
}
314-
FIO_ASSERT_DEBUG(a->w < a->capa, "CLI array index error!");
315+
FIO_ASSERT_DEBUG(a->w < (uint32_t)a->capa, "CLI array index error!");
315316
return a->w++;
316317
}
317318

fio-stl/102 stream.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ SFUNC fio_stream_packet_s *fio_stream_pack_data(void *buf,
325325
tmp->next = p;
326326
em = (fio_stream_packet_embd_s *)(tmp + 1);
327327
em->type = FIO_PACKET_TYPE_EMBEDDED;
328-
em->length = slice;
328+
em->length = (uint32_t)slice;
329329
FIO_MEMCPY(em->buf, (char *)buf + offset + (len - slice), slice);
330330
p = tmp;
331331
len -= slice;

fio-stl/102 string core.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ FIO_SFUNC fio_keystr_s fio_keystr_init(fio_str_info_s str,
10621062
return r;
10631063
no_mem:
10641064
FIO_LOG_FATAL("fio_keystr_init allocation failed - results undefined!!!");
1065-
r = fio_keystr_tmp(str.buf, str.len);
1065+
r = fio_keystr_tmp(str.buf, (uint32_t)str.len);
10661066
return r;
10671067
}
10681068
/** Destroys a copy of `fio_keystr_s` - used internally by the hash map. */
@@ -1389,7 +1389,7 @@ fio_string_is_greater
13891389
* Note: returns 0 if data in b is bigger than or equal(!).
13901390
*/
13911391
SFUNC int fio_string_is_greater_buf(fio_buf_info_s a, fio_buf_info_s b) {
1392-
const size_t a_len_is_bigger = a.len > b.len;
1392+
const int a_len_is_bigger = a.len > b.len;
13931393
size_t len = a_len_is_bigger ? b.len : a.len; /* shared length */
13941394
if (a.buf == b.buf)
13951395
return a_len_is_bigger;
@@ -2509,7 +2509,7 @@ SFUNC int fio_string_write_html_unescape(fio_str_info_s *dest,
25092509
continue;
25102510
del += (*del == ';');
25112511
reduced -= (del - tmp);
2512-
reduced += fio_utf8_code_len(num);
2512+
reduced += fio_utf8_code_len((uint32_t)num);
25132513
continue;
25142514
}
25152515
union {
@@ -2561,7 +2561,8 @@ SFUNC int fio_string_write_html_unescape(fio_str_info_s *dest,
25612561
(del[-1] == 'x' ? fio_atol16u : fio_atol10u)((char **)&del);
25622562
if (*del != ';' || num > 65535)
25632563
goto untrusted_no_encode;
2564-
dest->len += fio_utf8_write((uint8_t *)dest->buf + dest->len, num);
2564+
dest->len +=
2565+
fio_utf8_write((uint8_t *)dest->buf + dest->len, (uint32_t)num);
25652566
del += (del < end && del[0] == ';');
25662567
continue;
25672568
}
@@ -2743,7 +2744,7 @@ SFUNC int fio_bstr_reallocate(fio_str_info_s *dest, size_t len) {
27432744
fio___bstr_meta_s *bstr_m = NULL;
27442745
size_t new_capa = fio_string_capa4len(len + sizeof(bstr_m[0]));
27452746
if (FIO_UNLIKELY(new_capa > (size_t)0xFFFFFFFFULL))
2746-
new_capa = (size_t)0xFFFFFFFFULL + sizeof(bstr_m[0]);
2747+
new_capa = (size_t)0x0FFFFFFFFULL + sizeof(bstr_m[0]);
27472748
if (dest->capa < fio_string_capa4len(sizeof(bstr_m[0])) - 1)
27482749
goto copy_the_string;
27492750
bstr_m = (fio___bstr_meta_s *)FIO_MEM_REALLOC_(
@@ -2755,7 +2756,8 @@ SFUNC int fio_bstr_reallocate(fio_str_info_s *dest, size_t len) {
27552756
return -1;
27562757
update_metadata:
27572758
dest->buf = (char *)(bstr_m + 1);
2758-
bstr_m->capa = dest->capa = new_capa - sizeof(bstr_m[0]);
2759+
dest->capa = new_capa - sizeof(bstr_m[0]);
2760+
bstr_m->capa = (uint32_t)dest->capa;
27592761
return 0;
27602762

27612763
copy_the_string:
@@ -2767,7 +2769,7 @@ SFUNC int fio_bstr_reallocate(fio_str_info_s *dest, size_t len) {
27672769
FIO_LEAK_COUNTER_ON_ALLOC(fio_bstr_s);
27682770
if (dest->len) {
27692771
FIO_MEMCPY((bstr_m + 1), dest->buf, dest->len + 1);
2770-
bstr_m->len = dest->len;
2772+
bstr_m->len = (uint32_t)dest->len;
27712773
}
27722774
if (dest->capa)
27732775
fio_bstr_free(dest->buf);

fio-stl/104 mustache.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ FIO_IFUNC int fio___mustache_parse_section_end(fio___mustache_parser_s *p,
711711
if (!FIO_BUF_INFO_IS_EQ(old_var_name, var))
712712
goto value_name_mismatch;
713713

714-
fio_u2buf32u(prev + 3, fio_bstr_len(p->root) + 1);
714+
fio_u2buf32u(prev + 3, (uint32_t)(fio_bstr_len(p->root) + 1));
715715
fio___mustache_stand_alone_skip_eol(p);
716716

717717
#if FIO_MUSTACHE_LAMBDA_SUPPORT
@@ -838,7 +838,7 @@ FIO_IFUNC int fio___mustache_parse_partial(fio___mustache_parser_s *p,
838838
int r = fio___mustache_parse_template_file(&new_section);
839839
buf.u8[0] = FIO___MUSTACHE_I_STACK_POP;
840840
p->root = fio_bstr_write(new_section.root, buf.u8, 1);
841-
fio_u2buf32u(p->root + ipos, fio_bstr_len(p->root));
841+
fio_u2buf32u(p->root + ipos, (uint32_t)fio_bstr_len(p->root));
842842
fio___mustache_free_template(p, file_content);
843843
return r;
844844
}

0 commit comments

Comments
 (0)