Skip to content

Commit 9cc0eeb

Browse files
iabynap
authored andcommitted
MY_CXT: don't SEGV on a 1-byte struct.
The MY_CXT mechanism allows XS code to declare a 'static' struct which is actually per-interpreter. Behind the scenes, the memory for this struct is allocated as the PVX buf of an SV. If the size of the struct is 1 (e.g. '{ char foo }' ) then newSV(size-1) gets called as newSV(0), which skips allocating a PVX buffer. SEGVs ensue.
1 parent 65dea24 commit 9cc0eeb

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

util.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5491,8 +5491,9 @@ Perl_my_cxt_init(pTHX_ int *indexp, size_t size)
54915491
Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
54925492
}
54935493
}
5494-
/* newSV() allocates one more than needed */
5495-
p = (void*)SvPVX(newSV(size-1));
5494+
/* newSV() allocates one more than needed, hence the size-1.
5495+
* But if its arg is zero, it doesn't allocate a PVX at all. */
5496+
p = (void*)SvPVX(newSV(size > 1 ? size-1 : 1));
54965497
PL_my_cxt_list[index] = p;
54975498
Zero(p, size, char);
54985499
return p;

0 commit comments

Comments
 (0)