Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Parser/tokenizer/source.c
Line
Count
Source
1
#include "Python.h"
2
#include "pycore_pymem.h"  // PYMEM_DEADBYTE
3
4
#include "source.h"
5
6
276
#define SOURCE_BUFFER_GROWTH_FACTOR 2
7
8
void
9
_PyTok_SourceInit(_PyTok_SourceText *source)
10
135k
{
11
135k
    *source = (_PyTok_SourceText){0};
12
135k
}
13
14
void
15
_PyTok_SourceClear(_PyTok_SourceText *source)
16
67.6k
{
17
67.6k
    PyMem_Free(source->bytes);
18
67.6k
    _PyTok_SourceInit(source);
19
67.6k
}
20
21
void
22
_PyTok_SourceDiscard(_PyTok_SourceText *source)
23
316
{
24
316
    assert(source->base_offset <= PY_SSIZE_T_MAX - source->len);
25
316
    source->base_offset += source->len;
26
316
    source->len = 0;
27
316
    if (source->bytes != NULL) {
28
300
        source->bytes[0] = '\0';
29
300
    }
30
316
    source->nlines = 0;
31
316
}
32
33
static int
34
reserve_bytes(_PyTok_SourceText *source, Py_ssize_t needed)
35
268k
{
36
268k
    if (needed <= source->cap) {
37
202k
        return 0;
38
202k
    }
39
65.5k
    Py_ssize_t cap = source->cap > 0 ? source->cap : BUFSIZ;
40
65.6k
    while (cap < needed) {
41
138
        if (cap > PY_SSIZE_T_MAX / SOURCE_BUFFER_GROWTH_FACTOR) {
42
0
            cap = needed;
43
0
            break;
44
0
        }
45
138
        cap *= SOURCE_BUFFER_GROWTH_FACTOR;
46
138
    }
47
65.5k
    char *bytes;
48
#ifdef Py_DEBUG
49
    /* Moving on growth makes stale interior pointers fail in debug builds. */
50
    bytes = PyMem_Malloc(cap);
51
    if (bytes != NULL && source->len > 0) {
52
        memcpy(bytes, source->bytes, source->len);
53
    }
54
#else
55
65.5k
    bytes = PyMem_Realloc(source->bytes, cap);
56
65.5k
#endif
57
65.5k
    if (bytes == NULL) {
58
0
        PyErr_NoMemory();
59
0
        return -1;
60
0
    }
61
#ifdef Py_DEBUG
62
    if (source->bytes != NULL) {
63
        memset(source->bytes, PYMEM_DEADBYTE, source->cap);
64
        PyMem_Free(source->bytes);
65
    }
66
#endif
67
65.5k
    source->bytes = bytes;
68
65.5k
    source->cap = cap;
69
65.5k
    return 0;
70
65.5k
}
71
72
static int
73
validate_line(const _PyTok_SourceText *source, const char *bytes,
74
              Py_ssize_t len, int append_newline)
75
268k
{
76
268k
    if (len < 0 || (len == 0 && !append_newline) || bytes == NULL ||
77
268k
            (source->nlines > 0 &&
78
202k
             (source->len == 0 || source->bytes[source->len - 1] != '\n'))) {
79
0
        PyErr_SetString(PyExc_SystemError, "invalid tokenizer source line");
80
0
        return -1;
81
0
    }
82
268k
    const char *newline = memchr(bytes, '\n', len);
83
268k
    if (newline != NULL && (append_newline || newline != bytes + len - 1)) {
84
0
        PyErr_SetString(PyExc_SystemError, "invalid tokenizer source line");
85
0
        return -1;
86
0
    }
87
268k
    if (source->nlines == INT_MAX ||
88
268k
            (source->nlines == INT_MAX - 1 && (newline != NULL || append_newline))) {
89
0
        PyErr_SetString(PyExc_OverflowError, "too many tokenizer source lines");
90
0
        return -1;
91
0
    }
92
268k
    return 0;
93
268k
}
94
95
static _PyTok_Off
96
append_line(_PyTok_SourceText *source, const char *bytes,
97
            Py_ssize_t len, int append_newline)
98
268k
{
99
268k
    if (validate_line(source, bytes, len, append_newline) < 0) {
100
0
        return -1;
101
0
    }
102
268k
    if (source->len > PY_SSIZE_T_MAX - len - append_newline - 1 ||
103
268k
            source->base_offset > PY_SSIZE_T_MAX - source->len - len - append_newline) {
104
0
        PyErr_NoMemory();
105
0
        return -1;
106
0
    }
107
268k
    if (reserve_bytes(source, source->len + len + append_newline + 1) < 0) {
108
0
        return -1;
109
0
    }
110
111
268k
    _PyTok_Off start = source->len;
112
268k
    memcpy(source->bytes + start, bytes, len);
113
268k
    source->len += len;
114
268k
    if (append_newline) {
115
80.8k
        source->bytes[source->len++] = '\n';
116
80.8k
    }
117
268k
    source->bytes[source->len] = '\0';
118
268k
    source->nlines++;
119
268k
    return source->base_offset + start;
120
268k
}
121
122
_PyTok_Off
123
_PyTok_SourceAppendLine(_PyTok_SourceText *source, const char *bytes,
124
                        Py_ssize_t len)
125
187k
{
126
187k
    return append_line(source, bytes, len, 0);
127
187k
}
128
129
_PyTok_Off
130
_PyTok_SourceAppendLineWithNewline(_PyTok_SourceText *source, const char *bytes,
131
                                   Py_ssize_t len)
132
80.8k
{
133
80.8k
    return append_line(source, bytes, len, 1);
134
80.8k
}
135
136
const char *
137
_PyTok_SourceLineView(const _PyTok_SourceText *source, Py_ssize_t lineno,
138
                      Py_ssize_t *len)
139
319
{
140
319
    assert(len != NULL);
141
319
    const char *line = _PyTok_SourceData(source);
142
319
    const char *end = line + source->len;
143
7.60k
    while (lineno > 1) {
144
7.28k
        const char *newline = memchr(line, '\n', end - line);
145
7.28k
        if (newline == NULL) {
146
0
            break;
147
0
        }
148
7.28k
        line = newline + 1;
149
7.28k
        lineno--;
150
7.28k
    }
151
319
    const char *newline = memchr(line, '\n', end - line);
152
319
    *len = (newline != NULL ? newline : end) - line;
153
319
    return line;
154
319
}