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

Skip to content

Commit cfb60ee

Browse files
committed
Fixed very nasty null-dereferencing bug in DisposeControl/destroy
object.
1 parent cc778eb commit cfb60ee

4 files changed

Lines changed: 42 additions & 35 deletions

File tree

Mac/Modules/ctl/Ctlmodule.c

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,10 @@ CtlObj_Convert(v, p_itself)
9292
static void CtlObj_dealloc(self)
9393
ControlObject *self;
9494
{
95-
SetCRefCon(self->ob_itself, (long)0); /* Make it forget about us */
95+
if (self->ob_itself) SetCRefCon(self->ob_itself, (long)0); /* Make it forget about us */
9696
PyMem_DEL(self);
9797
}
9898

99-
static PyObject *CtlObj_DisposeControl(_self, _args)
100-
ControlObject *_self;
101-
PyObject *_args;
102-
{
103-
PyObject *_res = NULL;
104-
if (!PyArg_ParseTuple(_args, ""))
105-
return NULL;
106-
DisposeControl(_self->ob_itself);
107-
Py_INCREF(Py_None);
108-
_res = Py_None;
109-
return _res;
110-
}
111-
11299
static PyObject *CtlObj_ShowControl(_self, _args)
113100
ControlObject *_self;
114101
PyObject *_args;
@@ -452,9 +439,26 @@ static PyObject *CtlObj_as_Resource(_self, _args)
452439

453440
}
454441

442+
static PyObject *CtlObj_DisposeControl(_self, _args)
443+
ControlObject *_self;
444+
PyObject *_args;
445+
{
446+
PyObject *_res = NULL;
447+
448+
if (!PyArg_ParseTuple(_args, ""))
449+
return NULL;
450+
if ( _self->ob_itself ) {
451+
SetCRefCon(_self->ob_itself, (long)0); /* Make it forget about us */
452+
DisposeControl(_self->ob_itself);
453+
_self->ob_itself = NULL;
454+
}
455+
Py_INCREF(Py_None);
456+
_res = Py_None;
457+
return _res;
458+
459+
}
460+
455461
static PyMethodDef CtlObj_methods[] = {
456-
{"DisposeControl", (PyCFunction)CtlObj_DisposeControl, 1,
457-
"() -> None"},
458462
{"ShowControl", (PyCFunction)CtlObj_ShowControl, 1,
459463
"() -> None"},
460464
{"HideControl", (PyCFunction)CtlObj_HideControl, 1,
@@ -499,6 +503,8 @@ static PyMethodDef CtlObj_methods[] = {
499503
"() -> (SInt32 _rv)"},
500504
{"as_Resource", (PyCFunction)CtlObj_as_Resource, 1,
501505
"Return this Control as a Resource"},
506+
{"DisposeControl", (PyCFunction)CtlObj_DisposeControl, 1,
507+
"() -> None"},
502508
{NULL, NULL, 0}
503509
};
504510

@@ -588,21 +594,6 @@ static PyObject *Ctl_GetNewControl(_self, _args)
588594
return _res;
589595
}
590596

591-
static PyObject *Ctl_KillControls(_self, _args)
592-
PyObject *_self;
593-
PyObject *_args;
594-
{
595-
PyObject *_res = NULL;
596-
WindowPtr theWindow;
597-
if (!PyArg_ParseTuple(_args, "O&",
598-
WinObj_Convert, &theWindow))
599-
return NULL;
600-
KillControls(theWindow);
601-
Py_INCREF(Py_None);
602-
_res = Py_None;
603-
return _res;
604-
}
605-
606597
static PyObject *Ctl_DrawControls(_self, _args)
607598
PyObject *_self;
608599
PyObject *_args;
@@ -663,8 +654,6 @@ static PyMethodDef Ctl_methods[] = {
663654
"(WindowPtr theWindow, Rect boundsRect, Str255 title, Boolean visible, SInt16 value, SInt16 min, SInt16 max, SInt16 procID, SInt32 refCon) -> (ControlHandle _rv)"},
664655
{"GetNewControl", (PyCFunction)Ctl_GetNewControl, 1,
665656
"(SInt16 controlID, WindowPtr owner) -> (ControlHandle _rv)"},
666-
{"KillControls", (PyCFunction)Ctl_KillControls, 1,
667-
"(WindowPtr theWindow) -> None"},
668657
{"DrawControls", (PyCFunction)Ctl_DrawControls, 1,
669658
"(WindowPtr theWindow) -> None"},
670659
{"UpdateControls", (PyCFunction)Ctl_UpdateControls, 1,

Mac/Modules/ctl/ctledit.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,21 @@
66
f.docstring = lambda : "Return this Control as a Resource"
77

88
methods.append(f)
9+
10+
DisposeControl_body = """
11+
if (!PyArg_ParseTuple(_args, ""))
12+
return NULL;
13+
if ( _self->ob_itself ) {
14+
SetCRefCon(_self->ob_itself, (long)0); /* Make it forget about us */
15+
DisposeControl(_self->ob_itself);
16+
_self->ob_itself = NULL;
17+
}
18+
Py_INCREF(Py_None);
19+
_res = Py_None;
20+
return _res;
21+
"""
22+
23+
f = ManualGenerator("DisposeControl", DisposeControl_body)
24+
f.docstring = lambda : "() -> None"
25+
26+
methods.append(f)

Mac/Modules/ctl/ctlscan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def destination(self, type, name, arglist):
3030

3131
def makeblacklistnames(self):
3232
return [
33-
'DisposeControl' # Implied by deletion of control object
33+
'DisposeControl', # Generated manually
3434
'KillControls', # Implied by close of dialog
3535
'SetCtlAction',
3636
]

Mac/Modules/ctl/ctlsupport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def outputInitStructMembers(self):
6969
GlobalObjectDefinition.outputInitStructMembers(self)
7070
Output("SetCRefCon(itself, (long)it);")
7171
def outputCleanupStructMembers(self):
72-
Output("SetCRefCon(self->ob_itself, (long)0); /* Make it forget about us */")
72+
Output("if (self->ob_itself) SetCRefCon(self->ob_itself, (long)0); /* Make it forget about us */")
7373

7474
# Create the generator groups and link them
7575
module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)

0 commit comments

Comments
 (0)