@@ -79,7 +79,7 @@ __attribute__((packed))
79
79
typedef struct {
80
80
Py_uhash_t hash ;
81
81
/* Number of frames stored */
82
- int nframe ;
82
+ uint16_t nframe ;
83
83
/* Total number of frames the traceback had */
84
84
uint16_t total_nframe ;
85
85
frame_t frames [1 ];
@@ -88,8 +88,10 @@ typedef struct {
88
88
#define TRACEBACK_SIZE (NFRAME ) \
89
89
(sizeof(traceback_t) + sizeof(frame_t) * (NFRAME - 1))
90
90
91
- #define MAX_NFRAME \
92
- ((INT_MAX - (int)sizeof(traceback_t)) / (int)sizeof(frame_t) + 1)
91
+ /* The maximum number of frames is either:
92
+ - The maximum number of frames we can store in `traceback_t.nframe`
93
+ - The maximum memory size_t we can allocate */
94
+ static const unsigned long MAX_NFRAME = Py_MIN (UINT16_MAX , ((SIZE_MAX - sizeof (traceback_t )) / sizeof (frame_t ) + 1 ));
93
95
94
96
95
97
static PyObject * unknown_filename = NULL ;
@@ -1057,10 +1059,10 @@ tracemalloc_start(int max_nframe)
1057
1059
PyMemAllocatorEx alloc ;
1058
1060
size_t size ;
1059
1061
1060
- if (max_nframe < 1 || max_nframe > MAX_NFRAME ) {
1062
+ if (max_nframe < 1 || ( unsigned long ) max_nframe > MAX_NFRAME ) {
1061
1063
PyErr_Format (PyExc_ValueError ,
1062
- "the number of frames must be in range [1; %i ]" ,
1063
- ( int ) MAX_NFRAME );
1064
+ "the number of frames must be in range [1; %lu ]" ,
1065
+ MAX_NFRAME );
1064
1066
return -1 ;
1065
1067
}
1066
1068
@@ -1073,7 +1075,6 @@ tracemalloc_start(int max_nframe)
1073
1075
return 0 ;
1074
1076
}
1075
1077
1076
- assert (1 <= max_nframe && max_nframe <= MAX_NFRAME );
1077
1078
_Py_tracemalloc_config .max_nframe = max_nframe ;
1078
1079
1079
1080
/* allocate a buffer to store a new traceback */
0 commit comments