~ubuntu-branches/ubuntu/natty/python3.1/natty-security

« back to all changes in this revision

Viewing changes to Parser/node.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-07-06 16:52:42 UTC
  • mfrom: (1.2.1 upstream) (2.1.11 sid)
  • Revision ID: james.westby@ubuntu.com-20100706165242-2xv4i019r3et6c0j
Tags: 3.1.2+20100706-1ubuntu1
* Merge with Debian; remaining changes:
  - Regenerate the control file.
  - Add debian/patches/overwrite-semaphore-check for Lucid buildds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
node *
8
8
PyNode_New(int type)
9
9
{
10
 
        node *n = (node *) PyObject_MALLOC(1 * sizeof(node));
11
 
        if (n == NULL)
12
 
                return NULL;
13
 
        n->n_type = type;
14
 
        n->n_str = NULL;
15
 
        n->n_lineno = 0;
16
 
        n->n_nchildren = 0;
17
 
        n->n_child = NULL;
18
 
        return n;
 
10
    node *n = (node *) PyObject_MALLOC(1 * sizeof(node));
 
11
    if (n == NULL)
 
12
        return NULL;
 
13
    n->n_type = type;
 
14
    n->n_str = NULL;
 
15
    n->n_lineno = 0;
 
16
    n->n_nchildren = 0;
 
17
    n->n_child = NULL;
 
18
    return n;
19
19
}
20
20
 
21
21
/* See comments at XXXROUNDUP below.  Returns -1 on overflow. */
22
22
static int
23
23
fancy_roundup(int n)
24
24
{
25
 
        /* Round up to the closest power of 2 >= n. */
26
 
        int result = 256;
27
 
        assert(n > 128);
28
 
        while (result < n) {
29
 
                result <<= 1;
30
 
                if (result <= 0)
31
 
                        return -1;
32
 
        }
33
 
        return result;
 
25
    /* Round up to the closest power of 2 >= n. */
 
26
    int result = 256;
 
27
    assert(n > 128);
 
28
    while (result < n) {
 
29
        result <<= 1;
 
30
        if (result <= 0)
 
31
            return -1;
 
32
    }
 
33
    return result;
34
34
}
35
35
 
36
36
/* A gimmick to make massive numbers of reallocs quicker.  The result is
70
70
 * Note that this would be straightforward if a node stored its current
71
71
 * capacity.  The code is tricky to avoid that.
72
72
 */
73
 
#define XXXROUNDUP(n) ((n) <= 1 ? (n) :                 \
74
 
                       (n) <= 128 ? (((n) + 3) & ~3) :  \
75
 
                       fancy_roundup(n))
 
73
#define XXXROUNDUP(n) ((n) <= 1 ? (n) :                 \
 
74
               (n) <= 128 ? (((n) + 3) & ~3) :          \
 
75
               fancy_roundup(n))
76
76
 
77
77
 
78
78
int
79
79
PyNode_AddChild(register node *n1, int type, char *str, int lineno, int col_offset)
80
80
{
81
 
        const int nch = n1->n_nchildren;
82
 
        int current_capacity;
83
 
        int required_capacity;
84
 
        node *n;
85
 
 
86
 
        if (nch == INT_MAX || nch < 0)
87
 
                return E_OVERFLOW;
88
 
 
89
 
        current_capacity = XXXROUNDUP(nch);
90
 
        required_capacity = XXXROUNDUP(nch + 1);
91
 
        if (current_capacity < 0 || required_capacity < 0)
92
 
                return E_OVERFLOW;
93
 
        if (current_capacity < required_capacity) {
94
 
                if (required_capacity > PY_SIZE_MAX / sizeof(node)) {
95
 
                        return E_NOMEM;
96
 
                }
97
 
                n = n1->n_child;
98
 
                n = (node *) PyObject_REALLOC(n,
99
 
                                              required_capacity * sizeof(node));
100
 
                if (n == NULL)
101
 
                        return E_NOMEM;
102
 
                n1->n_child = n;
103
 
        }
104
 
 
105
 
        n = &n1->n_child[n1->n_nchildren++];
106
 
        n->n_type = type;
107
 
        n->n_str = str;
108
 
        n->n_lineno = lineno;
109
 
        n->n_col_offset = col_offset;
110
 
        n->n_nchildren = 0;
111
 
        n->n_child = NULL;
112
 
        return 0;
 
81
    const int nch = n1->n_nchildren;
 
82
    int current_capacity;
 
83
    int required_capacity;
 
84
    node *n;
 
85
 
 
86
    if (nch == INT_MAX || nch < 0)
 
87
        return E_OVERFLOW;
 
88
 
 
89
    current_capacity = XXXROUNDUP(nch);
 
90
    required_capacity = XXXROUNDUP(nch + 1);
 
91
    if (current_capacity < 0 || required_capacity < 0)
 
92
        return E_OVERFLOW;
 
93
    if (current_capacity < required_capacity) {
 
94
        if (required_capacity > PY_SIZE_MAX / sizeof(node)) {
 
95
            return E_NOMEM;
 
96
        }
 
97
        n = n1->n_child;
 
98
        n = (node *) PyObject_REALLOC(n,
 
99
                                      required_capacity * sizeof(node));
 
100
        if (n == NULL)
 
101
            return E_NOMEM;
 
102
        n1->n_child = n;
 
103
    }
 
104
 
 
105
    n = &n1->n_child[n1->n_nchildren++];
 
106
    n->n_type = type;
 
107
    n->n_str = str;
 
108
    n->n_lineno = lineno;
 
109
    n->n_col_offset = col_offset;
 
110
    n->n_nchildren = 0;
 
111
    n->n_child = NULL;
 
112
    return 0;
113
113
}
114
114
 
115
115
/* Forward */
119
119
void
120
120
PyNode_Free(node *n)
121
121
{
122
 
        if (n != NULL) {
123
 
                freechildren(n);
124
 
                PyObject_FREE(n);
125
 
        }
 
122
    if (n != NULL) {
 
123
        freechildren(n);
 
124
        PyObject_FREE(n);
 
125
    }
126
126
}
127
127
 
128
128
static void
129
129
freechildren(node *n)
130
130
{
131
 
        int i;
132
 
        for (i = NCH(n); --i >= 0; )
133
 
                freechildren(CHILD(n, i));
134
 
        if (n->n_child != NULL)
135
 
                PyObject_FREE(n->n_child);
136
 
        if (STR(n) != NULL)
137
 
                PyObject_FREE(STR(n));
 
131
    int i;
 
132
    for (i = NCH(n); --i >= 0; )
 
133
        freechildren(CHILD(n, i));
 
134
    if (n->n_child != NULL)
 
135
        PyObject_FREE(n->n_child);
 
136
    if (STR(n) != NULL)
 
137
        PyObject_FREE(STR(n));
138
138
}