Bug Summary

File:obj-scan-build/libftpconn/../../libftpconn/cwd.c
Location:line 44, column 12
Description:Memory is never released; potential leak of memory pointed to by 'cwd'

Annotated Source Code

1/* Get/set connection current working directory
2
3 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
4
5 Written by Miles Bader <miles@gnu.ai.mit.edu>
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21#include <unistd.h>
22#include <errno(*__errno_location ()).h>
23#include <string.h>
24
25#include <ftpconn.h>
26#include "priv.h"
27
28static error_t
29_cache_cwd (struct ftp_conn *conn, int reopen)
30{
31 int reply;
32 const char *txt;
33 error_t err =
34 (reopen ? ftp_conn_cmd_reopen : ftp_conn_cmd) (conn, "pwd", 0, &reply, &txt);
3
'?' condition is true
35
36 if (! err)
4
Assuming 'err' is 0
5
Taking true branch
37 {
38 if (reply == REPLY_DIR_NAME257)
6
Assuming 'reply' is equal to 257
7
Taking true branch
39 {
40 char *cwd = malloc (strlen (txt));
8
Memory is allocated
41 if (! cwd)
9
Assuming 'cwd' is non-null
10
Taking false branch
42 err = ENOMEM((0x10 << 26) | ((12) & 0x3fff));
43 else if (sscanf (txt, "\"%[^\"]\"", cwd) != 1)
11
Taking true branch
44 err = EGRATUITOUS((0x10 << 26) | ((105) & 0x3fff));
12
Within the expansion of the macro 'EGRATUITOUS':
a
Memory is never released; potential leak of memory pointed to by 'cwd'
45 else
46 {
47 if (conn->cwd)
48 free (conn->cwd);
49 conn->cwd = cwd;
50 }
51 }
52 else
53 err = unexpected_reply (conn, reply, txt, 0);
54 }
55
56 return err;
57}
58
59/* Return a malloced string containing CONN's working directory in CWD. */
60error_t
61ftp_conn_get_cwd (struct ftp_conn *conn, char **cwd)
62{
63 error_t err = 0;
64 if (! conn->cwd)
1
Taking true branch
65 err = _cache_cwd (conn, 1);
2
Calling '_cache_cwd'
66 if (! err)
67 {
68 *cwd = strdup (conn->cwd);
69 if (! *cwd)
70 err = ENOMEM((0x10 << 26) | ((12) & 0x3fff));
71 }
72 return err;
73}
74
75/* Return a malloced string containing CONN's working directory in CWD. */
76error_t
77ftp_conn_cwd (struct ftp_conn *conn, const char *cwd)
78{
79 error_t err = 0;
80 if (conn->cwd && strcmp (conn->cwd, cwd) == 0)
81 err = 0;
82 else
83 {
84 int reply;
85 const char *txt;
86 err = ftp_conn_cmd_reopen (conn, "cwd", cwd, &reply, &txt);
87 if (! err)
88 {
89 if (reply == REPLY_FCMD_OK250)
90 err = _cache_cwd (conn, 0);
91 else
92 err = unexpected_reply (conn, reply, txt, ftp_conn_poss_file_errs);
93 }
94 }
95 return err;
96}
97
98/* Return a malloced string containing CONN's working directory in CWD. */
99error_t
100ftp_conn_cdup (struct ftp_conn *conn)
101{
102 int reply;
103 const char *txt;
104 error_t err = ftp_conn_cmd_reopen (conn, "cdup", 0, &reply, &txt);
105 if (! err)
106 {
107 if (reply == REPLY_OK200)
108 err = _cache_cwd (conn, 0);
109 else
110 err = unexpected_reply (conn, reply, txt, ftp_conn_poss_file_errs);
111 }
112 return err;
113}