@@ -14,6 +14,7 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
1414 secp256k1_scratch * ret = (secp256k1_scratch * )checked_malloc (error_callback , sizeof (* ret ));
1515 if (ret != NULL ) {
1616 memset (ret , 0 , sizeof (* ret ));
17+ memcpy (ret -> magic , "scratch" , 8 );
1718 ret -> data = (secp256k1_scratch * )checked_malloc (error_callback , max_size );
1819 ret -> max_size = max_size ;
1920 }
@@ -23,6 +24,10 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
2324static void secp256k1_scratch_destroy (secp256k1_scratch * scratch ) {
2425 if (scratch != NULL ) {
2526 VERIFY_CHECK (scratch -> frame == 0 );
27+ if (memcmp (scratch -> magic , "scratch" , 8 ) != 0 ) {
28+ return ;
29+ }
30+ memset (scratch -> magic , 0 , sizeof (scratch -> magic ));
2631 free (scratch -> data );
2732 free (scratch );
2833 }
@@ -31,6 +36,9 @@ static void secp256k1_scratch_destroy(secp256k1_scratch* scratch) {
3136static size_t secp256k1_scratch_max_allocation (const secp256k1_scratch * scratch , size_t objects ) {
3237 size_t i = 0 ;
3338 size_t allocated = 0 ;
39+ if (memcmp (scratch -> magic , "scratch" , 8 ) != 0 ) {
40+ return 0 ;
41+ }
3442 for (i = 0 ; i < scratch -> frame ; i ++ ) {
3543 allocated += scratch -> frame_size [i ];
3644 }
@@ -43,6 +51,10 @@ static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch* scratch,
4351static int secp256k1_scratch_allocate_frame (secp256k1_scratch * scratch , size_t n , size_t objects ) {
4452 VERIFY_CHECK (scratch -> frame < SECP256K1_SCRATCH_MAX_FRAMES );
4553
54+ if (memcmp (scratch -> magic , "scratch" , 8 ) != 0 ) {
55+ return 0 ;
56+ }
57+
4658 if (n <= secp256k1_scratch_max_allocation (scratch , objects )) {
4759 n += objects * ALIGNMENT ;
4860 scratch -> current_frame = scratch -> data ;
@@ -58,6 +70,11 @@ static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n
5870
5971static void secp256k1_scratch_deallocate_frame (secp256k1_scratch * scratch ) {
6072 VERIFY_CHECK (scratch -> frame > 0 );
73+
74+ if (memcmp (scratch -> magic , "scratch" , 8 ) != 0 ) {
75+ return ;
76+ }
77+
6178 scratch -> frame -- ;
6279 scratch -> data = (void * ) ((char * ) scratch -> data - scratch -> frame_size [scratch -> frame ]);
6380}
@@ -67,6 +84,10 @@ static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) {
6784 size_t frame = scratch -> frame - 1 ;
6885 size = ROUND_TO_ALIGN (size );
6986
87+ if (memcmp (scratch -> magic , "scratch" , 8 ) != 0 ) {
88+ return NULL ;
89+ }
90+
7091 if (scratch -> frame == 0 || size + scratch -> offset [frame ] > scratch -> frame_size [frame ]) {
7192 return NULL ;
7293 }
0 commit comments