fix #36: merge
authorKrista Grothoff <krista@pep-project.org>
Fri, 22 Jul 2016 18:13:49 +0200
changeset 92296ca717a851f
parent 915 72fe8bd4ae5e
parent 921 2013f282d2f1
child 926 95247f30b919
fix #36: merge
     1.1 --- a/src/message_api.c	Wed Jul 20 15:26:26 2016 +0200
     1.2 +++ b/src/message_api.c	Fri Jul 22 18:13:49 2016 +0200
     1.3 @@ -107,16 +107,16 @@
     1.4      if (ptext == NULL)
     1.5          return NULL;
     1.6  
     1.7 -    strncpy(ptext, "Subject: ", bufsize);
     1.8 +    strlcpy(ptext, "Subject: ", bufsize);
     1.9      bufsize -= 9;
    1.10      
    1.11 -    strncat(ptext, shortmsg, bufsize);
    1.12 +    strlcat(ptext, shortmsg, bufsize);
    1.13      bufsize -= strlen(shortmsg);
    1.14      
    1.15 -    strncat(ptext, "\n\n", bufsize);
    1.16 +    strlcat(ptext, "\n\n", bufsize);
    1.17      bufsize -= 2;
    1.18      
    1.19 -    strncat(ptext, longmsg, bufsize);
    1.20 +    strlcat(ptext, longmsg, bufsize);
    1.21  
    1.22      return ptext;
    1.23  }
    1.24 @@ -536,8 +536,8 @@
    1.25                          if (filename == NULL)
    1.26                              goto enomem;
    1.27  
    1.28 -                        strncpy(filename, _s->filename, len);
    1.29 -                        strncpy(filename + len, ".pgp", 5);
    1.30 +                        strlcpy(filename, _s->filename, len);
    1.31 +                        strlcpy(filename + len, ".pgp", 5);
    1.32                      }
    1.33                      else {
    1.34                          filename = calloc(1, 20);
     2.1 --- a/src/platform_unix.c	Wed Jul 20 15:26:26 2016 +0200
     2.2 +++ b/src/platform_unix.c	Fri Jul 22 18:13:49 2016 +0200
     2.3 @@ -94,6 +94,36 @@
     2.4  }
     2.5  #endif
     2.6  
     2.7 +#ifndef BSD
     2.8 +#if !TARGET_OS_IPHONE
     2.9 +size_t strlcpy(char* dst, const	char* src, size_t size) {
    2.10 +    size_t retval = strlen(src);
    2.11 +    size_t size_to_copy = (retval < size ? retval : size - 1);
    2.12 +    
    2.13 +    // strlcpy doc says src and dst not allowed to overlap, as
    2.14 +    // it's undefined. So this is acceptable:
    2.15 +    memcpy((void*)dst, (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
    2.16 +    dst[size_to_copy] = '\0';
    2.17 +    return retval;
    2.18 +}
    2.19 +size_t strlcat(char* dst, const	char* src, size_t size) {
    2.20 +    size_t start_len = strnlen(dst, size);
    2.21 +    if (start_len == size)
    2.22 +        return size; // no copy, no null termination in size bytes, according to spec
    2.23 +    
    2.24 +    size_t add_len = strlen(src);
    2.25 +    size_t retval = start_len + add_len;
    2.26 +    size_t size_to_copy = (retval < size ? add_len : (size - start_len) - 1);
    2.27 +    
    2.28 +    // strlcat doc says src and dst not allowed to overlap, as
    2.29 +    // it's undefined. So this is acceptable:
    2.30 +    memcpy((void*)(dst + start_len), (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
    2.31 +    dst[start_len + size_to_copy] = '\0';
    2.32 +    return retval;
    2.33 +}
    2.34 +#endif
    2.35 +#endif
    2.36 +
    2.37  const char *unix_local_db(void)
    2.38  {
    2.39      static char buffer[MAX_PATH];
     3.1 --- a/src/platform_unix.h	Wed Jul 20 15:26:26 2016 +0200
     3.2 +++ b/src/platform_unix.h	Fri Jul 22 18:13:49 2016 +0200
     3.3 @@ -29,6 +29,7 @@
     3.4  
     3.5  #elif __APPLE__
     3.6  #include "TargetConditionals.h"
     3.7 +#include <string.h>
     3.8  #if TARGET_OS_IPHONE
     3.9  
    3.10  extern char* SystemDB;
    3.11 @@ -37,6 +38,13 @@
    3.12  #endif
    3.13  #endif
    3.14  
    3.15 +#ifndef BSD
    3.16 +#if !TARGET_OS_IPHONE
    3.17 +size_t strlcpy(char* dst, const	char* src, size_t size);
    3.18 +size_t strlcat(char* dst, const	char* src, size_t size);
    3.19 +#endif
    3.20 +#endif
    3.21 +
    3.22  #ifdef __cplusplus
    3.23  }
    3.24  #endif
     4.1 --- a/src/platform_windows.cpp	Wed Jul 20 15:26:26 2016 +0200
     4.2 +++ b/src/platform_windows.cpp	Fri Jul 22 18:13:49 2016 +0200
     4.3 @@ -233,6 +233,32 @@
     4.4      return dst;
     4.5  }
     4.6  
     4.7 +size_t strlcpy(char* dst, const	char* src, size_t size) {
     4.8 +    size_t retval = strlen(src);
     4.9 +    size_t size_to_copy = (retval < size ? retval : size - 1);
    4.10 +    
    4.11 +    // strlcpy doc says src and dst not allowed to overlap, as
    4.12 +    // it's undefined. So this is acceptable:
    4.13 +    memcpy((void*)dst, (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
    4.14 +    dst[size_to_copy] = '\0';
    4.15 +    return retval;
    4.16 +}
    4.17 +size_t strlcat(char* dst, const	char* src, size_t size) {
    4.18 +    size_t start_len = strnlen(dst, size);
    4.19 +    if (start_len == size)
    4.20 +        return size; // no copy, no null termination in size bytes, according to spec
    4.21 +    
    4.22 +    size_t add_len = strlen(src);
    4.23 +    size_t retval = start_len + add_len;
    4.24 +    size_t size_to_copy = (retval < size ? add_len : (size - start_len) - 1);
    4.25 +    
    4.26 +    // strlcat doc says src and dst not allowed to overlap, as
    4.27 +    // it's undefined. So this is acceptable:
    4.28 +    memcpy((void*)(dst + start_len), (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
    4.29 +    dst[start_len + size_to_copy] = '\0';
    4.30 +    return retval;
    4.31 +}
    4.32 +
    4.33  int mkstemp(char *templ)
    4.34  {
    4.35      char *pathname = _mktemp(templ);
     5.1 --- a/src/platform_windows.h	Wed Jul 20 15:26:26 2016 +0200
     5.2 +++ b/src/platform_windows.h	Fri Jul 22 18:13:49 2016 +0200
     5.3 @@ -49,6 +49,9 @@
     5.4  char *strndup(const char *s1, size_t n);
     5.5  char *stpcpy(char *dst, const char *src);
     5.6  
     5.7 +size_t strlcpy(char* dst, const	char* src, size_t size);
     5.8 +size_t strlcat(char* dst, const	char* src, size_t size);
     5.9 +
    5.10  const char *windoze_local_db(void);
    5.11  const char *windoze_system_db(void);
    5.12  const char *gpg_conf(void);