~pythonregexp2.7/python/issue2636-09-01+10

« back to all changes in this revision

Viewing changes to Modules/cjkcodecs/multibytecodec.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 21:39:45 UTC
  • mfrom: (39055.1.33 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922213945-23717m5eiqpamcyn
Merged in changes from the Single-Loop Engine branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
163
163
static int
164
164
expand_encodebuffer(MultibyteEncodeBuffer *buf, Py_ssize_t esize)
165
165
{
166
 
        Py_ssize_t orgpos, orgsize;
 
166
        Py_ssize_t orgpos, orgsize, incsize;
167
167
 
168
168
        orgpos = (Py_ssize_t)((char *)buf->outbuf -
169
169
                                PyString_AS_STRING(buf->outobj));
170
170
        orgsize = PyString_GET_SIZE(buf->outobj);
171
 
        if (_PyString_Resize(&buf->outobj, orgsize + (
172
 
            esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize)) == -1)
 
171
        incsize = (esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize);
 
172
 
 
173
        if (orgsize > PY_SSIZE_T_MAX - incsize)
 
174
                return -1;
 
175
 
 
176
        if (_PyString_Resize(&buf->outobj, orgsize + incsize) == -1)
173
177
                return -1;
174
178
 
175
179
        buf->outbuf = (unsigned char *)PyString_AS_STRING(buf->outobj) +orgpos;
473
477
        buf.excobj = NULL;
474
478
        buf.inbuf = buf.inbuf_top = *data;
475
479
        buf.inbuf_end = buf.inbuf_top + datalen;
 
480
 
 
481
        if (datalen > (PY_SSIZE_T_MAX - 16) / 2) {
 
482
                PyErr_NoMemory();
 
483
                goto errorexit;
 
484
        }
 
485
 
476
486
        buf.outobj = PyString_FromStringAndSize(NULL, datalen * 2 + 16);
477
487
        if (buf.outobj == NULL)
478
488
                goto errorexit;
590
600
        MultibyteCodec_State state;
591
601
        MultibyteDecodeBuffer buf;
592
602
        PyObject *errorcb;
 
603
        Py_buffer pdata;
593
604
        const char *data, *errors = NULL;
594
605
        Py_ssize_t datalen, finalsize;
595
606
 
596
 
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|z:decode",
597
 
                                codeckwarglist, &data, &datalen, &errors))
 
607
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*|z:decode",
 
608
                                codeckwarglist, &pdata, &errors))
598
609
                return NULL;
 
610
        data = pdata.buf;
 
611
        datalen = pdata.len;
599
612
 
600
613
        errorcb = internal_error_callback(errors);
601
 
        if (errorcb == NULL)
 
614
        if (errorcb == NULL) {
 
615
                PyBuffer_Release(&pdata);
602
616
                return NULL;
 
617
        }
603
618
 
604
619
        if (datalen == 0) {
 
620
                PyBuffer_Release(&pdata);
605
621
                ERROR_DECREF(errorcb);
606
622
                return make_tuple(PyUnicode_FromUnicode(NULL, 0), 0);
607
623
        }
641
657
                if (PyUnicode_Resize(&buf.outobj, finalsize) == -1)
642
658
                        goto errorexit;
643
659
 
 
660
        PyBuffer_Release(&pdata);
644
661
        Py_XDECREF(buf.excobj);
645
662
        ERROR_DECREF(errorcb);
646
663
        return make_tuple(buf.outobj, datalen);
647
664
 
648
665
errorexit:
 
666
        PyBuffer_Release(&pdata);
649
667
        ERROR_DECREF(errorcb);
650
668
        Py_XDECREF(buf.excobj);
651
669
        Py_XDECREF(buf.outobj);
735
753
        origpending = ctx->pendingsize;
736
754
 
737
755
        if (origpending > 0) {
 
756
                if (datalen > PY_SSIZE_T_MAX - ctx->pendingsize) {
 
757
                        PyErr_NoMemory();
 
758
                        /* inbuf_tmp == NULL */
 
759
                        goto errorexit;
 
760
                }
738
761
                inbuf_tmp = PyMem_New(Py_UNICODE, datalen + ctx->pendingsize);
739
762
                if (inbuf_tmp == NULL)
740
763
                        goto errorexit;
797
820
        Py_ssize_t npendings;
798
821
 
799
822
        npendings = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
800
 
        if (npendings + ctx->pendingsize > MAXDECPENDING) {
801
 
                PyErr_SetString(PyExc_UnicodeError, "pending buffer overflow");
802
 
                return -1;
 
823
        if (npendings + ctx->pendingsize > MAXDECPENDING ||
 
824
                npendings > PY_SSIZE_T_MAX - ctx->pendingsize) {
 
825
                        PyErr_SetString(PyExc_UnicodeError, "pending buffer overflow");
 
826
                        return -1;
803
827
        }
804
828
        memcpy(ctx->pending + ctx->pendingsize, buf->inbuf, npendings);
805
829
        ctx->pendingsize += npendings;
1001
1025
                  PyObject *args, PyObject *kwargs)
1002
1026
{
1003
1027
        MultibyteDecodeBuffer buf;
1004
 
        char *data, *wdata;
 
1028
        char *data, *wdata = NULL;
 
1029
        Py_buffer pdata;
1005
1030
        Py_ssize_t wsize, finalsize = 0, size, origpending;
1006
1031
        int final = 0;
1007
1032
 
1008
 
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "t#|i:decode",
1009
 
                        incrementalkwarglist, &data, &size, &final))
 
1033
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*|i:decode",
 
1034
                        incrementalkwarglist, &pdata, &final))
1010
1035
                return NULL;
 
1036
        data = pdata.buf;
 
1037
        size = pdata.len;
1011
1038
 
1012
1039
        buf.outobj = buf.excobj = NULL;
1013
1040
        origpending = self->pendingsize;
1017
1044
                wdata = data;
1018
1045
        }
1019
1046
        else {
 
1047
                if (size > PY_SSIZE_T_MAX - self->pendingsize) {
 
1048
                        PyErr_NoMemory();
 
1049
                        goto errorexit;
 
1050
                }
1020
1051
                wsize = size + self->pendingsize;
1021
1052
                wdata = PyMem_Malloc(wsize);
1022
1053
                if (wdata == NULL)
1052
1083
                if (PyUnicode_Resize(&buf.outobj, finalsize) == -1)
1053
1084
                        goto errorexit;
1054
1085
 
 
1086
        PyBuffer_Release(&pdata);
1055
1087
        if (wdata != data)
1056
1088
                PyMem_Del(wdata);
1057
1089
        Py_XDECREF(buf.excobj);
1058
1090
        return buf.outobj;
1059
1091
 
1060
1092
errorexit:
 
1093
        PyBuffer_Release(&pdata);
1061
1094
        if (wdata != NULL && wdata != data)
1062
1095
                PyMem_Del(wdata);
1063
1096
        Py_XDECREF(buf.excobj);
1235
1268
                        PyObject *ctr;
1236
1269
                        char *ctrdata;
1237
1270
 
 
1271
                        if (PyString_GET_SIZE(cres) > PY_SSIZE_T_MAX - self->pendingsize) {
 
1272
                                PyErr_NoMemory();
 
1273
                                goto errorexit;
 
1274
            }
1238
1275
                        rsize = PyString_GET_SIZE(cres) + self->pendingsize;
1239
1276
                        ctr = PyString_FromStringAndSize(NULL, rsize);
1240
1277
                        if (ctr == NULL)
1460
1497
{
1461
1498
        PyObject_GC_UnTrack(self);
1462
1499
        ERROR_DECREF(self->errors);
1463
 
        Py_DECREF(self->stream);
 
1500
        Py_XDECREF(self->stream);
1464
1501
        Py_TYPE(self)->tp_free(self);
1465
1502
}
1466
1503
 
1662
1699
{
1663
1700
        PyObject_GC_UnTrack(self);
1664
1701
        ERROR_DECREF(self->errors);
1665
 
        Py_DECREF(self->stream);
 
1702
        Py_XDECREF(self->stream);
1666
1703
        Py_TYPE(self)->tp_free(self);
1667
1704
}
1668
1705