svn commit: r281181 - head/usr.bin/sort

Pedro F. Giffuni pfg at FreeBSD.org
Tue Apr 7 01:17:30 UTC 2015


Author: pfg
Date: Tue Apr  7 01:17:29 2015
New Revision: 281181
URL: https://svnweb.freebsd.org/changeset/base/281181

Log:
  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

Modified:
  head/usr.bin/sort/bwstring.c

Modified: head/usr.bin/sort/bwstring.c
==============================================================================
--- head/usr.bin/sort/bwstring.c	Mon Apr  6 23:37:04 2015	(r281180)
+++ head/usr.bin/sort/bwstring.c	Tue Apr  7 01:17:29 2015	(r281181)
@@ -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]);


More information about the svn-src-all mailing list