From b9b68ae7a5176605ec482420cd8030bc56b144e1 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 16 Jul 2008 22:04:20 +0000 Subject: [PATCH] #3305: self->stream can be NULL. --- Lib/test/test_multibytecodec.py | 9 +++++++++ Modules/cjkcodecs/multibytecodec.c | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_multibytecodec.py b/Lib/test/test_multibytecodec.py index f2b042cc7ce..090374cdafb 100644 --- a/Lib/test/test_multibytecodec.py +++ b/Lib/test/test_multibytecodec.py @@ -8,6 +8,7 @@ from test import test_support from test import test_multibytecodec_support from test.test_support import TESTFN import unittest, StringIO, codecs, sys, os +import _multibytecodec ALL_CJKENCODINGS = [ # _codecs_cn @@ -53,6 +54,14 @@ class Test_MultibyteCodec(unittest.TestCase): finally: os.unlink(TESTFN) + def test_init_segfault(self): + # bug #3305: this used to segfault + self.assertRaises(AttributeError, + _multibytecodec.MultibyteStreamReader, None) + self.assertRaises(AttributeError, + _multibytecodec.MultibyteStreamWriter, None) + + class Test_IncrementalEncoder(unittest.TestCase): def test_stateless(self): diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c index a1b0ca91805..2bd8b0dcce7 100644 --- a/Modules/cjkcodecs/multibytecodec.c +++ b/Modules/cjkcodecs/multibytecodec.c @@ -1484,7 +1484,7 @@ mbstreamreader_dealloc(MultibyteStreamReaderObject *self) { PyObject_GC_UnTrack(self); ERROR_DECREF(self->errors); - Py_DECREF(self->stream); + Py_XDECREF(self->stream); Py_TYPE(self)->tp_free(self); } @@ -1686,7 +1686,7 @@ mbstreamwriter_dealloc(MultibyteStreamWriterObject *self) { PyObject_GC_UnTrack(self); ERROR_DECREF(self->errors); - Py_DECREF(self->stream); + Py_XDECREF(self->stream); Py_TYPE(self)->tp_free(self); }