From fbd2e679fc1afa3c6325358ff986eb8c156ee3e9 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Sun, 21 May 2017 12:44:10 +0100 Subject: [PATCH] Don't crash if loading the default voice fails. --- src/libespeak-ng/speech.c | 9 ++++++--- tests/api.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/libespeak-ng/speech.c b/src/libespeak-ng/speech.c index 3427e51c..bd8cd294 100644 --- a/src/libespeak-ng/speech.c +++ b/src/libespeak-ng/speech.c @@ -398,13 +398,16 @@ static espeak_ng_STATUS Synthesize(unsigned int unique_identifier, const void *t count_samples = 0; - if (translator == NULL) - espeak_SetVoiceByName("en"); + espeak_ng_STATUS status; + if (translator == NULL) { + status = espeak_SetVoiceByName("en"); + if (status != ENS_OK) + return status; + } if (p_decoder == NULL) p_decoder = create_text_decoder(); - espeak_ng_STATUS status; status = text_decoder_decode_string_multibyte(p_decoder, text, translator->encoding, flags); if (status != ENS_OK) return status; diff --git a/tests/api.c b/tests/api.c index 84362206..b7cdacce 100644 --- a/tests/api.c +++ b/tests/api.c @@ -95,14 +95,51 @@ test_espeak_synth() assert(p_decoder == NULL); } +void +test_espeak_synth_no_voices(const char *path) +{ + printf("testing espeak_Synth in path with no voices\n"); + + assert(event_list == NULL); + assert(translator == NULL); + assert(p_decoder == NULL); + + assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, path, espeakINITIALIZE_DONT_EXIT) == 22050); + assert(event_list != NULL); + assert(translator == NULL); + assert(p_decoder == NULL); + + const char *test = "One two three."; + assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_INTERNAL_ERROR); + assert(translator == NULL); + assert(p_decoder == NULL); + + assert(espeak_Synchronize() == EE_OK); + assert(translator == NULL); + assert(p_decoder == NULL); + + assert(espeak_Terminate() == EE_OK); + assert(event_list == NULL); + assert(translator == NULL); + assert(p_decoder == NULL); +} + int main(int argc, char **argv) { + char *progdir = strdup(argv[0]); + char *dir = strrchr(progdir, '/'); + if (dir != NULL) *dir = 0; + test_espeak_terminate_without_initialize(); test_espeak_initialize(); test_espeak_synth(); test_espeak_synth(); // Check that this does not crash when run a second time. + test_espeak_synth_no_voices(progdir); + test_espeak_synth(); + + free(progdir); return EXIT_SUCCESS; }