Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sbin/gpt refactor the utf code so that it does not leak memory.
details: https://anonhg.NetBSD.org/src/rev/2bbd5dee682a
branches: trunk
changeset: 342001:2bbd5dee682a
user: christos <christos%NetBSD.org@localhost>
date: Wed Dec 02 04:07:11 2015 +0000
description:
refactor the utf code so that it does not leak memory.
diffstat:
sbin/gpt/add.c | 7 ++++---
sbin/gpt/biosboot.c | 12 +++++++-----
sbin/gpt/gpt.c | 34 ++++++++++++++++++----------------
sbin/gpt/gpt.h | 2 +-
sbin/gpt/label.c | 4 ++--
sbin/gpt/restore.c | 5 +++--
sbin/gpt/show.c | 12 ++++++++----
7 files changed, 43 insertions(+), 33 deletions(-)
diffs (265 lines):
diff -r 47641b158a67 -r 2bbd5dee682a sbin/gpt/add.c
--- a/sbin/gpt/add.c Wed Dec 02 04:06:47 2015 +0000
+++ b/sbin/gpt/add.c Wed Dec 02 04:07:11 2015 +0000
@@ -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 @@
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
diff -r 47641b158a67 -r 2bbd5dee682a sbin/gpt/biosboot.c
--- a/sbin/gpt/biosboot.c Wed Dec 02 04:06:47 2015 +0000
+++ b/sbin/gpt/biosboot.c Wed Dec 02 04:07:11 2015 +0000
@@ -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 @@
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 @@
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 &&
diff -r 47641b158a67 -r 2bbd5dee682a sbin/gpt/gpt.c
--- a/sbin/gpt/gpt.c Wed Dec 02 04:06:47 2015 +0000
+++ b/sbin/gpt/gpt.c Wed Dec 02 04:07:11 2015 +0000
@@ -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 @@
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 @@
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 @@
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 @@
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))
diff -r 47641b158a67 -r 2bbd5dee682a sbin/gpt/gpt.h
--- a/sbin/gpt/gpt.h Wed Dec 02 04:06:47 2015 +0000
+++ b/sbin/gpt/gpt.h Wed Dec 02 04:07:11 2015 +0000
@@ -97,7 +97,7 @@
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:"
diff -r 47641b158a67 -r 2bbd5dee682a sbin/gpt/label.c
--- a/sbin/gpt/label.c Wed Dec 02 04:06:47 2015 +0000
+++ b/sbin/gpt/label.c Wed Dec 02 04:07:11 2015 +0000
@@ -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 @@
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
diff -r 47641b158a67 -r 2bbd5dee682a sbin/gpt/restore.c
--- a/sbin/gpt/restore.c Wed Dec 02 04:06:47 2015 +0000
+++ b/sbin/gpt/restore.c Wed Dec 02 04:07:11 2015 +0000
@@ -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 @@
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);
diff -r 47641b158a67 -r 2bbd5dee682a sbin/gpt/show.c
--- a/sbin/gpt/show.c Wed Dec 02 04:06:47 2015 +0000
+++ b/sbin/gpt/show.c Wed Dec 02 04:07:11 2015 +0000
@@ -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 @@
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 @@
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 @@
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 @@
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)
Home |
Main Index |
Thread Index |
Old Index