1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
From 4714c177e2fde09cfa76609ccaf9c72d62f39bfd Mon Sep 17 00:00:00 2001
From: Michael Forney <mforney@mforney.org>
Date: Sun, 5 Jun 2016 17:42:29 -0700
Subject: [PATCH] Use standard C functions
---
sshfs.c | 101 ++++++++++++++++++++++++++++++++++++++--------------------------
1 file changed, 60 insertions(+), 41 deletions(-)
diff --git a/sshfs.c b/sshfs.c
index 14adca5..4f3bfe4 100644
--- a/sshfs.c
+++ b/sshfs.c
@@ -637,25 +637,25 @@ static inline void buf_add_path(struct buffer *buf, const char *path)
if (sshfs.base_path[0]) {
if (path[1]) {
if (sshfs.base_path[strlen(sshfs.base_path)-1] != '/') {
- realpath = g_strdup_printf("%s/%s",
- sshfs.base_path,
- path + 1);
+ if (asprintf(&realpath, "%s/%s", sshfs.base_path, path + 1) < 0)
+ abort();
} else {
- realpath = g_strdup_printf("%s%s",
- sshfs.base_path,
- path + 1);
+ if (asprintf(&realpath, "%s%s", sshfs.base_path, path + 1) < 0)
+ abort();
}
} else {
- realpath = g_strdup(sshfs.base_path);
+ realpath = strdup(sshfs.base_path);
}
} else {
if (path[1])
- realpath = g_strdup(path + 1);
+ realpath = strdup(path + 1);
else
- realpath = g_strdup(".");
+ realpath = strdup(".");
}
+ if (!realpath)
+ abort();
buf_add_string(buf, realpath);
- g_free(realpath);
+ free(realpath);
}
static int buf_check_get(struct buffer *buf, size_t len)
@@ -948,10 +948,9 @@ nobundle:
pathok:
#endif
- newpreload = g_strdup_printf("%s%s%s",
- oldpreload ? oldpreload : "",
- oldpreload ? " " : "",
- sopath);
+ if (asprintf(&newpreload, "%s%s%s",
+ oldpreload ? oldpreload : "", oldpreload ? " " : "", sopath) < 0)
+ abort();
#ifdef __APPLE__
if (!newpreload || setenv("DYLD_INSERT_LIBRARIES", newpreload, 1) == -1)
@@ -962,7 +961,7 @@ pathok:
"for ssh nodelay workaround\n");
}
#endif /* __APPLE__ */
- g_free(newpreload);
+ free(newpreload);
return 0;
}
#endif
@@ -1342,7 +1341,7 @@ static void request_free(struct request *req)
{
buf_free(&req->reply);
sem_destroy(&req->ready);
- g_free(req);
+ free(req);
}
static int request_table_insert(struct request_table *reqtab, struct request *req)
@@ -1390,9 +1389,9 @@ static void chunk_free(struct read_chunk *chunk)
rreq = list_entry(chunk->reqs.prev, struct read_req, list);
list_del(&rreq->list);
buf_free(&rreq->data);
- g_free(rreq);
+ free(rreq);
}
- g_free(chunk);
+ free(chunk);
}
static void chunk_put(struct read_chunk *chunk)
@@ -1944,8 +1943,10 @@ static int sftp_request_send(uint8_t type, struct iovec *iov, size_t count,
struct request **reqp)
{
int err;
- struct request *req = g_new0(struct request, 1);
+ struct request *req = calloc(1, sizeof(struct request));
+ if (!req)
+ return -ENOMEM;
req->want_reply = want_reply;
req->end_func = end_func;
req->data = data;
@@ -2586,7 +2587,9 @@ static int sshfs_open_common(const char *path, mode_t mode,
if (fi->flags & O_TRUNC)
pflags |= SSH_FXF_TRUNC;
- sf = g_new0(struct sshfs_file, 1);
+ sf = calloc(1, sizeof(struct sshfs_file));
+ if (!sf)
+ return -ENOMEM;
list_init(&sf->write_reqs);
pthread_cond_init(&sf->write_finished, NULL);
/* Assume random read after open */
@@ -2628,7 +2631,7 @@ static int sshfs_open_common(const char *path, mode_t mode,
fi->fh = (unsigned long) sf;
} else {
cache_invalidate(path);
- g_free(sf);
+ free(sf);
}
buf_free(&buf);
return err;
@@ -2701,7 +2704,7 @@ static void sshfs_file_put(struct sshfs_file *sf)
{
sf->refs--;
if (!sf->refs)
- g_free(sf);
+ free(sf);
}
static void sshfs_file_get(struct sshfs_file *sf)
@@ -2771,9 +2774,11 @@ static void sshfs_read_begin(struct request *req)
static struct read_chunk *sshfs_send_read(struct sshfs_file *sf, size_t size,
off_t offset)
{
- struct read_chunk *chunk = g_new0(struct read_chunk, 1);
+ struct read_chunk *chunk = calloc(1, sizeof(struct read_chunk));
struct buffer *handle = &sf->handle;
+ if (!chunk)
+ abort();
pthread_cond_init(&chunk->sio.finished, NULL);
list_init(&chunk->reqs);
chunk->size = size;
@@ -2787,7 +2792,9 @@ static struct read_chunk *sshfs_send_read(struct sshfs_file *sf, size_t size,
struct read_req *rreq;
size_t bsize = size < sshfs.max_read ? size : sshfs.max_read;
- rreq = g_new0(struct read_req, 1);
+ rreq = calloc(1, sizeof(struct read_req));
+ if (!rreq)
+ abort();
rreq->sio = &chunk->sio;
rreq->size = bsize;
buf_init(&rreq->data, 0);
@@ -2858,7 +2865,7 @@ static int wait_chunk(struct read_chunk *chunk, char *buf, size_t size)
size -= rreq->res;
list_del(&rreq->list);
buf_free(&rreq->data);
- g_free(rreq);
+ free(rreq);
}
}
@@ -3513,9 +3520,10 @@ static int sshfs_opt_proc(void *data, const char *arg, int key,
switch (key) {
case FUSE_OPT_KEY_OPT:
if (is_ssh_opt(arg)) {
- tmp = g_strdup_printf("-o%s", arg);
+ if (asprintf(&tmp, "-o%s", arg) < 0)
+ abort();
ssh_add_arg(tmp);
- g_free(tmp);
+ free(tmp);
return 0;
}
return 1;
@@ -3528,9 +3536,10 @@ static int sshfs_opt_proc(void *data, const char *arg, int key,
return 1;
case KEY_PORT:
- tmp = g_strdup_printf("-oPort=%s", arg + 2);
+ if (asprintf(&tmp, "-oPort=%s", arg + 2) < 0)
+ abort();
ssh_add_arg(tmp);
- g_free(tmp);
+ free(tmp);
return 0;
case KEY_COMPRESS:
@@ -3538,9 +3547,10 @@ static int sshfs_opt_proc(void *data, const char *arg, int key,
return 0;
case KEY_CONFIGFILE:
- tmp = g_strdup_printf("-F%s", arg + 2);
+ if (asprintf(&tmp, "-F%s", arg + 2) < 0)
+ abort();
ssh_add_arg(tmp);
- g_free(tmp);
+ free(tmp);
return 0;
case KEY_HELP:
@@ -3755,17 +3765,19 @@ static void fsname_remove_commas(char *fsname)
#if FUSE_VERSION >= 27
static char *fsname_escape_commas(char *fsnameold)
{
- char *fsname = g_malloc(strlen(fsnameold) * 2 + 1);
+ char *fsname = malloc(strlen(fsnameold) * 2 + 1);
char *d = fsname;
char *s;
+ if (!fsname)
+ abort();
for (s = fsnameold; *s; s++) {
if (*s == '\\' || *s == ',')
*d++ = '\\';
*d++ = *s;
}
*d = '\0';
- g_free(fsnameold);
+ free(fsnameold);
return fsname;
}
@@ -4081,15 +4093,20 @@ int main(int argc, char *argv[])
exit(1);
}
- fsname = g_strdup(sshfs.host);
- sshfs.base_path = g_strdup(find_base_path());
+ fsname = strdup(sshfs.host);
+ if (!fsname)
+ abort();
+ sshfs.base_path = strdup(find_base_path());
+ if (!sshfs.base_path)
+ abort();
if (sshfs.ssh_command)
set_ssh_command();
- tmp = g_strdup_printf("-%i", sshfs.ssh_ver);
+ if (asprintf(&tmp, "-%i", sshfs.ssh_ver) < 0)
+ abort();
ssh_add_arg(tmp);
- g_free(tmp);
+ free(tmp);
ssh_add_arg(sshfs.host);
if (sshfs.sftp_server)
sftp_server = sshfs.sftp_server;
@@ -4125,14 +4142,16 @@ int main(int argc, char *argv[])
fsname = fsname_escape_commas(fsname);
else
fsname_remove_commas(fsname);
- tmp = g_strdup_printf("-osubtype=sshfs,fsname=%s", fsname);
+ if (asprintf(&tmp, "-osubtype=sshfs,fsname=%s", fsname) < 0)
+ abort();
#else
fsname_remove_commas(fsname);
- tmp = g_strdup_printf("-ofsname=sshfs#%s", fsname);
+ if (asprintf(&tmp, "-ofsname=sshfs#%s", fsname) < 0)
+ abort();
#endif
fuse_opt_insert_arg(&args, 1, tmp);
- g_free(tmp);
- g_free(fsname);
+ free(tmp);
+ free(fsname);
check_large_read(&args);
#if FUSE_VERSION >= 26
--
2.9.0
|