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

Skip to content

Commit f215060

Browse files
committed
Jeremy's patches for more robust handling of unmarshallable types.
1 parent bbb1e26 commit f215060

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

Python/marshal.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5050

5151
typedef struct {
5252
FILE *fp;
53+
int error;
5354
/* If fp == NULL, the following are valid: */
5455
object *str;
5556
char *ptr;
@@ -220,6 +221,7 @@ w_object(v, p)
220221
}
221222
else {
222223
w_byte(TYPE_UNKNOWN, p);
224+
p->error = 1;
223225
}
224226
}
225227

@@ -230,6 +232,7 @@ wr_long(x, fp)
230232
{
231233
WFILE wf;
232234
wf.fp = fp;
235+
wf.error = 0;
233236
w_long(x, &wf);
234237
}
235238

@@ -240,6 +243,7 @@ wr_object(x, fp)
240243
{
241244
WFILE wf;
242245
wf.fp = fp;
246+
wf.error = 0;
243247
w_object(x, &wf);
244248
}
245249

@@ -429,10 +433,10 @@ r_object(p)
429433
object *key, *val;
430434
key = r_object(p);
431435
if (key == NULL)
432-
break; /* XXXX and how about memory errors? */
436+
break; /* XXX Assume TYPE_NULL, not an error */
433437
val = r_object(p);
434-
/* XXXX error check? */
435-
dict2insert(v, key, val);
438+
if (val != NULL)
439+
dict2insert(v, key, val);
436440
DECREF(key);
437441
XDECREF(val);
438442
}
@@ -476,8 +480,10 @@ r_object(p)
476480
return v;
477481

478482
default:
479-
err_setstr(TypeError, "read unknown object");
480-
return NULL;
483+
/* Bogus data got written, which isn't ideal.
484+
This will let you keep working and recover. */
485+
INCREF(None);
486+
return None;
481487

482488
}
483489
}
@@ -541,7 +547,12 @@ marshal_dump(self, args)
541547
wf.fp = getfilefile(f);
542548
wf.str = NULL;
543549
wf.ptr = wf.end = NULL;
550+
wf.error = 0;
544551
w_object(x, &wf);
552+
if (wf.error) {
553+
err_setstr(ValueError, "unmarshallable object");
554+
return NULL;
555+
}
545556
INCREF(None);
546557
return None;
547558
}
@@ -587,10 +598,16 @@ marshal_dumps(self, args)
587598
return NULL;
588599
wf.ptr = GETSTRINGVALUE((stringobject *)wf.str);
589600
wf.end = wf.ptr + getstringsize(wf.str);
601+
wf.error = 0;
590602
w_object(x, &wf);
591603
if (wf.str != NULL)
592604
resizestring(&wf.str,
593605
(int) (wf.ptr - GETSTRINGVALUE((stringobject *)wf.str)));
606+
if (wf.error) {
607+
XDECREF(wf.str);
608+
err_setstr(ValueError, "unmarshallable object");
609+
return NULL;
610+
}
594611
return wf.str;
595612
}
596613

0 commit comments

Comments
 (0)