sort(1): Cleanups and a small memory leak.
Remove useless check for leading blanks in the month name. The code didn't adjust len after stripping blanks so even if a month *did* start with a blank we'd end up copying garbage at the end. Also convert a malloc + memcpy to strdup and fix a memory leak in the wide char version if mbstowcs() fails. Originally from Andre Smagin. Obtained from: OpenBSD (CVS rev. 1.2, 1.3) MFC after: 1 week
This commit is contained in:
parent
2df04ab49b
commit
b904ab99bc
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=281181
@ -65,18 +65,12 @@ initialise_months(void)
|
|||||||
for (int i = 0; i < 12; i++) {
|
for (int i = 0; i < 12; i++) {
|
||||||
cmonths[i] = NULL;
|
cmonths[i] = NULL;
|
||||||
tmp = (unsigned char *) nl_langinfo(item[i]);
|
tmp = (unsigned char *) nl_langinfo(item[i]);
|
||||||
if (tmp == NULL)
|
|
||||||
continue;
|
|
||||||
if (debug_sort)
|
if (debug_sort)
|
||||||
printf("month[%d]=%s\n", i, tmp);
|
printf("month[%d]=%s\n", i, tmp);
|
||||||
len = strlen((char*)tmp);
|
if (*tmp == '\0')
|
||||||
if (len < 1)
|
|
||||||
continue;
|
continue;
|
||||||
while (isblank(*tmp))
|
m = sort_strdup(tmp);
|
||||||
++tmp;
|
len = strlen(tmp);
|
||||||
m = sort_malloc(len + 1);
|
|
||||||
memcpy(m, tmp, len + 1);
|
|
||||||
m[len] = '\0';
|
|
||||||
for (unsigned int j = 0; j < len; j++)
|
for (unsigned int j = 0; j < len; j++)
|
||||||
m[j] = toupper(m[j]);
|
m[j] = toupper(m[j]);
|
||||||
cmonths[i] = m;
|
cmonths[i] = m;
|
||||||
@ -91,18 +85,17 @@ initialise_months(void)
|
|||||||
for (int i = 0; i < 12; i++) {
|
for (int i = 0; i < 12; i++) {
|
||||||
wmonths[i] = NULL;
|
wmonths[i] = NULL;
|
||||||
tmp = (unsigned char *) nl_langinfo(item[i]);
|
tmp = (unsigned char *) nl_langinfo(item[i]);
|
||||||
if (tmp == NULL)
|
|
||||||
continue;
|
|
||||||
if (debug_sort)
|
if (debug_sort)
|
||||||
printf("month[%d]=%s\n", i, tmp);
|
printf("month[%d]=%s\n", i, tmp);
|
||||||
len = strlen((char*)tmp);
|
if (*tmp == '\0')
|
||||||
if (len < 1)
|
|
||||||
continue;
|
continue;
|
||||||
while (isblank(*tmp))
|
len = strlen(tmp);
|
||||||
++tmp;
|
|
||||||
m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1));
|
m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1));
|
||||||
if (mbstowcs(m, (char*)tmp, len) == ((size_t) -1))
|
if (mbstowcs(m, (char*)tmp, len) ==
|
||||||
|
((size_t) - 1)) {
|
||||||
|
sort_free(m);
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
m[len] = L'\0';
|
m[len] = L'\0';
|
||||||
for (unsigned int j = 0; j < len; j++)
|
for (unsigned int j = 0; j < len; j++)
|
||||||
m[j] = towupper(m[j]);
|
m[j] = towupper(m[j]);
|
||||||
|
Loading…
Reference in New Issue
Block a user