Changed to support memory allocation routines that can fail.
authorPekka Riikonen <priikone@silcnet.org>
Fri, 3 May 2002 17:52:23 +0000 (17:52 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Fri, 3 May 2002 17:52:23 +0000 (17:52 +0000)
lib/silcsftp/sftp_client.c
lib/silcsftp/sftp_fs_memory.c
lib/silcsftp/sftp_server.c
lib/silcsftp/sftp_util.c
lib/silcsftp/silcsftp.h

index 0b4ee1647345499ecdd2f39a847b02f2f8d05fce..1dfb9cf8984c1cc615bfe443653bf48e55582356 100644 (file)
@@ -63,7 +63,11 @@ static SilcSFTPHandle silc_sftp_handle_create(unsigned char *data,
   SilcSFTPHandle handle;
 
   handle = silc_calloc(1, sizeof(*handle));
+  if (!handle)
+    return NULL;
   handle->data = silc_calloc(data_len, sizeof(*handle->data));
+  if (!handle->data)
+    return NULL;
   memcpy(handle->data, data, data_len);
   handle->data_len = data_len;
 
@@ -189,6 +193,11 @@ static void silc_sftp_call_request(SilcSFTPClient sftp,
       hdata = (unsigned char *)va_arg(vp, unsigned char *);
       hdata_len = (SilcUInt32)va_arg(vp, SilcUInt32);
       handle = silc_sftp_handle_create(hdata, hdata_len);
+      if (!handle) {
+       if (req->handle)
+         (*req->handle)((SilcSFTP)sftp, status, NULL, req->context);
+       break;
+      }
 
       if (req->handle)
        (*req->handle)((SilcSFTP)sftp, status, handle, req->context);
@@ -307,6 +316,8 @@ SilcSFTP silc_sftp_client_start(SilcSFTPSendPacketCallback send_packet,
     return NULL;
 
   sftp = silc_calloc(1, sizeof(*sftp));
+  if (!sftp)
+    return NULL;
   sftp->send_packet = send_packet;
   sftp->send_context = send_context;
   sftp->version = callback;
@@ -581,6 +592,8 @@ void silc_sftp_open(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Open request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_OPEN;
   req->handle = callback;
@@ -588,6 +601,8 @@ void silc_sftp_open(SilcSFTP sftp,
   silc_list_add(client->requests, req);
 
   attrs_buf = silc_sftp_attr_encode(attrs);
+  if (attrs_buf)
+    return;
   len = 4 + 4 + strlen(filename) + 4 + attrs_buf->len;
 
   silc_sftp_send_packet(client, req->type, len, 
@@ -616,6 +631,8 @@ void silc_sftp_close(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Close request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_CLOSE;
   req->status = callback;
@@ -648,6 +665,8 @@ void silc_sftp_read(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Read request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_READ;
   req->data = callback;
@@ -683,6 +702,8 @@ void silc_sftp_write(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Write request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_WRITE;
   req->status = callback;
@@ -714,6 +735,8 @@ void silc_sftp_remove(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Remove request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_REMOVE;
   req->status = callback;
@@ -742,6 +765,8 @@ void silc_sftp_rename(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Rename request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_RENAME;
   req->status = callback;
@@ -773,6 +798,8 @@ void silc_sftp_mkdir(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Mkdir request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_MKDIR;
   req->status = callback;
@@ -780,6 +807,8 @@ void silc_sftp_mkdir(SilcSFTP sftp,
   silc_list_add(client->requests, req);
 
   attrs_buf = silc_sftp_attr_encode(attrs);
+  if (attrs_buf)
+    return;
   len = 4 + 4 + strlen(path) + attrs_buf->len;
 
   silc_sftp_send_packet(client, req->type, len, 
@@ -805,6 +834,8 @@ void silc_sftp_rmdir(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Rmdir request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_RMDIR;
   req->status = callback;
@@ -832,6 +863,8 @@ void silc_sftp_opendir(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Opendir request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_OPENDIR;
   req->handle = callback;
@@ -861,6 +894,8 @@ void silc_sftp_readdir(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Readdir request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_READDIR;
   req->name = callback;
@@ -889,6 +924,8 @@ void silc_sftp_stat(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Stat request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_STAT;
   req->attr = callback;
@@ -916,6 +953,8 @@ void silc_sftp_lstat(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Lstat request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_LSTAT;
   req->attr = callback;
@@ -945,6 +984,8 @@ void silc_sftp_fstat(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Fstat request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_FSTAT;
   req->attr = callback;
@@ -975,6 +1016,8 @@ void silc_sftp_setstat(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Setstat request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_SETSTAT;
   req->status = callback;
@@ -982,6 +1025,8 @@ void silc_sftp_setstat(SilcSFTP sftp,
   silc_list_add(client->requests, req);
 
   attrs_buf = silc_sftp_attr_encode(attrs);
+  if (attrs_buf)
+    return;
   len = 4 + 4 + strlen(path) + attrs_buf->len;
 
   silc_sftp_send_packet(client, req->type, len, 
@@ -1011,6 +1056,8 @@ void silc_sftp_fsetstat(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Fsetstat request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_FSETSTAT;
   req->status = callback;
@@ -1019,6 +1066,8 @@ void silc_sftp_fsetstat(SilcSFTP sftp,
 
   silc_sftp_handle_get(handle, &hdata, &hdata_len);
   attrs_buf = silc_sftp_attr_encode(attrs);
+  if (attrs_buf)
+    return;
   len = 4 + 4 + hdata_len + attrs_buf->len;
 
   silc_sftp_send_packet(client, req->type, len, 
@@ -1044,6 +1093,8 @@ void silc_sftp_readlink(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Readlink request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_READLINK;
   req->name = callback;
@@ -1072,6 +1123,8 @@ void silc_sftp_symlink(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Symlink request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_SYMLINK;
   req->status = callback;
@@ -1101,6 +1154,8 @@ void silc_sftp_realpath(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Realpath request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_REALPATH;
   req->name = callback;
@@ -1130,6 +1185,8 @@ void silc_sftp_extended(SilcSFTP sftp,
   SILC_LOG_DEBUG(("Extended request"));
 
   req = silc_calloc(1, sizeof(*req));
+  if (!req)
+    return;
   req->id = client->id++;
   req->type = SILC_SFTP_WRITE;
   req->extended = callback;
index 5ac53706a16739180c30dcf3fb4c145f09dd030b..ef6eb230c7c08defdba9c23c88b9d6d093a21555 100644 (file)
@@ -83,6 +83,8 @@ static bool mem_add_entry(MemFSEntry dir, MemFSEntry entry,
 
   if (!dir->entry) {
     dir->entry = silc_calloc(3, sizeof(*entry));
+    if (!dir->entry)
+      return FALSE;
     dir->entry[0] = entry;
     dir->entry_count = 3;
     entry->created = time(0);
@@ -100,6 +102,8 @@ static bool mem_add_entry(MemFSEntry dir, MemFSEntry entry,
 
   dir->entry = silc_realloc(dir->entry, sizeof(*dir->entry) *
                            (dir->entry_count + 3));
+  if (!dir->entry)
+    return FALSE;
   for (i = dir->entry_count + 1; i < dir->entry_count + 3; i++)
     dir->entry[i] = NULL;
   dir->entry[dir->entry_count] = entry;
@@ -228,11 +232,15 @@ static MemFSFileHandle mem_create_handle(MemFS fs, int fd, MemFSEntry entry)
   int i;
 
   handle = silc_calloc(1, sizeof(*handle));
+  if (!handle)
+    return NULL;
   handle->fd = fd;
   handle->entry = entry;
 
   if (!fs->handles) {
     fs->handles = silc_calloc(5, sizeof(*fs->handles));
+    if (!fs->handles)
+      return NULL;
     fs->handles[0] = handle;
     fs->handles_count = 5;
 
@@ -254,6 +262,8 @@ static MemFSFileHandle mem_create_handle(MemFS fs, int fd, MemFSEntry entry)
 
   fs->handles = silc_realloc(fs->handles, sizeof(*fs->handles) *
                             (fs->handles_count + 5));
+  if (!fs->handles)
+    return NULL;
   for (i = fs->handles_count + 1; i < fs->handles_count + 5; i++)
     fs->handles[i] = NULL;
   fs->handles[fs->handles_count] = handle;
@@ -312,15 +322,28 @@ SilcSFTPFilesystem silc_sftp_fs_memory_alloc(SilcSFTPFSMemoryPerm perm)
   MemFS fs;
 
   fs = silc_calloc(1, sizeof(*fs));
+  if (!fs)
+    return NULL;
+
   fs->root = silc_calloc(1, sizeof(*fs->root));
+  if (!fs->root) {
+    silc_free(fs);
+    return NULL;
+  }
+
   fs->root->perm = perm;
   fs->root_perm = perm;
   fs->root->directory = TRUE;
   fs->root->name = strdup(DIR_SEPARATOR);
 
   filesystem = silc_calloc(1, sizeof(*filesystem));
-  filesystem->fs = 
-    (struct SilcSFTPFilesystemOpsStruct *)&silc_sftp_fs_memory;
+  if (!filesystem) {
+    silc_free(fs->root);
+    silc_free(fs);
+    return NULL;
+  }
+
+  filesystem->fs = (struct SilcSFTPFilesystemOpsStruct *)&silc_sftp_fs_memory;
   filesystem->fs_context = (void *)fs;
 
   return filesystem;
@@ -353,6 +376,9 @@ void *silc_sftp_fs_memory_add_dir(SilcSFTPFilesystem fs, void *dir,
   MemFSEntry entry;
 
   entry = silc_calloc(1, sizeof(*entry));
+  if (!entry)
+    return NULL;
+
   entry->perm = perm;
   entry->name = strdup(name);
   entry->directory = TRUE;
@@ -383,6 +409,9 @@ bool silc_sftp_fs_memory_del_dir(SilcSFTPFilesystem fs, void *dir)
   ret = mem_del_entry(memfs->root, FALSE);
 
   memfs->root = silc_calloc(1, sizeof(*memfs->root));
+  if (!memfs->root)
+    return FALSE;
+
   memfs->root->perm = memfs->root_perm;
   memfs->root->directory = TRUE;
   memfs->root->name = strdup(DIR_SEPARATOR);
@@ -407,6 +436,9 @@ bool silc_sftp_fs_memory_add_file(SilcSFTPFilesystem fs, void *dir,
   MemFSEntry entry;
 
   entry = silc_calloc(1, sizeof(*entry));
+  if (!entry)
+    return FALSE;
+
   entry->perm = perm;
   entry->name = strdup(filename);
   entry->data = strdup(realpath);
@@ -452,9 +484,11 @@ unsigned char *mem_encode_handle(void *context, SilcSFTP sftp,
   MemFSFileHandle h = (MemFSFileHandle)handle;
 
   data = silc_calloc(4, sizeof(*data));
+  if (!data)
+    return NULL;
+
   SILC_PUT32_MSB(h->handle, data);
   *handle_len = 4;
-
   return data;
 }
 
@@ -524,8 +558,12 @@ void mem_open(void *context, SilcSFTP sftp,
 
   /* File opened, return handle */
   handle = mem_create_handle(fs, fd, entry);
-  (*callback)(sftp, SILC_SFTP_STATUS_OK, (SilcSFTPHandle)handle, 
-             callback_context);
+  if (handle)
+    (*callback)(sftp, SILC_SFTP_STATUS_OK, (SilcSFTPHandle)handle, 
+               callback_context);
+  else
+    (*callback)(sftp, SILC_SFTP_STATUS_PERMISSION_DENIED, NULL, 
+               callback_context);
 }
 
 void mem_close(void *context, SilcSFTP sftp,
@@ -565,6 +603,10 @@ void mem_read(void *context, SilcSFTP sftp,
     len = 32768;
 
   data = silc_malloc(len);
+  if (!data) {
+    (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, 0, callback_context);
+    return;
+  }
   lseek(h->fd, (off_t)offset, SEEK_SET);
 
   /* Attempt to read */
@@ -684,8 +726,12 @@ void mem_opendir(void *context, SilcSFTP sftp,
 
   /* Directory opened, return handle */
   handle = mem_create_handle(fs, 0, entry);
-  (*callback)(sftp, SILC_SFTP_STATUS_OK, (SilcSFTPHandle)handle, 
-             callback_context);
+  if (handle)
+    (*callback)(sftp, SILC_SFTP_STATUS_OK, (SilcSFTPHandle)handle, 
+               callback_context);
+  else
+    (*callback)(sftp, SILC_SFTP_STATUS_PERMISSION_DENIED, NULL, 
+               callback_context);
 }
 
 void mem_readdir(void *context, SilcSFTP sftp,
@@ -714,6 +760,10 @@ void mem_readdir(void *context, SilcSFTP sftp,
   }
 
   name = silc_calloc(1, sizeof(*name));
+  if (!name) {
+    (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, callback_context);
+    return;
+  }
   for (i = h->fd; i < 100 + h->fd; i++) {
     if (i >= h->entry->entry_count)
       break;
@@ -748,6 +798,10 @@ void mem_readdir(void *context, SilcSFTP sftp,
 
     /* Add attributes */
     attrs = silc_calloc(1, sizeof(*attrs));
+    if (!attrs) {
+      (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, callback_context);
+      return;
+    }
     attrs->flags = (SILC_SFTP_ATTR_SIZE |
                    SILC_SFTP_ATTR_UIDGID);
     attrs->size = filesize;
@@ -817,6 +871,10 @@ void mem_stat(void *context, SilcSFTP sftp,
   }
 
   attrs = silc_calloc(1, sizeof(*attrs));
+  if (!attrs) {
+    (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
+    return;
+  }    
   attrs->flags = (SILC_SFTP_ATTR_SIZE |
                  SILC_SFTP_ATTR_UIDGID |
                  SILC_SFTP_ATTR_ACMODTIME);
@@ -871,6 +929,10 @@ void mem_lstat(void *context, SilcSFTP sftp,
   }
 
   attrs = silc_calloc(1, sizeof(*attrs));
+  if (!attrs) {
+    (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
+    return;
+  }    
   attrs->flags = (SILC_SFTP_ATTR_SIZE |
                  SILC_SFTP_ATTR_UIDGID |
                  SILC_SFTP_ATTR_ACMODTIME);
@@ -910,6 +972,10 @@ void mem_fstat(void *context, SilcSFTP sftp,
   }
 
   attrs = silc_calloc(1, sizeof(*attrs));
+  if (!attrs) {
+    (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
+    return;
+  }    
   attrs->flags = (SILC_SFTP_ATTR_SIZE |
                  SILC_SFTP_ATTR_UIDGID |
                  SILC_SFTP_ATTR_ACMODTIME);
@@ -982,24 +1048,37 @@ void mem_realpath(void *context, SilcSFTP sftp,
     path = (const char *)DIR_SEPARATOR;
 
   realpath = mem_expand_path(fs->root, path);
-  if (!realpath) {
-    (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
-    return;
-  }
+  if (!realpath)
+    goto fail;
 
   name = silc_calloc(1, sizeof(*name));
+  if (!name)
+    goto fail;
+
   name->filename = silc_calloc(1, sizeof(*name->filename));
+  if (!name->filename)
+    goto fail;
   name->filename[0] = realpath;
   name->long_filename = silc_calloc(1, sizeof(*name->long_filename));
+  if (!name->long_filename)
+    goto fail;
   name->long_filename[0] = realpath;
   name->attrs = silc_calloc(1, sizeof(*name->attrs));
+  if (!name->attrs)
+    goto fail;
   name->attrs[0] = silc_calloc(1, sizeof(*name->attrs[0]));
+  if (!name->attrs[0])
+    goto fail;
   name->count = 1;
 
   (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, (const SilcSFTPName)name, 
              callback_context);
 
   silc_sftp_name_free(name);
+  return;
+
+ fail:
+  (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
 }
 
 void mem_extended(void *context, SilcSFTP sftp,
index 1f7d26eb6ca12550c6f2d826ed1dea1ba6126ca1..8c96355aac2a08a9eda17852fd05264d13a6314b 100644 (file)
@@ -220,6 +220,10 @@ static void silc_sftp_server_attr(SilcSFTP sftp,
   }
 
   attr_buf = silc_sftp_attr_encode(attrs);
+  if (!attr_buf) {
+    silc_sftp_send_error(server, SILC_SFTP_STATUS_FAILURE, id);
+    return;
+  }
 
   silc_sftp_send_packet(server, SILC_SFTP_ATTRS, 4 + attr_buf->len,
                        SILC_STR_UI_INT(id),
@@ -266,6 +270,8 @@ SilcSFTP silc_sftp_server_start(SilcSFTPSendPacketCallback send_packet,
   SilcSFTPServer server;
 
   server = silc_calloc(1, sizeof(*server));
+  if (!server)
+    return NULL;
   server->send_packet = send_packet;
   server->send_context = send_context;
   server->fs = fs;
@@ -383,8 +389,12 @@ void silc_sftp_server_receive_process(SilcSFTP sftp,
       if (attr_len) {
        silc_buffer_set(&tmpbuf, attr_buf, attr_len);
        attrs = silc_sftp_attr_decode(&tmpbuf);
+       if (!attrs)
+         goto failure;
       } else {
        attrs = silc_calloc(1, sizeof(*attrs));
+       if (!attrs)
+         goto failure;
       }
 
       /* Call monitor */
@@ -606,8 +616,12 @@ void silc_sftp_server_receive_process(SilcSFTP sftp,
       if (attr_len) {
        silc_buffer_set(&tmpbuf, attr_buf, attr_len);
        attrs = silc_sftp_attr_decode(&tmpbuf);
+       if (!attrs)
+         goto failure;
       } else {
        attrs = silc_calloc(1, sizeof(*attrs));
+       if (!attrs)
+         goto failure;
       }
 
       /* Call monitor */
@@ -822,8 +836,12 @@ void silc_sftp_server_receive_process(SilcSFTP sftp,
       if (attr_len) {
        silc_buffer_set(&tmpbuf, attr_buf, attr_len);
        attrs = silc_sftp_attr_decode(&tmpbuf);
+       if (!attrs)
+         goto failure;
       } else {
        attrs = silc_calloc(1, sizeof(*attrs));
+       if (!attrs)
+         goto failure;
       }
 
       /* Call monitor */
@@ -863,8 +881,12 @@ void silc_sftp_server_receive_process(SilcSFTP sftp,
       if (attr_len) {
        silc_buffer_set(&tmpbuf, attr_buf, attr_len);
        attrs = silc_sftp_attr_decode(&tmpbuf);
+       if (!attrs)
+         goto failure;
       } else {
        attrs = silc_calloc(1, sizeof(*attrs));
+       if (!attrs)
+         goto failure;
       }
 
       /* Get the handle */
index 574f884173f15710f321f24b1e363f6e18ac57bf..0d15f30eea052a61c7fc9c4085bd34c326cb9cd8 100644 (file)
@@ -54,13 +54,18 @@ SilcBuffer silc_sftp_packet_encode_vp(SilcSFTPPacket packet,
   int ret;
 
   if (packet_buf) {
-    if (packet_buf->truelen < 4 + 1 + len)
+    if (packet_buf->truelen < 4 + 1 + len) {
       packet_buf = silc_buffer_realloc(packet_buf, 4 + 1 + len);
+      if (!packet_buf)
+       return NULL;
+    }
 
     buffer = packet_buf;
     dyn = FALSE;
   } else {
     buffer = silc_buffer_alloc(4 + 1 + len);
+    if (!buffer)
+      return NULL;
     dyn = TRUE;
   }
 
@@ -148,6 +153,8 @@ SilcBuffer silc_sftp_attr_encode(SilcSFTPAttributes attr)
   }
 
   buffer = silc_buffer_alloc(len);
+  if (!buffer)
+    return NULL;
   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
 
   silc_buffer_format(buffer, 
@@ -218,6 +225,8 @@ SilcSFTPAttributes silc_sftp_attr_decode(SilcBuffer buffer)
   SilcSFTPAttributes attr;
 
   attr = silc_calloc(1, sizeof(*attr));
+  if (!attr)
+    return NULL;
 
   if (silc_buffer_unformat(buffer, 
                           SILC_STR_UI_INT(&attr->flags), 
@@ -278,6 +287,9 @@ SilcSFTPAttributes silc_sftp_attr_decode(SilcBuffer buffer)
                                      sizeof(*attr->extended_type));
     attr->extended_data = silc_calloc(attr->extended_count, 
                                      sizeof(*attr->extended_data));
+    if (!attr->extended_type || !attr->extended_data)
+      return NULL;
+
     for (i = 0; i < attr->extended_count; i++) {
       unsigned char *tmp, *tmp2;
       SilcUInt32 tmp_len, tmp2_len;
@@ -290,6 +302,8 @@ SilcSFTPAttributes silc_sftp_attr_decode(SilcBuffer buffer)
 
       attr->extended_type[i] = silc_buffer_alloc(tmp_len);
       attr->extended_data[i] = silc_buffer_alloc(tmp2_len);
+      if (!attr->extended_type[i] || !attr->extended_data[i])
+       return NULL;
       silc_buffer_put(attr->extended_type[i], tmp, tmp_len);
       silc_buffer_put(attr->extended_data[i], tmp2, tmp2_len);
 
@@ -331,6 +345,8 @@ void silc_sftp_name_add(SilcSFTPName name, const char *short_name,
                                     (name->count + 1));
   name->attrs = silc_realloc(name->attrs, sizeof(*name->attrs) *
                             (name->count + 1));
+  if (!name->filename || !name->long_filename || !name->attrs)
+    return;
 
   name->filename[name->count] = strdup(short_name);
   name->long_filename[name->count] = strdup(long_name);
@@ -348,13 +364,20 @@ SilcBuffer silc_sftp_name_encode(SilcSFTPName name)
   SilcBuffer *attr_buf;
 
   attr_buf = silc_calloc(name->count, sizeof(*attr_buf));
+  if (!attr_buf)
+    return NULL;
+
   for (i = 0; i < name->count; i++) {
     len += (8 + strlen(name->filename[i]) + strlen(name->long_filename[i]));
     attr_buf[i] = silc_sftp_attr_encode(name->attrs[i]);
+    if (attr_buf[i])
+      return NULL;
     len += attr_buf[i]->len;
   }
 
   buffer = silc_buffer_alloc(len);
+  if (!buffer)
+    return NULL;
   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
 
   silc_buffer_format(buffer,
@@ -394,9 +417,15 @@ SilcSFTPName silc_sftp_name_decode(SilcUInt32 count, SilcBuffer buffer)
   int ret;
 
   name = silc_calloc(1, sizeof(*name));
+  if (!name)
+    return NULL;
   name->filename = silc_calloc(count, sizeof(*name->filename));
   name->long_filename = silc_calloc(count, sizeof(*name->filename));
   name->attrs = silc_calloc(count, sizeof(*name->attrs));
+  if (!name->filename || !name->long_filename || !name->attrs) {
+    silc_sftp_name_free(name);
+    return NULL;
+  }
   name->count = count;
 
   for (i = 0; i < count; i++) {
@@ -415,6 +444,10 @@ SilcSFTPName silc_sftp_name_decode(SilcUInt32 count, SilcBuffer buffer)
     /* Decode attributes, this will pull the `buffer' to correct place
        for next round automatically. */
     name->attrs[i] = silc_sftp_attr_decode(buffer);
+    if (!name->attrs[i]) {
+      silc_sftp_name_free(name);
+      return NULL;
+    }
   }
 
   return name;
index 45954677a9f5ba075f19ea556588a9393e4d8f82..b7017448e9a83ca537efb92f6009de672bab5c37 100644 (file)
@@ -136,13 +136,13 @@ typedef enum {
  *
  ***/
 typedef struct {
-  SilcUInt32 flags;                    /* Flags to indicate present attributes */
-  SilcUInt64 size;                     /* Sife of the file in bytes */
-  SilcUInt32 uid;                      /* Unix user ID */
-  SilcUInt32 gid;                      /* Unix group ID */
-  SilcUInt32 permissions;              /* POSIX file permission bitmask */
-  SilcUInt32 atime;                    /* Access time of file */
-  SilcUInt32 mtime;                    /* Modification time of file */
+  SilcUInt32 flags;            /* Flags to indicate present attributes */
+  SilcUInt64 size;             /* Sife of the file in bytes */
+  SilcUInt32 uid;                      /* Unix user ID */
+  SilcUInt32 gid;                      /* Unix group ID */
+  SilcUInt32 permissions;              /* POSIX file permission bitmask */
+  SilcUInt32 atime;                    /* Access time of file */
+  SilcUInt32 mtime;            /* Modification time of file */
 
   SilcUInt32 extended_count;   /* Extended type and data count */
   SilcBuffer *extended_type;
@@ -163,7 +163,7 @@ typedef struct {
  *    example when reading the contents of a directory.
  *
  ***/
-typedef struct  {
+typedef struct {
   char **filename;
   char **long_filename;
   SilcSFTPAttributes *attrs;