add error detection to parse_line_column()

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2519 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
David Lawrence Ramsey 2005-05-17 00:25:50 +00:00
parent 2f0cd35322
commit c5659d320e
2 changed files with 22 additions and 7 deletions

View File

@ -547,7 +547,7 @@ int regexp_bol_or_eol(const regex_t *preg, const char *string);
int digits(size_t n); int digits(size_t n);
void get_homedir(void); void get_homedir(void);
bool parse_num(const char *str, ssize_t *val); 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 align(char **strp);
void null_at(char **data, size_t index); void null_at(char **data, size_t index);
void unsunder(char *str, size_t true_len); void unsunder(char *str, size_t true_len);

View File

@ -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 /* 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). */ * them in *line and *column (if they're not both NULL). On error, we
void parse_line_column(const char *str, int *line, ssize_t *column) * return FALSE. Otherwise, we return TRUE. */
bool parse_line_column(const char *str, int *line, ssize_t *column)
{ {
bool retval = TRUE;
char *comma; char *comma;
assert(str != NULL); assert(str != NULL);
comma = strchr(str, ','); comma = strchr(str, ',');
if (comma != NULL && column != NULL) if (comma != NULL && column != NULL) {
parse_num(&str[comma - str + 1], column); if (!parse_num(&str[comma - str + 1], column))
retval = FALSE;
}
if (line != NULL) if (line != NULL) {
*line = atoi(str); 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;
}
return retval;
} }
/* Fix the memory allocation for a string. */ /* Fix the memory allocation for a string. */