logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

0005-Use-standard-C-functions.patch (6657B)


  1. From af353da1558da816a014983d3abaafaa4929e04c Mon Sep 17 00:00:00 2001
  2. From: Michael Forney <mforney@mforney.org>
  3. Date: Sun, 5 Jun 2016 17:42:29 -0700
  4. Subject: [PATCH] Use standard C functions
  5. ---
  6. sshfs.c | 91 ++++++++++++++++++++++++++++++++++-----------------------
  7. 1 file changed, 55 insertions(+), 36 deletions(-)
  8. diff --git a/sshfs.c b/sshfs.c
  9. index 9c074a3..17f7d6a 100644
  10. --- a/sshfs.c
  11. +++ b/sshfs.c
  12. @@ -671,25 +671,25 @@ static inline void buf_add_path(struct buffer *buf, const char *path)
  13. if (sshfs.base_path[0]) {
  14. if (path[1]) {
  15. if (sshfs.base_path[strlen(sshfs.base_path)-1] != '/') {
  16. - realpath = g_strdup_printf("%s/%s",
  17. - sshfs.base_path,
  18. - path + 1);
  19. + if (asprintf(&realpath, "%s/%s", sshfs.base_path, path + 1) < 0)
  20. + abort();
  21. } else {
  22. - realpath = g_strdup_printf("%s%s",
  23. - sshfs.base_path,
  24. - path + 1);
  25. + if (asprintf(&realpath, "%s%s", sshfs.base_path, path + 1) < 0)
  26. + abort();
  27. }
  28. } else {
  29. - realpath = g_strdup(sshfs.base_path);
  30. + realpath = strdup(sshfs.base_path);
  31. }
  32. } else {
  33. if (path[1])
  34. - realpath = g_strdup(path + 1);
  35. + realpath = strdup(path + 1);
  36. else
  37. - realpath = g_strdup(".");
  38. + realpath = strdup(".");
  39. }
  40. + if (!realpath)
  41. + abort();
  42. buf_add_string(buf, realpath);
  43. - g_free(realpath);
  44. + free(realpath);
  45. }
  46. static int buf_check_get(struct buffer *buf, size_t len)
  47. @@ -1272,7 +1272,7 @@ static void request_free(struct request *req)
  48. {
  49. buf_free(&req->reply);
  50. sem_destroy(&req->ready);
  51. - g_free(req);
  52. + free(req);
  53. }
  54. static int request_table_insert(struct request_table *reqtab, struct request *req)
  55. @@ -1320,9 +1320,9 @@ static void chunk_free(struct read_chunk *chunk)
  56. rreq = list_entry(chunk->reqs.prev, struct read_req, list);
  57. list_del(&rreq->list);
  58. buf_free(&rreq->data);
  59. - g_free(rreq);
  60. + free(rreq);
  61. }
  62. - g_free(chunk);
  63. + free(chunk);
  64. }
  65. static void chunk_put(struct read_chunk *chunk)
  66. @@ -1871,8 +1871,10 @@ static int sftp_request_send(uint8_t type, struct iovec *iov, size_t count,
  67. struct request **reqp)
  68. {
  69. int err;
  70. - struct request *req = g_new0(struct request, 1);
  71. + struct request *req = calloc(1, sizeof(struct request));
  72. + if (!req)
  73. + return -ENOMEM;
  74. req->want_reply = want_reply;
  75. req->end_func = end_func;
  76. req->data = data;
  77. @@ -2579,8 +2581,10 @@ static int sshfs_open_common(const char *path, mode_t mode,
  78. if (fi->flags & O_APPEND)
  79. pflags |= SSH_FXF_APPEND;
  80. -
  81. - sf = g_new0(struct sshfs_file, 1);
  82. +
  83. + sf = calloc(1, sizeof(struct sshfs_file));
  84. + if (!sf)
  85. + return -ENOMEM;
  86. list_init(&sf->write_reqs);
  87. pthread_cond_init(&sf->write_finished, NULL);
  88. /* Assume random read after open */
  89. @@ -2624,7 +2628,7 @@ static int sshfs_open_common(const char *path, mode_t mode,
  90. } else {
  91. if (sshfs.dir_cache)
  92. cache_invalidate(path);
  93. - g_free(sf);
  94. + free(sf);
  95. }
  96. buf_free(&buf);
  97. return err;
  98. @@ -2691,7 +2695,7 @@ static void sshfs_file_put(struct sshfs_file *sf)
  99. {
  100. sf->refs--;
  101. if (!sf->refs)
  102. - g_free(sf);
  103. + free(sf);
  104. }
  105. static void sshfs_file_get(struct sshfs_file *sf)
  106. @@ -2761,9 +2765,11 @@ static void sshfs_read_begin(struct request *req)
  107. static struct read_chunk *sshfs_send_read(struct sshfs_file *sf, size_t size,
  108. off_t offset)
  109. {
  110. - struct read_chunk *chunk = g_new0(struct read_chunk, 1);
  111. + struct read_chunk *chunk = calloc(1, sizeof(struct read_chunk));
  112. struct buffer *handle = &sf->handle;
  113. + if (!chunk)
  114. + abort();
  115. pthread_cond_init(&chunk->sio.finished, NULL);
  116. list_init(&chunk->reqs);
  117. chunk->size = size;
  118. @@ -2777,7 +2783,9 @@ static struct read_chunk *sshfs_send_read(struct sshfs_file *sf, size_t size,
  119. struct read_req *rreq;
  120. size_t bsize = size < sshfs.max_read ? size : sshfs.max_read;
  121. - rreq = g_new0(struct read_req, 1);
  122. + rreq = calloc(1, sizeof(struct read_req));
  123. + if (!rreq)
  124. + abort();
  125. rreq->sio = &chunk->sio;
  126. rreq->size = bsize;
  127. buf_init(&rreq->data, 0);
  128. @@ -2848,7 +2856,7 @@ static int wait_chunk(struct read_chunk *chunk, char *buf, size_t size)
  129. size -= rreq->res;
  130. list_del(&rreq->list);
  131. buf_free(&rreq->data);
  132. - g_free(rreq);
  133. + free(rreq);
  134. }
  135. }
  136. @@ -3473,9 +3481,10 @@ static int sshfs_opt_proc(void *data, const char *arg, int key,
  137. switch (key) {
  138. case FUSE_OPT_KEY_OPT:
  139. if (is_ssh_opt(arg)) {
  140. - tmp = g_strdup_printf("-o%s", arg);
  141. + if (asprintf(&tmp, "-o%s", arg) < 0)
  142. + abort();
  143. ssh_add_arg(tmp);
  144. - g_free(tmp);
  145. + free(tmp);
  146. return 0;
  147. }
  148. /* Pass through */
  149. @@ -3544,9 +3553,10 @@ static int sshfs_opt_proc(void *data, const char *arg, int key,
  150. case KEY_PORT:
  151. - tmp = g_strdup_printf("-oPort=%s", arg + 2);
  152. + if (asprintf(&tmp, "-oPort=%s", arg + 2) < 0)
  153. + abort();
  154. ssh_add_arg(tmp);
  155. - g_free(tmp);
  156. + free(tmp);
  157. return 0;
  158. case KEY_COMPRESS:
  159. @@ -3554,9 +3564,10 @@ static int sshfs_opt_proc(void *data, const char *arg, int key,
  160. return 0;
  161. case KEY_CONFIGFILE:
  162. - tmp = g_strdup_printf("-F%s", arg + 2);
  163. + if (asprintf(&tmp, "-F%s", arg + 2) < 0)
  164. + abort();
  165. ssh_add_arg(tmp);
  166. - g_free(tmp);
  167. + free(tmp);
  168. return 0;
  169. default:
  170. @@ -3732,17 +3743,19 @@ static char *find_base_path(void)
  171. static char *fsname_escape_commas(char *fsnameold)
  172. {
  173. - char *fsname = g_malloc(strlen(fsnameold) * 2 + 1);
  174. + char *fsname = malloc(strlen(fsnameold) * 2 + 1);
  175. char *d = fsname;
  176. char *s;
  177. + if (!fsname)
  178. + abort();
  179. for (s = fsnameold; *s; s++) {
  180. if (*s == '\\' || *s == ',')
  181. *d++ = '\\';
  182. *d++ = *s;
  183. }
  184. *d = '\0';
  185. - g_free(fsnameold);
  186. + free(fsnameold);
  187. return fsname;
  188. }
  189. @@ -4083,15 +4096,20 @@ int main(int argc, char *argv[])
  190. else
  191. sshfs.max_outstanding_len = ~0;
  192. - fsname = g_strdup(sshfs.host);
  193. - sshfs.base_path = g_strdup(find_base_path());
  194. + fsname = strdup(sshfs.host);
  195. + if (!fsname)
  196. + abort();
  197. + sshfs.base_path = strdup(find_base_path());
  198. + if (!sshfs.base_path)
  199. + abort();
  200. if (sshfs.ssh_command)
  201. set_ssh_command();
  202. - tmp = g_strdup_printf("-%i", sshfs.ssh_ver);
  203. + if (asprintf(&tmp, "-%i", sshfs.ssh_ver) < 0)
  204. + abort();
  205. ssh_add_arg(tmp);
  206. - g_free(tmp);
  207. + free(tmp);
  208. ssh_add_arg(sshfs.host);
  209. if (sshfs.sftp_server)
  210. sftp_server = sshfs.sftp_server;
  211. @@ -4118,10 +4136,11 @@ int main(int argc, char *argv[])
  212. sshfs.max_write = 65536;
  213. fsname = fsname_escape_commas(fsname);
  214. - tmp = g_strdup_printf("-osubtype=sshfs,fsname=%s", fsname);
  215. + if (asprintf(&tmp, "-osubtype=sshfs,fsname=%s", fsname) < 0)
  216. + abort();
  217. fuse_opt_insert_arg(&args, 1, tmp);
  218. - g_free(tmp);
  219. - g_free(fsname);
  220. + free(tmp);
  221. + free(fsname);
  222. if(sshfs.dir_cache)
  223. sshfs.op = cache_wrap(&sshfs_oper);
  224. --
  225. 2.24.0