From: David Lawrence Ramsey Date: Tue, 17 May 2005 00:25:50 +0000 (+0000) Subject: add error detection to parse_line_column() X-Git-Tag: v1.3.8~284 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=c5659d320e8c5180a8c2736b7c3c293c4979006f;p=nano.git add error detection to parse_line_column() git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2519 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/src/proto.h b/src/proto.h index 3af2a555..b6430ad7 100644 --- a/src/proto.h +++ b/src/proto.h @@ -547,7 +547,7 @@ int regexp_bol_or_eol(const regex_t *preg, const char *string); int digits(size_t n); void get_homedir(void); bool parse_num(const char *str, ssize_t *val); -void parse_line_column(const char *str, int *line, ssize_t *column); +bool parse_line_column(const char *str, int *line, ssize_t *column); void align(char **strp); void null_at(char **data, size_t index); void unsunder(char *str, size_t true_len); diff --git a/src/utils.c b/src/utils.c index e5adf844..6a016d22 100644 --- a/src/utils.c +++ b/src/utils.c @@ -106,20 +106,35 @@ bool parse_num(const char *str, ssize_t *val) } /* Read an int and a ssize_t, separated by a comma, from str, and store - * them in *line and *column (if they're not both NULL). */ -void parse_line_column(const char *str, int *line, ssize_t *column) + * them in *line and *column (if they're not both NULL). On error, we + * return FALSE. Otherwise, we return TRUE. */ +bool parse_line_column(const char *str, int *line, ssize_t *column) { + bool retval = TRUE; char *comma; assert(str != NULL); comma = strchr(str, ','); - if (comma != NULL && column != NULL) - parse_num(&str[comma - str + 1], column); + if (comma != NULL && column != NULL) { + if (!parse_num(&str[comma - str + 1], column)) + retval = FALSE; + } + + if (line != NULL) { + if (comma != NULL) { + char *str_line = mallocstrncpy(NULL, str, comma - str); + + if (!parse_num(str_line, line)) + retval = FALSE; + + free(str_line); + } else if (!parse_num(str, line)) + retval = FALSE; + } - if (line != NULL) - *line = atoi(str); + return retval; } /* Fix the memory allocation for a string. */