session BUGFIX freeing last items in arrays
diff --git a/src/session_client_ssh.c b/src/session_client_ssh.c
index ae47917..7e5f6e3 100644
--- a/src/session_client_ssh.c
+++ b/src/session_client_ssh.c
@@ -637,9 +637,15 @@
     free(opts->keys[idx].privkey_path);
 
     --opts->key_count;
-
-    memcpy(opts->keys + idx, opts->keys + opts->key_count, sizeof *opts->keys);
-    opts->keys = realloc(opts->keys, opts->key_count * sizeof *opts->keys);
+    if (idx < opts->key_count) {
+        memcpy(&opts->keys[idx], &opts->keys[opts->key_count], sizeof *opts->keys);
+    }
+    if (opts->key_count) {
+        opts->keys = realloc(opts->keys, opts->key_count * sizeof *opts->keys);
+    } else {
+        free(opts->keys);
+        opts->keys = NULL;
+    }
 
     return 0;
 }
diff --git a/src/session_server.c b/src/session_server.c
index d8c6ca0..d268458 100644
--- a/src/session_server.c
+++ b/src/session_server.c
@@ -1052,8 +1052,15 @@
                 free(server_opts.endpts[i].ti_opts);
 
                 --server_opts.endpt_count;
-                memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
-                memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
+                if (i < server_opts.endpt_count) {
+                    memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
+                    memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
+                } else if (!server_opts.endpt_count) {
+                    free(server_opts.binds);
+                    server_opts.binds = NULL;
+                    free(server_opts.endpts);
+                    server_opts.endpts = NULL;
+                }
 
                 ret = 0;
 
diff --git a/src/session_server_ssh.c b/src/session_server_ssh.c
index 7377c4d..2055c91 100644
--- a/src/session_server_ssh.c
+++ b/src/session_server_ssh.c
@@ -383,7 +383,12 @@
                 nc_ctx_unlock();
 
                 --opts->authkey_count;
-                memcpy(&opts->authkeys[i], &opts->authkeys[opts->authkey_count], sizeof *opts->authkeys);
+                if (i < opts->authkey_count) {
+                    memcpy(&opts->authkeys[i], &opts->authkeys[opts->authkey_count], sizeof *opts->authkeys);
+                } else if (!opts->authkey_count) {
+                    free(opts->authkeys);
+                    opts->authkeys = NULL;
+                }
 
                 ret = 0;
             }