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:
Pedro F. Giffuni 2015-04-07 01:17:29 +00:00
parent 2df04ab49b
commit b904ab99bc
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=281181

View File

@ -65,18 +65,12 @@ initialise_months(void)
for (int i = 0; i < 12; i++) {
cmonths[i] = NULL;
tmp = (unsigned char *) nl_langinfo(item[i]);
if (tmp == NULL)
continue;
if (debug_sort)
printf("month[%d]=%s\n", i, tmp);
len = strlen((char*)tmp);
if (len < 1)
if (*tmp == '\0')
continue;
while (isblank(*tmp))
++tmp;
m = sort_malloc(len + 1);
memcpy(m, tmp, len + 1);
m[len] = '\0';
m = sort_strdup(tmp);
len = strlen(tmp);
for (unsigned int j = 0; j < len; j++)
m[j] = toupper(m[j]);
cmonths[i] = m;
@ -91,18 +85,17 @@ initialise_months(void)
for (int i = 0; i < 12; i++) {
wmonths[i] = NULL;
tmp = (unsigned char *) nl_langinfo(item[i]);
if (tmp == NULL)
continue;
if (debug_sort)
printf("month[%d]=%s\n", i, tmp);
len = strlen((char*)tmp);
if (len < 1)
if (*tmp == '\0')
continue;
while (isblank(*tmp))
++tmp;
len = strlen(tmp);
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;
}
m[len] = L'\0';
for (unsigned int j = 0; j < len; j++)
m[j] = towupper(m[j]);