All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Add 'precision' to grub_printf %s format
@ 2009-01-21 19:05 Christian Franke
  2009-01-22 20:28 ` Christian Franke
  0 siblings, 1 reply; 2+ messages in thread
From: Christian Franke @ 2009-01-21 19:05 UTC (permalink / raw)
  To: grub-devel

[-- Attachment #1: Type: text/plain, Size: 545 bytes --]

This patch adds 'precision' support to grub_printf '%s' format, e.g.:

grub_printf("data=%.20s...\n", data);

This feature of standard printf() is useful to limit output length or to 
print strings which are not 0-terminated.

Christian


2009-01-21  Christian Franke  <franke@computer.org> 

	* kern/misc.c (grub_vsprintf): Fix size and termination of `format2'
	(precision) digit string.  Allow `.format2' without `format1' (width).
	Limit input chars for `%s' output to `format2' if specified.  This is
	compatible with standard printf ().



[-- Attachment #2: grub2-printf-string-precision.patch --]
[-- Type: text/x-diff, Size: 1862 bytes --]

diff --git a/kern/misc.c b/kern/misc.c
index 6fbc651..da64eb0 100644
--- a/kern/misc.c
+++ b/kern/misc.c
@@ -700,7 +700,7 @@ grub_vsprintf (char *str, const char *fmt, va_list args)
 	  char tmp[32];
 	  char *p;
 	  unsigned int format1 = 0;
-	  unsigned int format2 = 3;
+	  unsigned int format2 = ~ 0U;
 	  char zerofill = ' ';
 	  int rightfill = 0;
 	  int n;
@@ -727,20 +727,22 @@ grub_vsprintf (char *str, const char *fmt, va_list args)
 		zerofill = '0';
 	      format1 = grub_strtoul (s, 0, 10);
 	      fmt = p;
-	      if (*p && *p == '.')
+	    }
+
+	  if (*p && *p == '.')
+	    {
+	      p++;
+	      fmt++;
+	      while (*p && grub_isdigit (*p))
+		p++;
+
+	      if (p > fmt)
 		{
-		  p++;
-		  fmt++;
-		  while (*p && grub_isdigit (*p))
-		    p++;
-		  
-		  if (p > fmt)
-		    {
-		      char fstr[p - fmt];
-		      grub_strncpy (fstr, fmt, p - fmt);
-		      format2 = grub_strtoul (fstr, 0, 10);
-		      fmt = p;
-		    }
+		  char fstr[p - fmt + 1];
+		  grub_strncpy (fstr, fmt, p - fmt);
+		  fstr[p - fmt] = 0;
+		  format2 = grub_strtoul (fstr, 0, 10);
+		  fmt = p;
 		}
 	    }
 
@@ -847,13 +849,19 @@ grub_vsprintf (char *str, const char *fmt, va_list args)
 	      p = va_arg (args, char *);
 	      if (p)
 		{
-		  if (!rightfill && grub_strlen (p) < format1)
-		    write_fill (zerofill, format1 - grub_strlen (p));
-		  
-		  write_str (p);
-		  
-		  if (rightfill && grub_strlen (p) < format1)
-		    write_fill (zerofill, format1 - grub_strlen (p));
+		  grub_size_t len = 0;
+		  while (len < format2 && p[len])
+		    len++;
+
+		  if (!rightfill && len < format1)
+		    write_fill (zerofill, format1 - len);
+
+		  grub_size_t i;
+		  for (i = 0; i < len; i++)
+		    write_char (*p++);
+
+		  if (rightfill && len < format1)
+		    write_fill (zerofill, format1 - len);
 		}
 	      else
 		write_str ("(null)");

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Add 'precision' to grub_printf %s format
  2009-01-21 19:05 [PATCH] Add 'precision' to grub_printf %s format Christian Franke
@ 2009-01-22 20:28 ` Christian Franke
  0 siblings, 0 replies; 2+ messages in thread
From: Christian Franke @ 2009-01-22 20:28 UTC (permalink / raw)
  To: grub-devel

Christian Franke wrote:
> This patch adds 'precision' support to grub_printf '%s' format, e.g.:
Committed.

Christian




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-01-22 20:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-21 19:05 [PATCH] Add 'precision' to grub_printf %s format Christian Franke
2009-01-22 20:28 ` Christian Franke

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.