@@ -14,6 +14,7 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
14
14
secp256k1_scratch * ret = (secp256k1_scratch * )checked_malloc (error_callback , sizeof (* ret ));
15
15
if (ret != NULL ) {
16
16
memset (ret , 0 , sizeof (* ret ));
17
+ memcpy (ret -> magic , "scratch" , 8 );
17
18
ret -> data = (secp256k1_scratch * )checked_malloc (error_callback , max_size );
18
19
ret -> max_size = max_size ;
19
20
}
@@ -23,6 +24,10 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
23
24
static void secp256k1_scratch_destroy (secp256k1_scratch * scratch ) {
24
25
if (scratch != NULL ) {
25
26
VERIFY_CHECK (scratch -> frame == 0 );
27
+ if (memcmp (scratch -> magic , "scratch" , 8 ) != 0 ) {
28
+ return ;
29
+ }
30
+ memset (scratch -> magic , 0 , sizeof (scratch -> magic ));
26
31
free (scratch -> data );
27
32
free (scratch );
28
33
}
@@ -31,6 +36,9 @@ static void secp256k1_scratch_destroy(secp256k1_scratch* scratch) {
31
36
static size_t secp256k1_scratch_max_allocation (const secp256k1_scratch * scratch , size_t objects ) {
32
37
size_t i = 0 ;
33
38
size_t allocated = 0 ;
39
+ if (memcmp (scratch -> magic , "scratch" , 8 ) != 0 ) {
40
+ return 0 ;
41
+ }
34
42
for (i = 0 ; i < scratch -> frame ; i ++ ) {
35
43
allocated += scratch -> frame_size [i ];
36
44
}
@@ -43,6 +51,10 @@ static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch* scratch,
43
51
static int secp256k1_scratch_allocate_frame (secp256k1_scratch * scratch , size_t n , size_t objects ) {
44
52
VERIFY_CHECK (scratch -> frame < SECP256K1_SCRATCH_MAX_FRAMES );
45
53
54
+ if (memcmp (scratch -> magic , "scratch" , 8 ) != 0 ) {
55
+ return 0 ;
56
+ }
57
+
46
58
if (n <= secp256k1_scratch_max_allocation (scratch , objects )) {
47
59
n += objects * ALIGNMENT ;
48
60
scratch -> current_frame = scratch -> data ;
@@ -58,6 +70,11 @@ static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n
58
70
59
71
static void secp256k1_scratch_deallocate_frame (secp256k1_scratch * scratch ) {
60
72
VERIFY_CHECK (scratch -> frame > 0 );
73
+
74
+ if (memcmp (scratch -> magic , "scratch" , 8 ) != 0 ) {
75
+ return ;
76
+ }
77
+
61
78
scratch -> frame -- ;
62
79
scratch -> data = (void * ) ((char * ) scratch -> data - scratch -> frame_size [scratch -> frame ]);
63
80
}
@@ -67,6 +84,10 @@ static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) {
67
84
size_t frame = scratch -> frame - 1 ;
68
85
size = ROUND_TO_ALIGN (size );
69
86
87
+ if (memcmp (scratch -> magic , "scratch" , 8 ) != 0 ) {
88
+ return NULL ;
89
+ }
90
+
70
91
if (scratch -> frame == 0 || size + scratch -> offset [frame ] > scratch -> frame_size [frame ]) {
71
92
return NULL ;
72
93
}
0 commit comments