From bf0f323fb47c34f392a09c3bc1359a82e8339959 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Wed, 5 Feb 2020 19:45:27 +0000 Subject: [PATCH] Fix minor memory leak in psftp batch mode. Spotted by Leak Sanitiser, while I was investigating the PSFTP / proftpd issue mentioned in the previous commit (with ASan on as usual). The two very similar loops that read PSFTP commands from the interactive prompt and a batch file differed in one respect: only one of them remembered to free the command afterwards. Now I've moved the freeing code out into a subroutine that both loops can use. --- psftp.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/psftp.c b/psftp.c index c30e6fbd..efb5bbff 100644 --- a/psftp.c +++ b/psftp.c @@ -2307,6 +2307,16 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags) return cmd; } +static void sftp_cmd_free(struct sftp_command *cmd) +{ + if (cmd->words) { + for (size_t i = 0; i < cmd->nwords; i++) + sfree(cmd->words[i]); + sfree(cmd->words); + } + sfree(cmd); +} + static int do_sftp_init(void) { struct sftp_packet *pktin; @@ -2381,13 +2391,7 @@ int do_sftp(int mode, int modeflags, char *batchfile) if (!cmd) break; ret = cmd->obey(cmd); - if (cmd->words) { - int i; - for(i = 0; i < cmd->nwords; i++) - sfree(cmd->words[i]); - sfree(cmd->words); - } - sfree(cmd); + sftp_cmd_free(cmd); if (ret < 0) break; } @@ -2404,6 +2408,7 @@ int do_sftp(int mode, int modeflags, char *batchfile) if (!cmd) break; ret = cmd->obey(cmd); + sftp_cmd_free(cmd); if (ret < 0) break; if (ret == 0) {