The following commit has been merged in the master branch:
commit 295a6102cacb6be15bf2441549d2dcb505990138
Author: Guillem Jover <[email protected]>
Date:   Sun Oct 31 06:28:58 2010 +0100

    libdpkg: Refactor string termination in path_quote_filename
    
    Move all destination string termination to the end of the function.
    Always check the size limit before writting to the desintation and bail
    out if there's no enough space.

diff --git a/lib/dpkg/path.c b/lib/dpkg/path.c
index 2f54916..55641fb 100644
--- a/lib/dpkg/path.c
+++ b/lib/dpkg/path.c
@@ -3,7 +3,7 @@
  * path.c - path handling functions
  *
  * Copyright © 1995 Ian Jackson <[email protected]>
- * Copyright © 2008, 2009 Guillem Jover <[email protected]>
+ * Copyright © 2008-2010 Guillem Jover <[email protected]>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -113,39 +113,34 @@ path_quote_filename(char *dst, const char *src, size_t n)
        if (size == 0)
                return r;
 
-       while (size > 0) {
-               if (*src == '\0') {
-                       *dst = '\0';
-                       return r;
-               } else if (*src == '\\') {
-                       if (size <= 2) {
-                               /* Buffer full. */
-                               *dst = '\0';
-                               return r;
-                       }
+       while (*src) {
+               if (*src == '\\') {
+                       size -= 2;
+                       if (size <= 0)
+                               break;
+
                        *dst++ = '\\';
                        *dst++ = '\\';
                        src++;
-                       size -= 2;
                } else if (((*src) & 0x80) == '\0') {
+                       size--;
+                       if (size <= 0)
+                               break;
+
                        *dst++ = *src++;
-                       --size;
                } else {
-                       if (size > 4) {
-                               sprintf(dst, "\\%03o",
-                                       *(const unsigned char *)src);
-                               size -= 4;
-                               dst += 4;
-                               src++;
-                       } else {
-                               /* Buffer full. */
-                               *dst = '\0';
-                               return r;
-                       }
+                       size -= 4;
+                       if (size <= 0)
+                               break;
+
+                       sprintf(dst, "\\%03o",
+                               *(const unsigned char *)src);
+                       dst += 4;
+                       src++;
                }
        }
-       /* Buffer full. */
-       *(dst - 1) = '\0';
+
+       *dst = '\0';
 
        return r;
 }

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to