Skip to content

Commit 77e7c92

Browse files
committed
Use sized 32/64 bit ints in several places
Also convert integers in bitwise and modulo operators to the same bit-width as PRJM_EVAL_F to prevent overflows.
1 parent 3e554a5 commit 77e7c92

File tree

5 files changed

+51
-49
lines changed

5 files changed

+51
-49
lines changed

projectm-eval/CompileContext.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
#include <string.h>
1111

1212
prjm_eval_compiler_context_t* prjm_eval_create_compile_context(projectm_eval_mem_buffer global_memory,
13-
PRJM_EVAL_F (* global_variables)[100])
13+
PRJM_EVAL_F (*global_variables)[100])
1414
{
1515
prjm_eval_compiler_context_t* cctx = calloc(1, sizeof(prjm_eval_compiler_context_t));
1616

1717
prjm_eval_intrinsic_function_list intrinsics;
18-
int intrinsics_count = 0;
18+
uint32_t intrinsics_count = 0;
1919
prjm_eval_intrinsic_functions(&intrinsics, &intrinsics_count);
2020

2121
assert(intrinsics);
2222
assert(intrinsics_count);
2323

2424
prjm_eval_function_list_item_t* last_func = NULL;
25-
for (int index = intrinsics_count - 1; index >= 0; --index)
25+
for (int32_t index = (int32_t)intrinsics_count - 1; index >= 0; --index)
2626
{
2727
assert(&intrinsics[index]);
2828
prjm_eval_function_list_item_t* func = malloc(sizeof(prjm_eval_function_list_item_t));
@@ -137,7 +137,8 @@ void prjm_eval_reset_context_vars(prjm_eval_compiler_context_t* cctx)
137137
}
138138
}
139139

140-
const char* prjm_eval_compiler_get_error(prjm_eval_compiler_context_t* cctx, int* line, int* column_start, int* column_end)
140+
const char* prjm_eval_compiler_get_error(prjm_eval_compiler_context_t* cctx, int* line, int* column_start,
141+
int* column_end)
141142
{
142143
assert(cctx);
143144

@@ -157,4 +158,4 @@ const char* prjm_eval_compiler_get_error(prjm_eval_compiler_context_t* cctx, int
157158
}
158159

159160
return cctx->error.error;
160-
}
161+
}

projectm-eval/MemoryBuffer.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "MemoryBuffer.h"
22

3+
#include <stdint.h>
34
#include <stdlib.h>
45
#include <string.h>
56

@@ -61,7 +62,7 @@ void prjm_eval_memory_free(projectm_eval_mem_buffer buffer)
6162
projectm_eval_memory_host_unlock_mutex();
6263
}
6364

64-
void prjm_eval_memory_free_block(projectm_eval_mem_buffer buffer, int block)
65+
void prjm_eval_memory_free_block(projectm_eval_mem_buffer buffer, int32_t block)
6566
{
6667
if (block < 0)
6768
{
@@ -75,7 +76,7 @@ void prjm_eval_memory_free_block(projectm_eval_mem_buffer buffer, int block)
7576
}
7677
}
7778

78-
PRJM_EVAL_F* prjm_eval_memory_allocate(projectm_eval_mem_buffer buffer, int index)
79+
PRJM_EVAL_F* prjm_eval_memory_allocate(projectm_eval_mem_buffer buffer, int32_t index)
7980
{
8081
int block;
8182
if (!buffer)
@@ -115,9 +116,9 @@ PRJM_EVAL_F* prjm_eval_memory_copy(projectm_eval_mem_buffer buffer,
115116
PRJM_EVAL_F* len)
116117
{
117118
// Add 0.0001 to avoid using the wrong index due to tiny float rounding errors.
118-
int offset_dest = (int) (*dest + 0.0001);
119-
int offset_src = (int) (*src + 0.0001);
120-
int count = (int) (*len + 0.0001);
119+
int32_t offset_dest = (int32_t) (*dest + 0.0001);
120+
int32_t offset_src = (int32_t) (*src + 0.0001);
121+
int32_t count = (int32_t) (*len + 0.0001);
121122

122123
// Trim to front if an offset is less than zero.
123124
if (offset_src < 0)
@@ -138,10 +139,10 @@ PRJM_EVAL_F* prjm_eval_memory_copy(projectm_eval_mem_buffer buffer,
138139

139140
while (count > 0)
140141
{
141-
int copy_length = count;
142+
int32_t copy_length = count;
142143

143-
int max_dst_len = PRJM_EVAL_MEM_ITEMSPERBLOCK - (offset_dest & (PRJM_EVAL_MEM_ITEMSPERBLOCK - 1));
144-
int max_src_len = PRJM_EVAL_MEM_ITEMSPERBLOCK - (offset_src & (PRJM_EVAL_MEM_ITEMSPERBLOCK - 1));
144+
int32_t max_dst_len = PRJM_EVAL_MEM_ITEMSPERBLOCK - (offset_dest & (PRJM_EVAL_MEM_ITEMSPERBLOCK - 1));
145+
int32_t max_src_len = PRJM_EVAL_MEM_ITEMSPERBLOCK - (offset_src & (PRJM_EVAL_MEM_ITEMSPERBLOCK - 1));
145146

146147
if (offset_dest >= PRJM_EVAL_MEM_BLOCKS * PRJM_EVAL_MEM_ITEMSPERBLOCK ||
147148
offset_src >= PRJM_EVAL_MEM_BLOCKS * PRJM_EVAL_MEM_ITEMSPERBLOCK)
@@ -185,8 +186,8 @@ PRJM_EVAL_F* prjm_eval_memory_set(projectm_eval_mem_buffer buffer,
185186
PRJM_EVAL_F* len)
186187
{
187188
// Add 0.0001 to avoid using the wrong index due to tiny float rounding errors.
188-
int offset_dest = (int) (*dest + 0.0001);
189-
int count = (int) (*len + 0.0001);
189+
int32_t offset_dest = (int32_t) (*dest + 0.0001);
190+
int32_t count = (int32_t) (*len + 0.0001);
190191

191192
// Trim to front
192193
if (offset_dest < 0)
@@ -219,7 +220,7 @@ PRJM_EVAL_F* prjm_eval_memory_set(projectm_eval_mem_buffer buffer,
219220
break;
220221
}
221222

222-
int block_count = PRJM_EVAL_MEM_ITEMSPERBLOCK - (offset_dest & (PRJM_EVAL_MEM_ITEMSPERBLOCK - 1));
223+
int32_t block_count = PRJM_EVAL_MEM_ITEMSPERBLOCK - (offset_dest & (PRJM_EVAL_MEM_ITEMSPERBLOCK - 1));
223224
if (block_count > count)
224225
{
225226
block_count = count;

projectm-eval/TreeFunctions.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#include <assert.h>
1111
#include <stdint.h>
1212

13+
#if PRJM_F_SIZE == 4
14+
typedef int32_t PRJM_EVAL_I;
15+
#else
16+
typedef int64_t PRJM_EVAL_I;
17+
#endif
1318
/**
1419
* @brief projectM-EvalLib intrinsic Function table.
1520
* Contains all predefined functions and information about their invocation. Most functions beginning
@@ -151,7 +156,7 @@ static const PRJM_EVAL_F close_factor_low = 1e-300;
151156
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
152157
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
153158

154-
void prjm_eval_intrinsic_functions(prjm_eval_intrinsic_function_list_ptr list, int* count)
159+
void prjm_eval_intrinsic_functions(prjm_eval_intrinsic_function_list_ptr list, uint32_t* count)
155160
{
156161
*count = sizeof(intrinsic_function_table) / sizeof(prjm_eval_function_def_t);
157162
*list = intrinsic_function_table;
@@ -265,14 +270,14 @@ prjm_eval_function_decl(execute_loop)
265270
PRJM_EVAL_F* value_ptr = &ctx->value;
266271
invoke_arg(0, &value_ptr);
267272

268-
int loop_count_int = (int) (*value_ptr);
273+
PRJM_EVAL_I loop_count_int = (PRJM_EVAL_I) (*value_ptr);
269274
/* Limit execution count */
270275
if (loop_count_int > MAX_LOOP_COUNT)
271276
{
272277
loop_count_int = MAX_LOOP_COUNT;
273278
}
274279

275-
for (int i = 0; i < loop_count_int; i++)
280+
for (PRJM_EVAL_I i = 0; i < loop_count_int; i++)
276281
{
277282
ctx->value = .0;
278283
value_ptr = &ctx->value;
@@ -288,7 +293,7 @@ prjm_eval_function_decl(execute_while)
288293

289294
ctx->value = .0;
290295
PRJM_EVAL_F* value_ptr = &ctx->value;
291-
int loop_count_int = MAX_LOOP_COUNT;
296+
PRJM_EVAL_I loop_count_int = MAX_LOOP_COUNT;
292297
do
293298
{
294299
invoke_arg(0, &value_ptr);
@@ -361,7 +366,7 @@ prjm_eval_function_decl(mem)
361366
invoke_arg(0, &index_ptr);
362367

363368
// Add 0.0001 to avoid using the wrong index due to tiny float rounding errors.
364-
PRJM_EVAL_F* mem_addr = prjm_eval_memory_allocate(ctx->memory_buffer, (int) (*index_ptr + 0.0001));
369+
PRJM_EVAL_F* mem_addr = prjm_eval_memory_allocate(ctx->memory_buffer, (int32_t) (*index_ptr + 0.0001));
365370
if (mem_addr)
366371
{
367372
assign_ret_ref(mem_addr);
@@ -379,7 +384,7 @@ prjm_eval_function_decl(freembuf)
379384
invoke_arg(0, ret_val);
380385

381386
// Add 0.0001 to avoid using the wrong index due to tiny float rounding errors.
382-
prjm_eval_memory_free_block(ctx->memory_buffer, (int) (**ret_val + 0.0001));
387+
prjm_eval_memory_free_block(ctx->memory_buffer, (int32_t) (**ret_val + 0.0001));
383388
}
384389

385390
prjm_eval_function_decl(memcpy)
@@ -601,13 +606,13 @@ prjm_eval_function_decl(mod)
601606
invoke_arg(0, &val1_ptr);
602607
invoke_arg(1, &val2_ptr);
603608

604-
int divisor = (int) *val2_ptr;
609+
PRJM_EVAL_I divisor = (PRJM_EVAL_I) *val2_ptr;
605610
if (divisor == 0)
606611
{
607612
assign_ret_val(0.0);
608613
return;
609614
}
610-
assign_ret_val((PRJM_EVAL_F) ((int) *val1_ptr % divisor));
615+
assign_ret_val((PRJM_EVAL_F) ((PRJM_EVAL_I) *val1_ptr % divisor));
611616
}
612617

613618
prjm_eval_function_decl(boolean_and_op)
@@ -776,7 +781,7 @@ prjm_eval_function_decl(bitwise_or_op)
776781
invoke_arg(0, ret_val);
777782
invoke_arg(1, &val2_ptr);
778783

779-
assign_ret_val((PRJM_EVAL_F) ((int)(**ret_val) | (int)(*val2_ptr)));
784+
assign_ret_val((PRJM_EVAL_F) ((PRJM_EVAL_I)(**ret_val) | (PRJM_EVAL_I)(*val2_ptr)));
780785
}
781786

782787
prjm_eval_function_decl(bitwise_or)
@@ -791,7 +796,7 @@ prjm_eval_function_decl(bitwise_or)
791796
invoke_arg(0, &val1_ptr);
792797
invoke_arg(1, &val2_ptr);
793798

794-
assign_ret_val((PRJM_EVAL_F) ((int)(*val1_ptr) | (int)(*val2_ptr)));
799+
assign_ret_val((PRJM_EVAL_F) ((PRJM_EVAL_I)(*val1_ptr) | (PRJM_EVAL_I)(*val2_ptr)));
795800
}
796801

797802
prjm_eval_function_decl(bitwise_and_op)
@@ -804,7 +809,7 @@ prjm_eval_function_decl(bitwise_and_op)
804809
invoke_arg(0, ret_val);
805810
invoke_arg(1, &val2_ptr);
806811

807-
assign_ret_val((PRJM_EVAL_F) ((int)(**ret_val) & (int)(*val2_ptr)));
812+
assign_ret_val((PRJM_EVAL_F) ((PRJM_EVAL_I)(**ret_val) & (PRJM_EVAL_I)(*val2_ptr)));
808813
}
809814

810815
prjm_eval_function_decl(bitwise_and)
@@ -819,7 +824,7 @@ prjm_eval_function_decl(bitwise_and)
819824
invoke_arg(0, &val1_ptr);
820825
invoke_arg(1, &val2_ptr);
821826

822-
assign_ret_val((PRJM_EVAL_F) ((int)(*val1_ptr) & (int)(*val2_ptr)));
827+
assign_ret_val((PRJM_EVAL_F) ((PRJM_EVAL_I)(*val1_ptr) & (PRJM_EVAL_I)(*val2_ptr)));
823828
}
824829

825830
prjm_eval_function_decl(mod_op)
@@ -832,13 +837,13 @@ prjm_eval_function_decl(mod_op)
832837
invoke_arg(0, ret_val);
833838
invoke_arg(1, &val2_ptr);
834839

835-
int divisor = (int) *val2_ptr;
840+
PRJM_EVAL_I divisor = (PRJM_EVAL_I) *val2_ptr;
836841
if (divisor == 0)
837842
{
838843
assign_ret_val(0.0);
839844
return;
840845
}
841-
assign_ret_val((PRJM_EVAL_F) ((int)(**ret_val) % divisor));
846+
assign_ret_val((PRJM_EVAL_F) ((PRJM_EVAL_I)(**ret_val) % divisor));
842847
}
843848

844849
prjm_eval_function_decl(pow_op)

projectm-eval/TreeFunctions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
#include "CompilerTypes.h"
88
#include "ExpressionTree.h"
99

10+
#include <stdint.h>
11+
1012
/**
1113
* @brief Returns the list with the built-in function table.
1214
* The list must not be freed, as it points to the internal static data.
1315
* @param list A pointer which will receive the list reference.
1416
* @param count The number of elements inserted into the list.
1517
*/
16-
void prjm_eval_intrinsic_functions(prjm_eval_intrinsic_function_list_ptr list, int* count);
18+
void prjm_eval_intrinsic_functions(prjm_eval_intrinsic_function_list_ptr list, uint32_t* count);
1719

1820
/**
1921
* @brief Abbreviates parser function declaration.

0 commit comments

Comments
 (0)