Skip to content

Commit 1b563e8

Browse files
authored
Merge pull request #1870 from ruby/c99
Make c99, c23 compatible
2 parents 64a96b1 + 88a72b8 commit 1b563e8

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

ext/rbs_extension/location.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i)))
44
#define RBS_LOC_OPTIONAL_P(loc, i) (!RBS_LOC_REQUIRED_P((loc), (i)))
5+
#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
56

67
VALUE RBS_Location;
78

@@ -25,7 +26,7 @@ static void check_children_max(unsigned short n) {
2526
void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap) {
2627
check_children_max(cap);
2728

28-
size_t s = sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * cap;
29+
size_t s = RBS_LOC_CHILDREN_SIZE(cap);
2930
loc->children = malloc(s);
3031

3132
loc->children->len = 0;
@@ -39,7 +40,7 @@ static void check_children_cap(rbs_loc *loc) {
3940
} else {
4041
if (loc->children->len == loc->children->cap) {
4142
check_children_max(loc->children->cap + 1);
42-
size_t s = sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * (++loc->children->cap);
43+
size_t s = RBS_LOC_CHILDREN_SIZE(++loc->children->cap);
4344
loc->children = realloc(loc->children, s);
4445
}
4546
}
@@ -85,7 +86,7 @@ static size_t rbs_loc_memsize(const void *ptr) {
8586
if (loc->children == NULL) {
8687
return sizeof(rbs_loc);
8788
} else {
88-
return sizeof(rbs_loc) + sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * loc->children->cap;
89+
return sizeof(rbs_loc) + RBS_LOC_CHILDREN_SIZE(loc->children->cap);
8990
}
9091
}
9192

@@ -129,7 +130,7 @@ static VALUE location_initialize_copy(VALUE self, VALUE other) {
129130
self_loc->rg = other_loc->rg;
130131
if (other_loc->children != NULL) {
131132
rbs_loc_alloc_children(self_loc, other_loc->children->cap);
132-
memcpy(self_loc->children, other_loc->children, sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * other_loc->children->cap);
133+
memcpy(self_loc->children, other_loc->children, RBS_LOC_CHILDREN_SIZE(other_loc->children->cap));
133134
}
134135

135136
return Qnil;

ext/rbs_extension/location.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ typedef struct {
1616

1717
typedef unsigned int rbs_loc_entry_bitmap;
1818

19+
// The flexible array always allocates, but it's okay.
20+
// This struct is not allocated when the `rbs_loc` doesn't have children.
1921
typedef struct {
2022
unsigned short len;
2123
unsigned short cap;
2224
rbs_loc_entry_bitmap required_p;
23-
rbs_loc_entry entries[0];
25+
rbs_loc_entry entries[1];
2426
} rbs_loc_children;
2527

2628
typedef struct {
2729
VALUE buffer;
2830
range rg;
29-
rbs_loc_children *children;
31+
rbs_loc_children *children; // NULL when no children is allocated
3032
} rbs_loc;
3133

3234
/**

0 commit comments

Comments
 (0)