Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1dd8e71e
Commit
1dd8e71e
authored
May 02, 2015
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issues #24099, #24100, and #24101: Fix free-after-use bug in heapq.
parent
a7db0576
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
50 deletions
+24
-50
Misc/NEWS
Misc/NEWS
+3
-0
Modules/_heapqmodule.c
Modules/_heapqmodule.c
+21
-50
No files found.
Misc/NEWS
View file @
1dd8e71e
...
@@ -42,6 +42,9 @@ Library
...
@@ -42,6 +42,9 @@ Library
- Issue #9246: On POSIX, os.getcwd() now supports paths longer than 1025 bytes.
- Issue #9246: On POSIX, os.getcwd() now supports paths longer than 1025 bytes.
Patch written by William Orr.
Patch written by William Orr.
- Issues #24099, #24100, and #24101: Fix free-after-use bug in heapq'
s
siftup
and
siftdown
functions
.
-
Issue
#
23008
:
Fixed
resolving
attributes
with
boolean
value
is
False
in
pydoc
.
-
Issue
#
23008
:
Fixed
resolving
attributes
with
boolean
value
is
False
in
pydoc
.
-
Fix
asyncio
issue
235
:
LifoQueue
and
PriorityQueue
's put didn'
t
-
Fix
asyncio
issue
235
:
LifoQueue
and
PriorityQueue
's put didn'
t
...
...
Modules/_heapqmodule.c
View file @
1dd8e71e
...
@@ -11,10 +11,9 @@ annotated by François Pinard, and converted to C by Raymond Hettinger.
...
@@ -11,10 +11,9 @@ annotated by François Pinard, and converted to C by Raymond Hettinger.
static
int
static
int
_siftdown
(
PyListObject
*
heap
,
Py_ssize_t
startpos
,
Py_ssize_t
pos
)
_siftdown
(
PyListObject
*
heap
,
Py_ssize_t
startpos
,
Py_ssize_t
pos
)
{
{
PyObject
*
newitem
,
*
parent
,
*
olditem
;
PyObject
*
newitem
,
*
parent
;
Py_ssize_t
parentpos
,
size
;
int
cmp
;
int
cmp
;
Py_ssize_t
parentpos
;
Py_ssize_t
size
;
assert
(
PyList_Check
(
heap
));
assert
(
PyList_Check
(
heap
));
size
=
PyList_GET_SIZE
(
heap
);
size
=
PyList_GET_SIZE
(
heap
);
...
@@ -23,39 +22,28 @@ _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
...
@@ -23,39 +22,28 @@ _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
return
-
1
;
return
-
1
;
}
}
newitem
=
PyList_GET_ITEM
(
heap
,
pos
);
Py_INCREF
(
newitem
);
/* Follow the path to the root, moving parents down until finding
/* Follow the path to the root, moving parents down until finding
a place newitem fits. */
a place newitem fits. */
while
(
pos
>
startpos
){
newitem
=
PyList_GET_ITEM
(
heap
,
pos
);
while
(
pos
>
startpos
)
{
parentpos
=
(
pos
-
1
)
>>
1
;
parentpos
=
(
pos
-
1
)
>>
1
;
parent
=
PyList_GET_ITEM
(
heap
,
parentpos
);
parent
=
PyList_GET_ITEM
(
heap
,
parentpos
);
cmp
=
PyObject_RichCompareBool
(
newitem
,
parent
,
Py_LT
);
cmp
=
PyObject_RichCompareBool
(
newitem
,
parent
,
Py_LT
);
if
(
cmp
==
-
1
)
{
if
(
cmp
==
-
1
)
Py_DECREF
(
newitem
);
return
-
1
;
return
-
1
;
}
if
(
size
!=
PyList_GET_SIZE
(
heap
))
{
if
(
size
!=
PyList_GET_SIZE
(
heap
))
{
Py_DECREF
(
newitem
);
PyErr_SetString
(
PyExc_RuntimeError
,
PyErr_SetString
(
PyExc_RuntimeError
,
"list changed size during iteration"
);
"list changed size during iteration"
);
return
-
1
;
return
-
1
;
}
}
if
(
cmp
==
0
)
if
(
cmp
==
0
)
break
;
break
;
Py_INCREF
(
parent
);
parent
=
PyList_GET_ITEM
(
heap
,
parentpos
);
olditem
=
PyList_GET_ITEM
(
heap
,
pos
);
newitem
=
PyList_GET_ITEM
(
heap
,
pos
);
PyList_SET_ITEM
(
heap
,
parentpos
,
newitem
);
PyList_SET_ITEM
(
heap
,
pos
,
parent
);
PyList_SET_ITEM
(
heap
,
pos
,
parent
);
Py_DECREF
(
olditem
);
pos
=
parentpos
;
pos
=
parentpos
;
if
(
size
!=
PyList_GET_SIZE
(
heap
))
{
PyErr_SetString
(
PyExc_RuntimeError
,
"list changed size during iteration"
);
return
-
1
;
}
}
}
Py_DECREF
(
PyList_GET_ITEM
(
heap
,
pos
));
PyList_SET_ITEM
(
heap
,
pos
,
newitem
);
return
0
;
return
0
;
}
}
...
@@ -63,20 +51,16 @@ static int
...
@@ -63,20 +51,16 @@ static int
_siftup
(
PyListObject
*
heap
,
Py_ssize_t
pos
)
_siftup
(
PyListObject
*
heap
,
Py_ssize_t
pos
)
{
{
Py_ssize_t
startpos
,
endpos
,
childpos
,
rightpos
,
limit
;
Py_ssize_t
startpos
,
endpos
,
childpos
,
rightpos
,
limit
;
PyObject
*
tmp1
,
*
tmp2
;
int
cmp
;
int
cmp
;
PyObject
*
newitem
,
*
tmp
,
*
olditem
;
Py_ssize_t
size
;
assert
(
PyList_Check
(
heap
));
assert
(
PyList_Check
(
heap
));
size
=
PyList_GET_SIZE
(
heap
);
endpos
=
PyList_GET_SIZE
(
heap
);
endpos
=
size
;
startpos
=
pos
;
startpos
=
pos
;
if
(
pos
>=
endpos
)
{
if
(
pos
>=
endpos
)
{
PyErr_SetString
(
PyExc_IndexError
,
"index out of range"
);
PyErr_SetString
(
PyExc_IndexError
,
"index out of range"
);
return
-
1
;
return
-
1
;
}
}
newitem
=
PyList_GET_ITEM
(
heap
,
pos
);
Py_INCREF
(
newitem
);
/* Bubble up the smaller child until hitting a leaf. */
/* Bubble up the smaller child until hitting a leaf. */
limit
=
endpos
/
2
;
/* smallest pos that has no child */
limit
=
endpos
/
2
;
/* smallest pos that has no child */
...
@@ -89,37 +73,24 @@ _siftup(PyListObject *heap, Py_ssize_t pos)
...
@@ -89,37 +73,24 @@ _siftup(PyListObject *heap, Py_ssize_t pos)
PyList_GET_ITEM
(
heap
,
childpos
),
PyList_GET_ITEM
(
heap
,
childpos
),
PyList_GET_ITEM
(
heap
,
rightpos
),
PyList_GET_ITEM
(
heap
,
rightpos
),
Py_LT
);
Py_LT
);
if
(
cmp
==
-
1
)
{
if
(
cmp
==
-
1
)
Py_DECREF
(
newitem
);
return
-
1
;
return
-
1
;
}
if
(
cmp
==
0
)
if
(
cmp
==
0
)
childpos
=
rightpos
;
childpos
=
rightpos
;
}
if
(
endpos
!=
PyList_GET_SIZE
(
heap
))
{
if
(
size
!=
PyList_GET_SIZE
(
heap
))
{
PyErr_SetString
(
PyExc_RuntimeError
,
Py_DECREF
(
newitem
);
"list changed size during iteration"
);
PyErr_SetString
(
PyExc_RuntimeError
,
return
-
1
;
"list changed size during iteration"
);
}
return
-
1
;
}
}
/* Move the smaller child up. */
/* Move the smaller child up. */
tmp
=
PyList_GET_ITEM
(
heap
,
childpos
);
tmp1
=
PyList_GET_ITEM
(
heap
,
childpos
);
Py_INCREF
(
tmp
);
tmp2
=
PyList_GET_ITEM
(
heap
,
pos
);
olditem
=
PyList_GET_ITEM
(
heap
,
pos
);
PyList_SET_ITEM
(
heap
,
childpos
,
tmp2
);
PyList_SET_ITEM
(
heap
,
pos
,
tmp
);
PyList_SET_ITEM
(
heap
,
pos
,
tmp1
);
Py_DECREF
(
olditem
);
pos
=
childpos
;
pos
=
childpos
;
if
(
size
!=
PyList_GET_SIZE
(
heap
))
{
PyErr_SetString
(
PyExc_RuntimeError
,
"list changed size during iteration"
);
return
-
1
;
}
}
}
/* Bubble it up to its final resting place (by sifting its parents down). */
/* The leaf at pos is empty now. Put newitem there, and bubble
it up to its final resting place (by sifting its parents down). */
Py_DECREF
(
PyList_GET_ITEM
(
heap
,
pos
));
PyList_SET_ITEM
(
heap
,
pos
,
newitem
);
return
_siftdown
(
heap
,
startpos
,
pos
);
return
_siftdown
(
heap
,
startpos
,
pos
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment