Skip to content

Commit

Permalink
Use the C_ARRAY_LENGTH.
Browse files Browse the repository at this point in the history
Use the C_ARRAY_LENGTH instead of sizeof(c_array)/sizeof(c_array[0])
or sizeof(c_array)/sizeof(type_of_element_in_c_array), and C_ARRAY_END
for c_array + C_ARRAY_LENGTH(c_array).

While doing this found potential off-by-one error in sv.c:Perl_sv_magic:
how > C_ARRAY_LENGTH(PL_magic_data)
should probably have been
how >= C_ARRAY_LENGTH(PL_magic_data)
No tests fail, but this seems to be more of an internal sanity check.
  • Loading branch information
jhi authored and tsee committed May 28, 2014
1 parent 40b5a54 commit c3caa5c
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
5 changes: 2 additions & 3 deletions dump.c
Expand Up @@ -868,8 +868,7 @@ const struct op_private_by_op op_private_names[] = {
static bool
S_op_private_to_names(pTHX_ SV *tmpsv, U32 optype, U32 op_private) {
const struct op_private_by_op *start = op_private_names;
const struct op_private_by_op *const end
= op_private_names + C_ARRAY_LENGTH(op_private_names);
const struct op_private_by_op *const end = C_ARRAY_END(op_private_names);

/* This is a linear search, but no worse than the code that it replaced.
It's debugging code - size is more important than speed. */
Expand Down Expand Up @@ -1894,7 +1893,7 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
if (HvARRAY(sv) && usedkeys) {
/* Show distribution of HEs in the ARRAY */
int freq[200];
#define FREQ_MAX ((int)(sizeof freq / sizeof freq[0] - 1))
#define FREQ_MAX ((int)(C_ARRAY_LENGTH(freq) - 1))
int i;
int max = 0;
U32 pow2 = 2, keys = usedkeys;
Expand Down
7 changes: 6 additions & 1 deletion handy.h
Expand Up @@ -1936,8 +1936,13 @@ void Perl_mem_log_del_sv(const SV *sv, const char *filename, const int linenumbe
#define StructCopy(s,d,t) Copy(s,d,1,t)
#endif

/* C_ARRAY_LENGTH is the number of elements in the C array (so you
* want your zero-based indices to be less than but not equal to).
*
* C_ARRAY_END is one past the last: half-open/half-closed range,
* not last-inclusive range. */
#define C_ARRAY_LENGTH(a) (sizeof(a)/sizeof((a)[0]))
#define C_ARRAY_END(a) (a) + (sizeof(a)/sizeof((a)[0]))
#define C_ARRAY_END(a) ((a) + C_ARRAY_LENGTH(a))

#ifdef NEED_VA_COPY
# ifdef va_copy
Expand Down
2 changes: 1 addition & 1 deletion patchlevel.h
Expand Up @@ -144,7 +144,7 @@ static const char * const local_patches[] = {

/* Initial space prevents this variable from being inserted in config.sh */
# define LOCAL_PATCH_COUNT \
((int)(sizeof(local_patches)/sizeof(local_patches[0])-2))
((int)(C_ARRAY_LENGTH(local_patches)-2))

/* the old terms of reference, add them only when explicitly included */
#define PATCHLEVEL PERL_VERSION
Expand Down
2 changes: 1 addition & 1 deletion perl.c
Expand Up @@ -677,7 +677,7 @@ perl_destruct(pTHXx)
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = vec;
msg.msg_iovlen = sizeof(vec)/sizeof(vec[0]);
msg.msg_iovlen = C_ARRAY_LENGTH(vec);

vec[0].iov_base = (void*)⌖
vec[0].iov_len = sizeof(target);
Expand Down
5 changes: 2 additions & 3 deletions sv.c
Expand Up @@ -5574,7 +5574,7 @@ Perl_sv_magic(pTHX_ SV *const sv, SV *const obj, const int how,

PERL_ARGS_ASSERT_SV_MAGIC;

if (how < 0 || (unsigned)how > C_ARRAY_LENGTH(PL_magic_data)
if (how < 0 || (unsigned)how >= C_ARRAY_LENGTH(PL_magic_data)
|| ((flags = PL_magic_data[how]),
(vtable_index = flags & PERL_MAGIC_VTABLE_MASK)
> magic_vtable_max))
Expand Down Expand Up @@ -12248,8 +12248,7 @@ Perl_ptr_table_store(pTHX_ PTR_TBL_t *const tbl, const void *const oldsv, void *
new_arena->next = tbl->tbl_arena;
tbl->tbl_arena = new_arena;
tbl->tbl_arena_next = new_arena->array;
tbl->tbl_arena_end = new_arena->array
+ sizeof(new_arena->array) / sizeof(new_arena->array[0]);
tbl->tbl_arena_end = C_ARRAY_END(new_arena->array);
}

tblent = tbl->tbl_arena_next++;
Expand Down
3 changes: 1 addition & 2 deletions universal.c
Expand Up @@ -1059,8 +1059,7 @@ Perl_boot_core_UNIVERSAL(pTHX)
dVAR;
static const char file[] = __FILE__;
const struct xsub_details *xsub = details;
const struct xsub_details *end
= details + sizeof(details) / sizeof(details[0]);
const struct xsub_details *end = C_ARRAY_END(details);

do {
newXS_flags(xsub->name, xsub->xsub, file, xsub->proto, 0);
Expand Down
4 changes: 2 additions & 2 deletions util.c
Expand Up @@ -4576,8 +4576,8 @@ Perl_init_global_struct(pTHX)
{
struct perl_vars *plvarsp = NULL;
# ifdef PERL_GLOBAL_STRUCT
const IV nppaddr = sizeof(Gppaddr)/sizeof(Perl_ppaddr_t);
const IV ncheck = sizeof(Gcheck) /sizeof(Perl_check_t);
const IV nppaddr = C_ARRAY_LENGTH(Gppaddr);
const IV ncheck = C_ARRAY_LENGTH(Gcheck);
# ifdef PERL_GLOBAL_STRUCT_PRIVATE
/* PerlMem_malloc() because can't use even safesysmalloc() this early. */
plvarsp = (struct perl_vars*)PerlMem_malloc(sizeof(struct perl_vars));
Expand Down

0 comments on commit c3caa5c

Please sign in to comment.