[PATCH] os: Teach vpnprintf() how to handle "%*.*s"

Jon TURNEY jon.turney at dronecode.org.uk
Tue Nov 11 06:54:55 PST 2014


On 28/10/2014 11:12, Jon TURNEY wrote:
> On 21/10/2014 15:25, Colin Harrison wrote:
>> There are instances of this format also in os/access.c (not that I
>> ever get sight of them!) Probably best fixed in vpnprintf() but your
>> xdmcp patch fixes the most seen instance (for me).
>
> Oh yes.  Also there are some occurrences of the "%*.*s" pattern in dmx.
>
> Patch withdrawn, I'll try to come up with a fix to vpnprintf() instead.

Attached is an attempt at fixing this in vpnprintf()

-------------- next part --------------
From b7d741dd3e7225ac033f8ad4323913dad46c766e Mon Sep 17 00:00:00 2001
From: Jon TURNEY <jon.turney at dronecode.org.uk>
Date: Tue, 4 Nov 2014 13:20:23 +0000
Subject: [PATCH] os: Teach vpnprintf() how to handle "%*.*s"

Teach vpnprintf() how to handle "%*.*s", which is currently used in a few places

$ fgrep -r "%*.*s" *
hw/dmx/config/scanner.l:    fprintf(stderr, "parse error on line %d at token \"%*.*s\"\n",
hw/dmx/dmxlog.c:        ErrorF("(%s) dmx[i%d/%*.*s]: ", type,
hw/dmx/input/dmxinputinit.c:                dmxLogCont(dmxInfo, "\t[i%d/%*.*s",
os/access.c:        ErrorF("Xserver: siAddrMatch(): type = %s, value = %*.*s -- %s\n",
os/access.c:                ("Xserver: siCheckAddr(): type = %s, value = %*.*s, len = %d -- %s\n",
os/xdmcp.c:    FatalError("XDMCP fatal error: %s %*.*s\n", type,

Signed-off-by: Jon TURNEY <jon.turney at dronecode.org.uk>
---
 os/log.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/os/log.c b/os/log.c
index 7348ad4..0f4c19b 100644
--- a/os/log.c
+++ b/os/log.c
@@ -352,6 +352,7 @@ vpnprintf(char *string, int size_in, const char *f, va_list args)
     uint64_t ui;
     int64_t si;
     size_t size = size_in;
+    int precision;
 
     for (; f_idx < f_len && s_idx < size - 1; f_idx++) {
         int length_modifier = 0;
@@ -362,9 +363,29 @@ vpnprintf(char *string, int size_in, const char *f, va_list args)
 
         f_idx++;
 
-        /* silently swallow digit length modifiers */
-        while (f_idx < f_len && ((f[f_idx] >= '0' && f[f_idx] <= '9') || f[f_idx] == '.'))
+        /* silently swallow minimum field width */
+        if (f[f_idx] == '*') {
             f_idx++;
+            va_arg(args, int);
+        } else {
+            while (f_idx < f_len && ((f[f_idx] >= '0' && f[f_idx] <= '9')))
+                f_idx++;
+        }
+
+        /* is there a precision? */
+        precision = size;
+        if (f[f_idx] == '.') {
+            f_idx++;
+            if (f[f_idx] == '*') {
+                f_idx++;
+                /* precision is supplied in an int argument */
+                precision = va_arg(args, int);
+            } else {
+                /* silently swallow precision digits */
+                while (f_idx < f_len && ((f[f_idx] >= '0' && f[f_idx] <= '9')))
+                    f_idx++;
+            }
+        }
 
         /* non-digit length modifiers */
         if (f_idx < f_len) {
@@ -380,9 +401,8 @@ vpnprintf(char *string, int size_in, const char *f, va_list args)
         switch (f[f_idx]) {
         case 's':
             string_arg = va_arg(args, char*);
-            p_len = strlen_sigsafe(string_arg);
 
-            for (i = 0; i < p_len && s_idx < size - 1; i++)
+            for (i = 0; string_arg[i] != 0 && s_idx < size - 1 && s_idx < precision; i++)
                 string[s_idx++] = string_arg[i];
             break;
 
-- 
2.1.1



More information about the xorg-devel mailing list