From dafece75bdcadeb7e418e3548720278f78bc7173 Mon Sep 17 00:00:00 2001 From: jthorn Date: Mon, 3 Feb 2003 08:57:03 +0000 Subject: add LocalInterp_Strlcpy() function for safer string copying, also standalone test driver for this to make sure I have it right! git-svn-id: http://svn.cactuscode.org/arrangements/CactusBase/LocalInterp/trunk@131 df1f8a13-aa1d-4dd4-9681-27ded5b42416 --- src/GeneralizedPolynomial-Uniform/makefile | 17 ++++- src/GeneralizedPolynomial-Uniform/test_strlcpy.c | 74 +++++++++++++++++++ src/GeneralizedPolynomial-Uniform/util.c | 94 ++++++++++++++++-------- 3 files changed, 153 insertions(+), 32 deletions(-) create mode 100644 src/GeneralizedPolynomial-Uniform/test_strlcpy.c (limited to 'src') diff --git a/src/GeneralizedPolynomial-Uniform/makefile b/src/GeneralizedPolynomial-Uniform/makefile index 752cb59..97920f1 100644 --- a/src/GeneralizedPolynomial-Uniform/makefile +++ b/src/GeneralizedPolynomial-Uniform/makefile @@ -1,5 +1,8 @@ # Makefile for standalone test programs in this directory -# $Header: /mnt/data2/cvs2svn/cvs-repositories/arrangements/CactusBase/LocalInterp/src/GeneralizedPolynomial-Uniform/makefile,v 1.5 2002-09-02 10:21:03 jthorn Exp $ +# $Header: /mnt/data2/cvs2svn/cvs-repositories/arrangements/CactusBase/LocalInterp/src/GeneralizedPolynomial-Uniform/makefile,v 1.6 2003-02-03 08:57:03 jthorn Exp $ + +.PHONY : all +all : test_molecule_posn test_strlcpy test_molecule_posn : test_molecule_posn.c molecule_posn.c \ InterpLocalUniform.h @@ -9,3 +12,15 @@ test_molecule_posn : test_molecule_posn.c molecule_posn.c \ -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations \ -DLOCALINTERP_STANDALONE_BUILD -o test_molecule_posn \ test_molecule_posn.c molecule_posn.c -lm + +test_strlcpy : test_strlcpy.c util.c InterpLocalUniform.h + gcc -g \ + -Wall -W -Wno-unused -Wshadow -Winline -Wpointer-arith \ + -Wbad-function-cast -Wcast-align -Wcast-qual \ + -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations \ + -DLOCALINTERP_STANDALONE_BUILD -o test_strlcpy \ + test_strlcpy.c util.c + +.PHONY : clean +clean : + -rm test_molecule_posn test_strlcpy diff --git a/src/GeneralizedPolynomial-Uniform/test_strlcpy.c b/src/GeneralizedPolynomial-Uniform/test_strlcpy.c new file mode 100644 index 0000000..247c972 --- /dev/null +++ b/src/GeneralizedPolynomial-Uniform/test_strlcpy.c @@ -0,0 +1,74 @@ +/* test_strlcpy -- test driver for Util_Strlcpy() */ +/* $Header$ */ + +#include +#include + +#ifndef LOCALINTERP_STANDALONE_BUILD + #include "cctk.h" +#endif + +#include "InterpLocalUniform.h" + +/******************************************************************************/ + +/* prototypes */ +size_t tryit(size_t dst_size, const char* src); +void nprint(int n_print, const char* buf); + +/* global data structures */ +static char buffer[100]; + +/******************************************************************************/ + +/* + * This program is a test driver for Util_Strlcpy() . + */ + +int main(void) +{ +size_t n; + +n = tryit(9, "hello"); +printf("bufsize=9: result=%d buffer=", (int) n); +nprint(9, buffer); + +n = tryit(6, "hello"); +printf("bufsize=6: result=%d buffer=", (int) n); +nprint(6, buffer); + +n = tryit(5, "hello"); +printf("bufsize=5: result=%d buffer=", (int) n); +nprint(5, buffer); + +n = tryit(4, "hello"); +printf("bufsize=4: result=%d buffer=", (int) n); +nprint(4, buffer); + +return 0; +} + +/******************************************************************************/ + +size_t tryit(size_t dst_size, const char* src) +{ +strcpy(buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); +return LocalInterp_Strlcpy(buffer, src, dst_size); +} + +/******************************************************************************/ + +/* print n_print characters of buf[], with visible indication of '\0' */ +void nprint(int n_print, const char* buf) +{ +int i; + +printf("\""); + for (i = 0 ; i < n_print ; ++i) + { + if (buf[i] == '\0') + then printf("\\0"); + else printf("%c", buf[i]); + } +printf("\"\n"); +} diff --git a/src/GeneralizedPolynomial-Uniform/util.c b/src/GeneralizedPolynomial-Uniform/util.c index f4715e2..03186f0 100644 --- a/src/GeneralizedPolynomial-Uniform/util.c +++ b/src/GeneralizedPolynomial-Uniform/util.c @@ -14,9 +14,15 @@ #include #include #include +#include -#ifndef LOCALINTERP_STANDALONE_BUILD +#ifdef LOCALINTERP_STANDALONE_BUILD + #define WANT_STRLCPY +#else #include "cctk.h" + #define WANT_ZERO_INT_ARRAY + #define WANT_DECODE_N_PARTS + #define WANT_STRLCPY #endif #include "InterpLocalUniform.h" @@ -29,36 +35,7 @@ static const char *rcsid = "$Header$"; /******************************************************************************/ -/*@@ - @routine LocalInterp_zero_int_array - @date 23 Oct 2001 - @author Jonathan Thornburg - @desc This function sets a CCTK_INT array to all zeros. - @enddesc - - @var N - @vdesc The number of elements in the array. - @vtype CCTK_INT N - @endvar - - @var array - @vdesc (A pointer to) the array - @vtype CCTK_INT array[] - @vio out - @endvar - @@*/ -void LocalInterp_zero_int_array(int N, CCTK_INT array[]) -{ -int i; - - for (i = 0 ; i < N ; ++i) - { - array[i] = 0; - } -} - -/******************************************************************************/ - +#ifdef WANT_DECODE_N_PARTS /*@@ @routine LocalInterp_decode_N_parts @date 22 Jan 2002 @@ -107,3 +84,58 @@ case CCTK_VARIABLE_FPOINTER: return 0; default: return -1; } } +#endif /* WANT_DECODE_N_PARTS */ + +/******************************************************************************/ + +#ifdef WANT_STRLCPY +/*@@ + @routine LocalInterp_Strlcpy + @date 1.Feb.2003 + @author Jonathan Thornburg + @desc This function implements the strlcpy() function + described in + http://www.openbsd.org/papers/strlcpy-paper.ps + + The strlcpy(3) function copies up to size-1 characters + from the null-terminated string src to dst , followed + by a null character (so dst is always null-terminated). + + strlcpy(3) is a replacement for strncpy(3). In comparison + to strncpy(3), strlcpy(3) is safer and easier to use (it + guarantees null termination of the destination buffer), + and faster (it doesn't have to fill the entire buffer with + null characters). + @enddesc + + @var dst + @vdesc A non-null pointer to the destination buffer. + @vtype char* dst + @endvar + + @var src + @vdesc A non-null pointer to the source string. + @vtype const char* dst + @endvar + + @var dst_size + @vdesc The size of the destination buffer. + @vtype size_t dst_size + @endvar + + @returntype size_t + @returndesc This function returns strlen(src). + @endreturndesc + @@*/ +size_t LocalInterp_Strlcpy(char* dst, const char* src, size_t dst_size) +{ +const size_t src_size = strlen(src); +if (src_size < dst_size) + then strcpy(dst, src); + else { + strncpy(dst, src, dst_size-1); + dst[dst_size-1] = '\0'; + } +return src_size; +} +#endif /* WANT_STRLCPY */ -- cgit v1.2.3