diff options
author | Robert James Kaes <rjkaes@users.sourceforge.net> | 2003-03-13 05:20:06 +0000 |
---|---|---|
committer | Robert James Kaes <rjkaes@users.sourceforge.net> | 2003-03-13 05:20:06 +0000 |
commit | 2f9370afe7d07307f636a2473348c2e17de59a9c (patch) | |
tree | 5edf831204f7597894016af435901d9de8046468 | |
parent | d98e5352c29c90503d614b8433740ad04624cc15 (diff) | |
download | tinyproxy-2f9370afe7d07307f636a2473348c2e17de59a9c.tar.gz tinyproxy-2f9370afe7d07307f636a2473348c2e17de59a9c.zip |
(chomp): Fixed up the code to prevent negative array access. Added
code to make sure the supplied arguments are valid.
Diffstat (limited to '')
-rw-r--r-- | src/text.c | 18 | ||||
-rw-r--r-- | src/text.h | 4 |
2 files changed, 14 insertions, 8 deletions
@@ -1,4 +1,4 @@ -/* $Id: text.c,v 1.2 2002-05-24 04:45:32 rjkaes Exp $ +/* $Id: text.c,v 1.3 2003-03-13 05:20:06 rjkaes Exp $ * * The functions included here are useful for text manipulation. They * replace or augment the standard C string library. These functions @@ -75,24 +75,30 @@ strlcat(char *dst, const char *src, size_t size) * "length" should be the number of characters in the buffer, not including * the trailing NULL. * - * Returns the number of characters removed from the end of the string. + * Returns the number of characters removed from the end of the string. A + * negative return value indicates an error. */ -size_t +ssize_t chomp(char *buffer, size_t length) { size_t chars; assert(buffer != NULL); + assert(length > 0); + + /* Make sure the arguments are valid */ + if (buffer == NULL) return -EFAULT; + if (length < 1) return -ERANGE; chars = 0; --length; while (buffer[length] == '\r' || buffer[length] == '\n') { - buffer[length--] = '\0'; + buffer[length] = '\0'; chars++; - if (length < 0) - break; + /* Stop once we get to zero to prevent wrap-around */ + if (length-- == 0) break; } return chars; @@ -1,4 +1,4 @@ -/* $Id: text.h,v 1.1 2002-05-23 04:42:30 rjkaes Exp $ +/* $Id: text.h,v 1.2 2003-03-13 05:20:06 rjkaes Exp $ * * See 'text.c' for a detailed description. * @@ -26,6 +26,6 @@ extern size_t strlcat(char *dst, const char *src, size_t size); extern size_t strlcpy(char *dst, const char *src, size_t size); #endif /* HAVE_STRLCPY */ -extern size_t chomp(char *buffer, size_t length); +extern ssize_t chomp(char *buffer, size_t length); #endif |