Module Name:    src
Committed By:   christos
Date:           Wed Dec  2 04:07:11 UTC 2015

Modified Files:
        src/sbin/gpt: add.c biosboot.c gpt.c gpt.h label.c restore.c show.c

Log Message:
refactor the utf code so that it does not leak memory.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/sbin/gpt/add.c
cvs rdiff -u -r1.18 -r1.19 src/sbin/gpt/biosboot.c
cvs rdiff -u -r1.52 -r1.53 src/sbin/gpt/gpt.c
cvs rdiff -u -r1.26 -r1.27 src/sbin/gpt/gpt.h src/sbin/gpt/show.c
cvs rdiff -u -r1.23 -r1.24 src/sbin/gpt/label.c
cvs rdiff -u -r1.11 -r1.12 src/sbin/gpt/restore.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/gpt/add.c
diff -u src/sbin/gpt/add.c:1.35 src/sbin/gpt/add.c:1.36
--- src/sbin/gpt/add.c:1.35	Tue Dec  1 20:01:55 2015
+++ src/sbin/gpt/add.c	Tue Dec  1 23:07:11 2015
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: add.c,v 1.35 2015/12/02 01:01:55 christos Exp $");
+__RCSID("$NetBSD: add.c,v 1.36 2015/12/02 04:07:11 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -78,8 +78,9 @@ ent_set(struct gpt_ent *ent, const map_t
 	gpt_uuid_copy(ent->ent_type, xtype);
 	ent->ent_lba_start = htole64(map->map_start);
 	ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
-	if (xname != NULL)
-		utf8_to_utf16(xname, ent->ent_name, sizeof(ent->ent_name));
+	if (xname == NULL)
+		return;
+	utf8_to_utf16(xname, ent->ent_name, __arraycount(ent->ent_name));
 }
 
 static int

Index: src/sbin/gpt/biosboot.c
diff -u src/sbin/gpt/biosboot.c:1.18 src/sbin/gpt/biosboot.c:1.19
--- src/sbin/gpt/biosboot.c:1.18	Tue Dec  1 18:29:07 2015
+++ src/sbin/gpt/biosboot.c	Tue Dec  1 23:07:11 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: biosboot.c,v 1.18 2015/12/01 23:29:07 christos Exp $ */
+/*	$NetBSD: biosboot.c,v 1.19 2015/12/02 04:07:11 christos Exp $ */
 
 /*
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: biosboot.c,v 1.18 2015/12/01 23:29:07 christos Exp $");
+__RCSID("$NetBSD: biosboot.c,v 1.19 2015/12/02 04:07:11 christos Exp $");
 #endif
 
 #include <sys/stat.h>
@@ -173,6 +173,7 @@ biosboot(gpt_t gpt)
 	struct mbr *mbr, *bootcode;
 	unsigned int i;
 	struct gpt_ent *ent;
+	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
 
 	/*
 	 * Parse and validate partition maps
@@ -212,10 +213,11 @@ biosboot(gpt_t gpt)
 		if (entry > 0 && m->map_index == entry)
 			break;
 
-		if (label != NULL)
-			if (strcmp((char *)label,
-			    (char *)utf16_to_utf8(ent->ent_name)) == 0)
+		if (label != NULL) {
+			utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+			if (strcmp((char *)label, (char *)utfbuf) == 0)
 				break;
+		}
 
 		/* next, partition as could be specified by wedge */
 		if (entry < 1 && label == NULL && size > 0 &&

Index: src/sbin/gpt/gpt.c
diff -u src/sbin/gpt/gpt.c:1.52 src/sbin/gpt/gpt.c:1.53
--- src/sbin/gpt/gpt.c:1.52	Tue Dec  1 18:29:07 2015
+++ src/sbin/gpt/gpt.c	Tue Dec  1 23:07:11 2015
@@ -35,7 +35,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: gpt.c,v 1.52 2015/12/01 23:29:07 christos Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.53 2015/12/02 04:07:11 christos Exp $");
 #endif
 
 #include <sys/param.h>
@@ -121,24 +121,16 @@ crc32(const void *buf, size_t size)
 	return crc ^ ~0U;
 }
 
-uint8_t *
-utf16_to_utf8(uint16_t *s16)
+void
+utf16_to_utf8(const uint16_t *s16, uint8_t *s8, size_t s8len)
 {
-	static uint8_t *s8 = NULL;
-	static size_t s8len = 0;
 	size_t s8idx, s16idx, s16len;
 	uint32_t utfchar;
 	unsigned int c;
 
 	s16len = 0;
 	while (s16[s16len++] != 0)
-		;
-	if (s8len < s16len * 3) {
-		if (s8 != NULL)
-			free(s8);
-		s8len = s16len * 3;
-		s8 = calloc(s16len, 3);
-	}
+		continue;
 	s8idx = s16idx = 0;
 	while (s16idx < s16len) {
 		utfchar = le16toh(s16[s16idx++]);
@@ -150,22 +142,30 @@ utf16_to_utf8(uint16_t *s16)
 				s16idx++;
 		}
 		if (utfchar < 0x80) {
+			if (s8idx + 1 >= s8len)
+				break;
 			s8[s8idx++] = utfchar;
 		} else if (utfchar < 0x800) {
+			if (s8idx + 2 >= s8len)
+				break;
 			s8[s8idx++] = 0xc0 | (utfchar >> 6);
 			s8[s8idx++] = 0x80 | (utfchar & 0x3f);
 		} else if (utfchar < 0x10000) {
+			if (s8idx + 3 >= s8len)
+				break;
 			s8[s8idx++] = 0xe0 | (utfchar >> 12);
 			s8[s8idx++] = 0x80 | ((utfchar >> 6) & 0x3f);
 			s8[s8idx++] = 0x80 | (utfchar & 0x3f);
 		} else if (utfchar < 0x200000) {
+			if (s8idx + 4 >= s8len)
+				break;
 			s8[s8idx++] = 0xf0 | (utfchar >> 18);
 			s8[s8idx++] = 0x80 | ((utfchar >> 12) & 0x3f);
 			s8[s8idx++] = 0x80 | ((utfchar >> 6) & 0x3f);
 			s8[s8idx++] = 0x80 | (utfchar & 0x3f);
 		}
 	}
-	return (s8);
+	s8[s8idx] = 0;
 }
 
 void
@@ -983,6 +983,7 @@ gpt_change_ent(gpt_t gpt, const struct g
 	struct gpt_hdr *hdr;
 	struct gpt_ent *ent;
 	unsigned int i;
+	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
 
 	if (!find->all ^
 	    (find->block > 0 || find->entry > 0 || find->label != NULL
@@ -1006,10 +1007,11 @@ gpt_change_ent(gpt_t gpt, const struct g
 		i = m->map_index - 1;
 
 		ent = gpt_ent_primary(gpt, i);
-		if (find->label != NULL)
-			if (strcmp((char *)find->label,
-			    (char *)utf16_to_utf8(ent->ent_name)) != 0)
+		if (find->label != NULL) {
+			utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+			if (strcmp((char *)find->label, (char *)utfbuf) == 0)
 				continue;
+		}
 
 		if (!gpt_uuid_is_nil(find->type) &&
 		    !gpt_uuid_equal(find->type, ent->ent_type))

Index: src/sbin/gpt/gpt.h
diff -u src/sbin/gpt/gpt.h:1.26 src/sbin/gpt/gpt.h:1.27
--- src/sbin/gpt/gpt.h:1.26	Tue Dec  1 18:29:07 2015
+++ src/sbin/gpt/gpt.h	Tue Dec  1 23:07:11 2015
@@ -97,7 +97,7 @@ struct gpt_ent *gpt_ent_primary(gpt_t, u
 struct gpt_ent *gpt_ent_backup(gpt_t, unsigned int);
 int	gpt_usage(const char *, const struct gpt_cmd *);
 
-uint8_t *utf16_to_utf8(uint16_t *);
+void 	utf16_to_utf8(const uint16_t *, uint8_t *, size_t);
 void	utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
 
 #define GPT_FIND "ab:i:L:s:t:"
Index: src/sbin/gpt/show.c
diff -u src/sbin/gpt/show.c:1.26 src/sbin/gpt/show.c:1.27
--- src/sbin/gpt/show.c:1.26	Tue Dec  1 18:29:07 2015
+++ src/sbin/gpt/show.c	Tue Dec  1 23:07:11 2015
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: show.c,v 1.26 2015/12/01 23:29:07 christos Exp $");
+__RCSID("$NetBSD: show.c,v 1.27 2015/12/02 04:07:11 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -77,6 +77,7 @@ show(gpt_t gpt)
 	struct mbr *mbr;
 	struct gpt_ent *ent;
 	unsigned int i;
+	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
 
 	printf("  %*s", gpt->lbawidth, "start");
 	printf("  %*s", gpt->lbawidth, "size");
@@ -134,8 +135,9 @@ show(gpt_t gpt)
 			printf("GPT part ");
 			ent = m->map_data;
 			if (show_label) {
-				printf("- \"%s\"",
-				    utf16_to_utf8(ent->ent_name));
+				utf16_to_utf8(ent->ent_name, utfbuf,
+				    sizeof(utfbuf));
+				printf("- \"%s\"", (char *)utfbuf);
 			} else if (show_guid) {
 				char buf[128];
 				gpt_uuid_snprintf(
@@ -169,6 +171,7 @@ show_one(gpt_t gpt)
 	map_t m;
 	struct gpt_ent *ent;
 	char s1[128], s2[128];
+	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
 #ifdef HN_AUTOSCALE
 	char human_num[5];
 #endif
@@ -212,7 +215,8 @@ show_one(gpt_t gpt)
 	gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
 	printf("GUID: %s\n", s2);
 
-	printf("Label: %s\n", utf16_to_utf8(ent->ent_name));
+	utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+	printf("Label: %s\n", (char *)utfbuf);
 
 	printf("Attributes:\n");
 	if (ent->ent_attr == 0)

Index: src/sbin/gpt/label.c
diff -u src/sbin/gpt/label.c:1.23 src/sbin/gpt/label.c:1.24
--- src/sbin/gpt/label.c:1.23	Tue Dec  1 18:29:07 2015
+++ src/sbin/gpt/label.c	Tue Dec  1 23:07:11 2015
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: label.c,v 1.23 2015/12/01 23:29:07 christos Exp $");
+__RCSID("$NetBSD: label.c,v 1.24 2015/12/02 04:07:11 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -70,7 +70,7 @@ static void
 change(struct gpt_ent *ent, void *v)
 {
 	uint8_t *name = v;
-	utf8_to_utf16(name, ent->ent_name, 36);
+	utf8_to_utf16(name, ent->ent_name, __arraycount(ent->ent_name));
 }
 
 static int

Index: src/sbin/gpt/restore.c
diff -u src/sbin/gpt/restore.c:1.11 src/sbin/gpt/restore.c:1.12
--- src/sbin/gpt/restore.c:1.11	Tue Dec  1 11:32:19 2015
+++ src/sbin/gpt/restore.c	Tue Dec  1 23:07:11 2015
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: restore.c,v 1.11 2015/12/01 16:32:19 christos Exp $");
+__RCSID("$NetBSD: restore.c,v 1.12 2015/12/02 04:07:11 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -331,7 +331,8 @@ restore(gpt_t gpt)
 		propstr = prop_dictionary_get(gpt_dict, "name");
 		if (propstr != NULL) {
 			s = prop_string_cstring_nocopy(propstr);
-			utf8_to_utf16((const uint8_t *)s, ent.ent_name, 36);
+			utf8_to_utf16((const uint8_t *)s, ent.ent_name,
+				__arraycount(ent.ent_name));
 		}
 		propnum = prop_dictionary_get(gpt_dict, "index");
 		PROP_ERR(propnum);

Reply via email to