bbc6a8
From 76188809d5ca40c5285b0ab202b5edea7be3f04d Mon Sep 17 00:00:00 2001
bbc6a8
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <pali.rohar@gmail.com>
bbc6a8
Date: Thu, 22 Jun 2017 14:33:17 +0200
31cddd
Subject: [PATCH] udf: Fix reading label, lvd.ident is dstring
bbc6a8
bbc6a8
UDF dstring has stored length in the last byte of buffer. Therefore last
bbc6a8
byte is not part of recorded characters. And empty string in dstring is
bbc6a8
encoded as empty buffer, including first byte (compression id).
bbc6a8
---
bbc6a8
 grub-core/fs/udf.c | 21 ++++++++++++++++++++-
bbc6a8
 1 file changed, 20 insertions(+), 1 deletion(-)
bbc6a8
bbc6a8
diff --git a/grub-core/fs/udf.c b/grub-core/fs/udf.c
ec4acb
index 839bff88963..2587456336e 100644
bbc6a8
--- a/grub-core/fs/udf.c
bbc6a8
+++ b/grub-core/fs/udf.c
bbc6a8
@@ -860,6 +860,25 @@ read_string (const grub_uint8_t *raw, grub_size_t sz, char *outbuf)
bbc6a8
   return outbuf;
bbc6a8
 }
bbc6a8
 
bbc6a8
+static char *
bbc6a8
+read_dstring (const grub_uint8_t *raw, grub_size_t sz)
bbc6a8
+{
bbc6a8
+  grub_size_t len;
bbc6a8
+
bbc6a8
+  if (raw[0] == 0) {
bbc6a8
+      char *outbuf = grub_malloc (1);
bbc6a8
+      if (!outbuf)
bbc6a8
+	return NULL;
bbc6a8
+      outbuf[0] = 0;
bbc6a8
+      return outbuf;
bbc6a8
+    }
bbc6a8
+
bbc6a8
+  len = raw[sz - 1];
bbc6a8
+  if (len > sz - 1)
bbc6a8
+    len = sz - 1;
bbc6a8
+  return read_string (raw, len, NULL);
bbc6a8
+}
bbc6a8
+
bbc6a8
 static int
bbc6a8
 grub_udf_iterate_dir (grub_fshelp_node_t dir,
bbc6a8
 		      grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
bbc6a8
@@ -1197,7 +1216,7 @@ grub_udf_label (grub_device_t device, char **label)
bbc6a8
 
bbc6a8
   if (data)
bbc6a8
     {
bbc6a8
-      *label = read_string (data->lvd.ident, sizeof (data->lvd.ident), 0);
bbc6a8
+      *label = read_dstring (data->lvd.ident, sizeof (data->lvd.ident));
bbc6a8
       grub_free (data);
bbc6a8
     }
bbc6a8
   else