Skip to content

Commit d74d20a

Browse files
Yosshi999Hiroshiba
andauthored
safer version of text2mecab (r9y9#4)
* 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 <[email protected]>
1 parent 0eb5a84 commit d74d20a

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/bin/open_jtalk.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ static int Open_JTalk_synthesis(Open_JTalk * open_jtalk, const char *txt, FILE *
172172
int result = 0;
173173
char buff[MAXBUFLEN];
174174

175-
text2mecab(buff, txt);
175+
errno_t mecab_result = text2mecab(buff, MAXBUFLEN, txt);
176+
if (mecab_result != 0) {
177+
return 0;
178+
}
176179
Mecab_analysis(&open_jtalk->mecab, buff);
177180
mecab2njd(&open_jtalk->njd, Mecab_get_feature(&open_jtalk->mecab),
178181
Mecab_get_size(&open_jtalk->mecab));

src/text2mecab/text2mecab.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ static int strtopcmp(const char *str, const char *pattern)
9292
}
9393
}
9494

95-
void text2mecab(char *output, const char *input)
95+
errno_t text2mecab(char *output, size_t sizeOfOutput, const char *input)
9696
{
97+
if (input == NULL || output == NULL || sizeOfOutput == 0)
98+
return EINVAL;
99+
97100
int i, j;
98101
const int length = strlen(input);
99102
const char *str;
@@ -112,6 +115,10 @@ void text2mecab(char *output, const char *input)
112115
/* convert */
113116
s += e;
114117
str = text2mecab_conv_list[i + 1];
118+
if (index + strlen(str) >= sizeOfOutput) {
119+
output[0] = '\0';
120+
return ERANGE;
121+
}
115122
for (j = 0; str[j] != '\0'; j++)
116123
output[index++] = str[j];
117124
} else if (text2mecab_control_range[0] <= str[0] && str[0] <= text2mecab_control_range[1]) {
@@ -127,6 +134,10 @@ void text2mecab(char *output, const char *input)
127134
}
128135
}
129136
if (e > 0) {
137+
if (index + e >= sizeOfOutput) {
138+
output[0] = '\0';
139+
return ERANGE;
140+
}
130141
for (j = 0; j < e; j++)
131142
output[index++] = input[s++];
132143
} else {
@@ -137,6 +148,7 @@ void text2mecab(char *output, const char *input)
137148
}
138149
}
139150
output[index] = '\0';
151+
return 0;
140152
}
141153

142154
TEXT2MECAB_C_END;

src/text2mecab/text2mecab.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
TEXT2MECAB_H_START;
5353

54-
void text2mecab(char *output, const char *input);
54+
errno_t text2mecab(char *output, size_t sizeOfOutput, const char *input);
5555

5656
TEXT2MECAB_H_END;
5757

0 commit comments

Comments
 (0)