work on fix #36: conditional strlcpy/strcat impl for non-BSD systems. Branched to make testing on other OSs easier before merge to default. strl-imp
authorKrista Grothoff <krista@pep-project.org>
Fri, 22 Jul 2016 12:29:06 +0200
branchstrl-imp
changeset 91845c80cc4c2ed
parent 915 72fe8bd4ae5e
child 919 5d02ed589daa
work on fix #36: conditional strlcpy/strcat impl for non-BSD systems. Branched to make testing on other OSs easier before merge to default.
src/message_api.c
src/platform_unix.c
src/platform_unix.h
src/platform_windows.h
     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 12:29:06 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 12:29:06 2016 +0200
     2.3 @@ -94,6 +94,34 @@
     2.4  }
     2.5  #endif
     2.6  
     2.7 +#ifndef BSD
     2.8 +size_t strlcpy(char* dst, const	char* src, size_t size) {
     2.9 +    size_t retval = strlen(src);
    2.10 +    size_t size_to_copy = (retval < size ? retval : size - 1);
    2.11 +    
    2.12 +    // strlcpy doc says src and dst not allowed to overlap, as
    2.13 +    // it's undefined. So this is acceptable:
    2.14 +    memcpy((void*)dst, (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
    2.15 +    dst[size_to_copy] = '\0';
    2.16 +    return retval;
    2.17 +}
    2.18 +size_t strlcat(char* dst, const	char* src, size_t size) {
    2.19 +    size_t start_len = strnlen(dst, size);
    2.20 +    if (start_len == size)
    2.21 +        return size; // no copy, no null termination in size bytes, according to spec
    2.22 +    
    2.23 +    size_t add_len = strlen(src);
    2.24 +    size_t retval = start_len + add_len;
    2.25 +    size_t size_to_copy = (retval < size ? add_len : (size - start_len) - 1);
    2.26 +    
    2.27 +    // strlcat doc says src and dst not allowed to overlap, as
    2.28 +    // it's undefined. So this is acceptable:
    2.29 +    memcpy((void*)(dst + start_len), (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
    2.30 +    dst[start_len + size_to_copy] = '\0';
    2.31 +    return retval;
    2.32 +}
    2.33 +#endif
    2.34 +
    2.35  const char *unix_local_db(void)
    2.36  {
    2.37      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 12:29:06 2016 +0200
     3.3 @@ -37,6 +37,11 @@
     3.4  #endif
     3.5  #endif
     3.6  
     3.7 +#ifndef BSD
     3.8 +size_t strlcpy(char* dst, const	char* src, size_t size);
     3.9 +size_t strlcat(char* dst, const	char* src, size_t size);
    3.10 +#endif
    3.11 +
    3.12  #ifdef __cplusplus
    3.13  }
    3.14  #endif
     4.1 --- a/src/platform_windows.h	Wed Jul 20 15:26:26 2016 +0200
     4.2 +++ b/src/platform_windows.h	Fri Jul 22 12:29:06 2016 +0200
     4.3 @@ -49,6 +49,9 @@
     4.4  char *strndup(const char *s1, size_t n);
     4.5  char *stpcpy(char *dst, const char *src);
     4.6  
     4.7 +size_t strlcpy(char* dst, const	char* src, size_t size);
     4.8 +size_t strlcat(char* dst, const	char* src, size_t size);
     4.9 +
    4.10  const char *windoze_local_db(void);
    4.11  const char *windoze_system_db(void);
    4.12  const char *gpg_conf(void);