Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stringification of subclasses of OSError can cause crash #59434

Closed
sbt mannequin opened this issue Jun 30, 2012 · 4 comments
Closed

stringification of subclasses of OSError can cause crash #59434

sbt mannequin opened this issue Jun 30, 2012 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@sbt
Copy link
Mannequin

sbt mannequin commented Jun 30, 2012

BPO 15229
Nosy @pitrou

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2012-06-30.21:39:08.495>
created_at = <Date 2012-06-30.20:09:41.869>
labels = ['interpreter-core', 'type-crash']
title = 'stringification of subclasses of OSError can cause crash'
updated_at = <Date 2012-06-30.21:39:08.493>
user = 'https://bugs.python.org/sbt'

bugs.python.org fields:

activity = <Date 2012-06-30.21:39:08.493>
actor = 'pitrou'
assignee = 'none'
closed = True
closed_date = <Date 2012-06-30.21:39:08.495>
closer = 'pitrou'
components = ['Interpreter Core']
creation = <Date 2012-06-30.20:09:41.869>
creator = 'sbt'
dependencies = []
files = []
hgrepos = []
issue_num = 15229
keywords = []
message_count = 4.0
messages = ['164423', '164426', '164427', '164428']
nosy_count = 3.0
nosy_names = ['pitrou', 'python-dev', 'sbt']
pr_nums = []
priority = 'high'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue15229'
versions = ['Python 3.3']

@sbt
Copy link
Mannequin Author

sbt mannequin commented Jun 30, 2012

If you subclass OSError without calling OSError.__init__() then you can get a crash. For example

  Python 3.3.0b1 (default:cfbe51e66749, Jun 30 2012, 20:50:54) [MSC v.1600 32 bit
  (Intel)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  >>> class MyError(OSError):
  ...   def __init__(self):
  ...     pass
  ...
  [61116 refs]
  >>> repr(MyError())
  Assertion failed: obj, file ..\Objects\unicodeobject.c, line 2646

Note that str(MyError()) results in a crash without hitting a failed assertion.

The problem does not occur in Python 3.2, and it does not affect subclasses of Exception.

@sbt sbt mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump labels Jun 30, 2012
@pitrou
Copy link
Member

pitrou commented Jun 30, 2012

Here is a quick patch (needs a test):

diff --git a/Objects/exceptions.c b/Objects/exceptions.c
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -834,6 +834,7 @@ oserror_init(PyOSErrorObject *self, PyOb
 #endif
 
     /* Steals the reference to args */
+    Py_CLEAR(self->args);
     self->args = args;
     args = NULL;
 
@@ -916,6 +917,11 @@ OSError_new(PyTypeObject *type, PyObject
             ))
             goto error;
     }
+    else {
+        self->args = PyTuple_New(0);
+        if (self->args == NULL)
+            goto error;
+    }
 
     return (PyObject *) self;

@python-dev
Copy link
Mannequin

python-dev mannequin commented Jun 30, 2012

New changeset 1cbab581bf1e by Antoine Pitrou in branch 'default':
Issue bpo-15229: An OSError subclass whose __init__ doesn't call back
http://hg.python.org/cpython/rev/1cbab581bf1e

@pitrou
Copy link
Member

pitrou commented Jun 30, 2012

It should be fixed now.

@pitrou pitrou closed this as completed Jun 30, 2012
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

1 participant