Reimplement strlcpy() using strncpy_nul()

This commit is contained in:
Andrew Bettison 2015-10-13 18:40:10 +10:30
parent 3ab7e04497
commit 27d98a29fa
4 changed files with 30 additions and 21 deletions

View File

@ -27,7 +27,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "conf.h"
#include "crypto.h"
#include "strbuf.h"
#include "strlcpy.h"
#include "keyring.h"
#include "dataformats.h"
#include "commandline.h"

View File

@ -31,7 +31,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "serval.h"
#include "rhizome.h"
#include "conf.h"
#include "strlcpy.h"
#define RHIZOME_BUFFER_MAXIMUM_SIZE (1024*1024)

View File

@ -1,24 +1,30 @@
/*
* ANSI C version of strlcpy
* Based on the NetBSD strlcpy man page.
*
* Nathan Myers <ncm-nospam@cantrip.org>, 2003/06/03
* Placed in the public domain.
*/
Copyright (C) 2015 Serval Project Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef HAVE_STRLCPY
#include <stdlib.h> /* for size_t */
#include <string.h> /* for strlen, memcpy */
#include "str.h"
size_t
strlcpy(char *dst, const char *src, size_t size) {
const size_t len = strlen(src);
if (size != 0) {
memcpy(dst, src, (len > size - 1) ? size - 1 : len);
dst[size - 1] = 0;
}
return len;
size_t strlcpy(char *dst, const char *src, size_t size)
{
strncpy_nul(dst, src, size);
return strlen(src);
}
#endif

View File

@ -1,5 +1,5 @@
/*
Copyright (C) 2012 Serval Project Inc.
Copyright (C) 2012,2015 Serval Project Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
@ -20,10 +20,15 @@
#ifndef __STRLCPY_H__
#define __STRLCPY_H__
// Do not use strlcpy() in Serval DNA source code, use strncpy_nul() or
// buf_strncpy_nul() from "str.h" instead. This strlcpy() is provided only
// because it is needed by sqlite3.c.
#ifdef HAVE_STRLCPY
#include <string.h>
# include <string.h>
#else
size_t strlcpy(char *dst, const char *src, size_t sz);
# include <stdlib.h> // for size_t
size_t strlcpy(char *dst, const char *src, size_t sz);
#endif
#endif