[Pkg-gnupg-commit] [gnupg2] 167/241: common: Extend utf8_charcount to include the string's length.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Wed Dec 9 20:32:12 UTC 2015


This is an automated email from the git hooks/post-receive script.

dkg pushed a commit to branch master
in repository gnupg2.

commit 5b84b0d660c8329e184d98682665aaea7e1703d2
Author: Neal H. Walfield <neal at g10code.com>
Date:   Mon Nov 23 22:13:56 2015 +0100

    common: Extend utf8_charcount to include the string's length.
    
    * common/stringhelp.c (utf8_charcount): Take additional parameter,
    len.  Process at most LEN bytes.
    
    --
    Signed-off-by: Neal H. Walfield <neal at g10code.com>
---
 agent/genkey.c      |  2 +-
 common/stringhelp.c | 24 +++++++++++++++++++-----
 common/stringhelp.h |  2 +-
 g10/gpg.c           |  2 +-
 4 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/agent/genkey.c b/agent/genkey.c
index b780c50..2eec974 100644
--- a/agent/genkey.c
+++ b/agent/genkey.c
@@ -221,7 +221,7 @@ check_passphrase_constraints (ctrl_t ctrl, const char *pw,
 
   /* Now check the constraints and collect the error messages unless
      in in silent mode which returns immediately.  */
-  if (utf8_charcount (pw) < minlen )
+  if (utf8_charcount (pw, -1) < minlen )
     {
       if (!failed_constraint)
         {
diff --git a/common/stringhelp.c b/common/stringhelp.c
index 38c3832..d0b5561 100644
--- a/common/stringhelp.c
+++ b/common/stringhelp.c
@@ -746,16 +746,30 @@ sanitize_buffer (const void *p_arg, size_t n, int delim)
 
 /* Given a string containing an UTF-8 encoded text, return the number
    of characters in this string.  It differs from strlen in that it
-   only counts complete UTF-8 characters.  Note, that this function
-   does not take combined characters into account.  */
+   only counts complete UTF-8 characters.  SIZE is the maximum length
+   of the string in bytes.  If SIZE is -1, then a NUL character is
+   taken to be the end of the string.  Note, that this function does
+   not take combined characters into account.  */
 size_t
-utf8_charcount (const char *s)
+utf8_charcount (const char *s, int len)
 {
   size_t n;
 
+  if (len == 0)
+    return 0;
+
   for (n=0; *s; s++)
-    if ( (*s&0xc0) != 0x80 ) /* Exclude continuation bytes: 10xxxxxx */
-      n++;
+    {
+      if ( (*s&0xc0) != 0x80 ) /* Exclude continuation bytes: 10xxxxxx */
+        n++;
+
+      if (len != -1)
+        {
+          len --;
+          if (len == 0)
+            break;
+        }
+    }
 
   return n;
 }
diff --git a/common/stringhelp.h b/common/stringhelp.h
index a84c81b..b34d28b 100644
--- a/common/stringhelp.h
+++ b/common/stringhelp.h
@@ -64,7 +64,7 @@ int hextobyte (const char *s);
 char *sanitize_buffer (const void *p, size_t n, int delim);
 
 
-size_t utf8_charcount (const char *s);
+size_t utf8_charcount (const char *s, int len);
 
 
 #ifdef HAVE_W32_SYSTEM
diff --git a/g10/gpg.c b/g10/gpg.c
index 5ba3a20..5c5afa6 100644
--- a/g10/gpg.c
+++ b/g10/gpg.c
@@ -1043,7 +1043,7 @@ build_list (const char *text, char letter,
   if (maybe_setuid)
     gcry_control (GCRYCTL_INIT_SECMEM, 0, 0);  /* Drop setuid. */
 
-  indent = utf8_charcount (text);
+  indent = utf8_charcount (text, -1);
   len = 0;
   init_membuf (&mb, 512);
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/gnupg2.git



More information about the Pkg-gnupg-commit mailing list