Changes: Added early bounds checks for nodes_len/roots_len against UINT32_MAX, plus safe guards for order allocation and roots_len * 8 overflow. pel_program_dag.c New tests for oversized counts: test_pel_program_dag_encode.c Wired the new test into CMake: CMakeLists.txt
61 lines
1.3 KiB
C
61 lines
1.3 KiB
C
#include "amduat/enc/pel_program_dag.h"
|
|
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
static int test_encode_nodes_count_overflow(void) {
|
|
amduat_pel_program_t program;
|
|
amduat_octets_t encoded = amduat_octets(NULL, 0);
|
|
|
|
if (SIZE_MAX <= UINT32_MAX) {
|
|
return 0;
|
|
}
|
|
|
|
memset(&program, 0, sizeof(program));
|
|
program.nodes_len = (size_t)UINT32_MAX + 1u;
|
|
program.roots_len = 0;
|
|
|
|
if (amduat_enc_pel_program_dag_encode_v1(&program, &encoded)) {
|
|
fprintf(stderr, "expected encode failure for nodes_len overflow\n");
|
|
amduat_octets_free(&encoded);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int test_encode_roots_len_overflow(void) {
|
|
amduat_pel_program_t program;
|
|
amduat_octets_t encoded = amduat_octets(NULL, 0);
|
|
size_t roots_len;
|
|
|
|
if (SIZE_MAX / 8 == 0) {
|
|
return 0;
|
|
}
|
|
|
|
roots_len = (SIZE_MAX / 8) + 1u;
|
|
|
|
memset(&program, 0, sizeof(program));
|
|
program.nodes_len = 0;
|
|
program.roots_len = roots_len;
|
|
|
|
if (amduat_enc_pel_program_dag_encode_v1(&program, &encoded)) {
|
|
fprintf(stderr, "expected encode failure for roots_len overflow\n");
|
|
amduat_octets_free(&encoded);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(void) {
|
|
if (test_encode_nodes_count_overflow() != 0) {
|
|
return 1;
|
|
}
|
|
if (test_encode_roots_len_overflow() != 0) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|