From 612743192e317aadc6c0b7a91100ed2bca244c38 Mon Sep 17 00:00:00 2001 From: "Andrew M. Kuchling" Date: Tue, 3 Oct 2006 18:29:35 +0000 Subject: [Backport r50677 | neal.norwitz] Fix memory leaks in some conditions. Reported by Klocwork #152. --- Modules/readline.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Modules/readline.c b/Modules/readline.c index a01935b..5094bf2 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -22,6 +22,12 @@ #include #endif +#ifdef SAVE_LOCALE +# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); } +#else +# define RESTORE_LOCALE(sl) +#endif + /* GNU readline definitions */ #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */ #include @@ -725,10 +731,7 @@ setup_readline(void) */ rl_initialize(); -#ifdef SAVE_LOCALE - setlocale(LC_CTYPE, saved_locale); /* Restore locale */ - free(saved_locale); -#endif + RESTORE_LOCALE(saved_locale) } /* Wrapper around GNU readline that handles signals differently. */ @@ -866,7 +869,8 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) p = readline_until_enter_or_signal(prompt, &signal); /* we got an interrupt signal */ - if(signal) { + if (signal) { + RESTORE_LOCALE(saved_locale) return NULL; } @@ -875,6 +879,7 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) p = PyMem_Malloc(1); if (p != NULL) *p = '\0'; + RESTORE_LOCALE(saved_locale) return p; } @@ -907,10 +912,7 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) p[n+1] = '\0'; } free(q); -#ifdef SAVE_LOCALE - setlocale(LC_CTYPE, saved_locale); /* Restore locale */ - free(saved_locale); -#endif + RESTORE_LOCALE(saved_locale) return p; } -- cgit v0.12