Skip to content

Commit

Permalink
safer version of text2mecab (r9y9#4)
Browse files Browse the repository at this point in the history
* safer version of text2mecab

* guard strlen(NULL)

* prohibit 0-length output for text2mecab_s

* merge text2mecab_s to original text2mecab

* Update src/text2mecab/text2mecab.c

Co-authored-by: Hiroshiba <hihokaruta@gmail.com>
  • Loading branch information
Yosshi999 and Hiroshiba authored Apr 13, 2022
1 parent 0eb5a84 commit d74d20a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/bin/open_jtalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ static int Open_JTalk_synthesis(Open_JTalk * open_jtalk, const char *txt, FILE *
int result = 0;
char buff[MAXBUFLEN];

text2mecab(buff, txt);
errno_t mecab_result = text2mecab(buff, MAXBUFLEN, txt);
if (mecab_result != 0) {
return 0;
}
Mecab_analysis(&open_jtalk->mecab, buff);
mecab2njd(&open_jtalk->njd, Mecab_get_feature(&open_jtalk->mecab),
Mecab_get_size(&open_jtalk->mecab));
Expand Down
14 changes: 13 additions & 1 deletion src/text2mecab/text2mecab.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ static int strtopcmp(const char *str, const char *pattern)
}
}

void text2mecab(char *output, const char *input)
errno_t text2mecab(char *output, size_t sizeOfOutput, const char *input)
{
if (input == NULL || output == NULL || sizeOfOutput == 0)
return EINVAL;

int i, j;
const int length = strlen(input);
const char *str;
Expand All @@ -112,6 +115,10 @@ void text2mecab(char *output, const char *input)
/* convert */
s += e;
str = text2mecab_conv_list[i + 1];
if (index + strlen(str) >= sizeOfOutput) {
output[0] = '\0';
return ERANGE;
}
for (j = 0; str[j] != '\0'; j++)
output[index++] = str[j];
} else if (text2mecab_control_range[0] <= str[0] && str[0] <= text2mecab_control_range[1]) {
Expand All @@ -127,6 +134,10 @@ void text2mecab(char *output, const char *input)
}
}
if (e > 0) {
if (index + e >= sizeOfOutput) {
output[0] = '\0';
return ERANGE;
}
for (j = 0; j < e; j++)
output[index++] = input[s++];
} else {
Expand All @@ -137,6 +148,7 @@ void text2mecab(char *output, const char *input)
}
}
output[index] = '\0';
return 0;
}

TEXT2MECAB_C_END;
Expand Down
2 changes: 1 addition & 1 deletion src/text2mecab/text2mecab.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

TEXT2MECAB_H_START;

void text2mecab(char *output, const char *input);
errno_t text2mecab(char *output, size_t sizeOfOutput, const char *input);

TEXT2MECAB_H_END;

Expand Down

0 comments on commit d74d20a

Please sign in to comment.