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

Skip to content

Commit 4cd44ef

Browse files
committed
Fudge. stropmodule and stringobject both had copies of the buggy
mymemXXX stuff, and they were already out of synch. Fix the remaining bugs in both and get them back in synch. Bugfix release candidate.
1 parent 1a7b3ee commit 4cd44ef

2 files changed

Lines changed: 54 additions & 40 deletions

File tree

Modules/stropmodule.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ strop_translate(PyObject *self, PyObject *args)
982982
MEM, the function returns -1.
983983
*/
984984
static int
985-
mymemfind(char *mem, int len, char *pat, int pat_len)
985+
mymemfind(const char *mem, int len, const char *pat, int pat_len)
986986
{
987987
register int ii;
988988

@@ -1007,7 +1007,7 @@ mymemfind(char *mem, int len, char *pat, int pat_len)
10071007
mem=11111 and pat==11 also return 2.
10081008
*/
10091009
static int
1010-
mymemcnt(char *mem, int len, char *pat, int pat_len)
1010+
mymemcnt(const char *mem, int len, const char *pat, int pat_len)
10111011
{
10121012
register int offset = 0;
10131013
int nfound = 0;
@@ -1043,10 +1043,11 @@ mymemcnt(char *mem, int len, char *pat, int pat_len)
10431043
NULL if an error occurred.
10441044
*/
10451045
static char *
1046-
mymemreplace(char *str, int len,
1047-
char *pat, int pat_len,
1048-
char *sub, int sub_len,
1049-
int count, int *out_len)
1046+
mymemreplace(const char *str, int len, /* input string */
1047+
const char *pat, int pat_len, /* pattern string to find */
1048+
const char *sub, int sub_len, /* substitution string */
1049+
int count, /* number of replacements */
1050+
int *out_len)
10501051
{
10511052
char *out_s;
10521053
char *new_s;
@@ -1064,7 +1065,11 @@ mymemreplace(char *str, int len,
10641065

10651066
new_len = len + nfound*(sub_len - pat_len);
10661067
if (new_len == 0) {
1067-
out_s = "";
1068+
/* Have to allocate something for the caller to free(). */
1069+
out_s = (char *)PyMem_MALLOC(1);
1070+
if (out_s = NULL)
1071+
return NULL;
1072+
out_s[0] = '\0';
10681073
}
10691074
else {
10701075
assert(new_len > 0);
@@ -1102,7 +1107,7 @@ mymemreplace(char *str, int len,
11021107

11031108
return_same:
11041109
*out_len = -1;
1105-
return str;
1110+
return (char *)str; /* cast away const */
11061111
}
11071112

11081113

Objects/stringobject.c

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ mymemreplace(const char *str, int len, /* input string */
15461546
const char *pat, int pat_len, /* pattern string to find */
15471547
const char *sub, int sub_len, /* substitution string */
15481548
int count, /* number of replacements */
1549-
int *out_len)
1549+
int *out_len)
15501550
{
15511551
char *out_s;
15521552
char *new_s;
@@ -1557,47 +1557,56 @@ mymemreplace(const char *str, int len, /* input string */
15571557

15581558
/* find length of output string */
15591559
nfound = mymemcnt(str, len, pat, pat_len);
1560-
if (count < 0)
1561-
count = INT_MAX;
1562-
else if (nfound > count)
1563-
nfound = count;
1560+
if (count > 0)
1561+
nfound = nfound > count ? count : nfound;
15641562
if (nfound == 0)
15651563
goto return_same;
1564+
15661565
new_len = len + nfound*(sub_len - pat_len);
1566+
if (new_len == 0) {
1567+
/* Have to allocate something for the caller to free(). */
1568+
out_s = (char *)PyMem_MALLOC(1);
1569+
if (out_s = NULL)
1570+
return NULL;
1571+
out_s[0] = '\0';
1572+
}
1573+
else {
1574+
assert(new_len > 0);
1575+
new_s = (char *)PyMem_MALLOC(new_len);
1576+
if (new_s == NULL)
1577+
return NULL;
1578+
out_s = new_s;
15671579

1568-
new_s = (char *)PyMem_MALLOC(new_len);
1569-
if (new_s == NULL) return NULL;
1580+
while (len > 0) {
1581+
/* find index of next instance of pattern */
1582+
offset = mymemfind(str, len, pat, pat_len);
1583+
if (offset == -1)
1584+
break;
15701585

1586+
/* copy non matching part of input string */
1587+
memcpy(new_s, str, offset);
1588+
str += offset + pat_len;
1589+
len -= offset + pat_len;
1590+
1591+
/* copy substitute into the output string */
1592+
new_s += offset;
1593+
memcpy(new_s, sub, sub_len);
1594+
new_s += sub_len;
1595+
1596+
/* note count==0 is effectively infinity */
1597+
if (--count == 0)
1598+
break;
1599+
}
1600+
/* copy any remaining values into output string */
1601+
if (len > 0)
1602+
memcpy(new_s, str, len);
1603+
}
15711604
*out_len = new_len;
1572-
out_s = new_s;
1573-
1574-
while (len > 0) {
1575-
/* find index of next instance of pattern */
1576-
offset = mymemfind(str, len, pat, pat_len);
1577-
/* if not found, break out of loop */
1578-
if (offset == -1) break;
1579-
1580-
/* copy non matching part of input string */
1581-
memcpy(new_s, str, offset); /* copy part of str before pat */
1582-
str += offset + pat_len; /* move str past pattern */
1583-
len -= offset + pat_len; /* reduce length of str remaining */
1584-
1585-
/* copy substitute into the output string */
1586-
new_s += offset; /* move new_s to dest for sub string */
1587-
memcpy(new_s, sub, sub_len); /* copy substring into new_s */
1588-
new_s += sub_len; /* offset new_s past sub string */
1589-
1590-
/* break when we've done count replacements */
1591-
if (--count == 0) break;
1592-
}
1593-
/* copy any remaining values into output string */
1594-
if (len > 0)
1595-
memcpy(new_s, str, len);
15961605
return out_s;
15971606

15981607
return_same:
15991608
*out_len = -1;
1600-
return (char*)str; /* have to cast away constness here */
1609+
return (char *)str; /* cast away const */
16011610
}
16021611

16031612

0 commit comments

Comments
 (0)