Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc8a108

Browse files
committed
Protect dir() against non-directory __dict__ attributes.
1 parent c0aab89 commit dc8a108

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

Python/bltinmodule.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,27 @@ builtin_dir(self, v)
7878
object *d;
7979
if (v == NULL) {
8080
d = getlocals();
81+
INCREF(d);
8182
}
8283
else {
83-
if (!is_moduleobject(v)) {
84+
d = getattr(v, "__dict__");
85+
if (d == NULL) {
8486
err_setstr(TypeError,
85-
"dir() argument must be module or absent");
87+
"dir() argument has no variable attributes");
8688
return NULL;
8789
}
88-
d = getmoduledict(v);
8990
}
90-
v = getdictkeys(d);
91-
if (sortlist(v) != 0) {
92-
DECREF(v);
93-
v = NULL;
91+
if (!is_dictobject(d)) { /* Assume it is None */
92+
v = newlistobject(0);
9493
}
94+
else {
95+
v = getdictkeys(d);
96+
if (sortlist(v) != 0) {
97+
DECREF(v);
98+
v = NULL;
99+
}
100+
}
101+
DECREF(d);
95102
return v;
96103
}
97104

0 commit comments

Comments
 (0)