]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
libsmb: Save errno across TALLOC_FREE in a few places
authorVolker Lendecke <vl@samba.org>
Mon, 28 Apr 2025 12:48:16 +0000 (14:48 +0200)
committerVolker Lendecke <vl@samba.org>
Thu, 15 May 2025 14:03:34 +0000 (14:03 +0000)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
source3/libsmb/libsmb_dir.c
source3/libsmb/libsmb_file.c
source3/libsmb/libsmb_stat.c
source3/libsmb/libsmb_xattr.c

index 2522f8d5bf6000ad25dc44ea9bd9dd62760168aa..1d2743df56b03703ab51092f4809d91e4278d93d 100644 (file)
@@ -878,12 +878,12 @@ SMBC_opendir_ctx(SMBCCTX *context,
                                }
                         } else {
                                 /* Neither the workgroup nor server exists */
-                                errno = ECONNREFUSED;
                                 if (dir) {
                                         SAFE_FREE(dir->fname);
                                         SAFE_FREE(dir);
                                 }
                                TALLOC_FREE(frame);
+                               errno = ECONNREFUSED;
                                 return NULL;
                        }
 
@@ -1038,8 +1038,8 @@ SMBC_closedir_ctx(SMBCCTX *context,
        frame = talloc_stackframe();
 
        if (!SMBC_dlist_contains(context->internal->files, dir)) {
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
        }
 
@@ -1140,27 +1140,27 @@ SMBC_readdir_ctx(SMBCCTX *context,
 
        if (!context || !context->internal->initialized) {
 
-               errno = EINVAL;
                 DEBUG(0, ("Invalid context in SMBC_readdir_ctx()\n"));
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return NULL;
 
        }
 
        if (!SMBC_dlist_contains(context->internal->files, dir)) {
 
-               errno = EBADF;
                 DEBUG(0, ("Invalid dir in SMBC_readdir_ctx()\n"));
                TALLOC_FREE(frame);
+               errno = EBADF;
                return NULL;
 
        }
 
        if (dir->file != False) { /* FIXME, should be dir, perhaps */
 
-               errno = ENOTDIR;
                 DEBUG(0, ("Found file vs directory in SMBC_readdir_ctx()\n"));
                TALLOC_FREE(frame);
+               errno = ENOTDIR;
                return NULL;
 
        }
@@ -1173,8 +1173,8 @@ SMBC_readdir_ctx(SMBCCTX *context,
         dirent = dir->dir_next->dirent;
         if (!dirent) {
 
-                errno = ENOENT;
                TALLOC_FREE(frame);
+               errno = ENOENT;
                 return NULL;
 
         }
@@ -1184,8 +1184,8 @@ SMBC_readdir_ctx(SMBCCTX *context,
 
         ret = smbc_readdir_internal(context, dirp, dirent, maxlen);
        if (ret == -1) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                 return NULL;
        }
 
@@ -1404,24 +1404,24 @@ SMBC_getdents_ctx(SMBCCTX *context,
 
        if (!context || !context->internal->initialized) {
 
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
 
        }
 
        if (!SMBC_dlist_contains(context->internal->files, dir)) {
 
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
 
        }
 
        if (dir->file != False) { /* FIXME, should be dir, perhaps */
 
-               errno = ENOTDIR;
                TALLOC_FREE(frame);
+               errno = ENOTDIR;
                return -1;
 
        }
@@ -1439,8 +1439,8 @@ SMBC_getdents_ctx(SMBCCTX *context,
 
                if (!dirlist->dirent) {
 
-                       errno = ENOENT;  /* Bad error */
                        TALLOC_FREE(frame);
+                       errno = ENOENT; /* Bad error */
                        return -1;
 
                }
@@ -1451,8 +1451,8 @@ SMBC_getdents_ctx(SMBCCTX *context,
                ret = smbc_readdir_internal(context, dirent,
                                       dirlist->dirent, maxlen);
                if (ret == -1) {
-                       errno = EINVAL;
                        TALLOC_FREE(frame);
+                       errno = EINVAL;
                        return -1;
                }
 
@@ -1462,15 +1462,15 @@ SMBC_getdents_ctx(SMBCCTX *context,
 
                        if (rem < count) { /* We managed to copy something */
 
-                               errno = 0;
                                TALLOC_FREE(frame);
+                               errno = 0;
                                return count - rem;
 
                        }
                        else { /* Nothing copied ... */
 
-                               errno = EINVAL;  /* Not enough space ... */
                                TALLOC_FREE(frame);
+                               errno = EINVAL; /* Not enough space ... */
                                return -1;
 
                        }
@@ -1541,14 +1541,14 @@ SMBC_mkdir_ctx(SMBCCTX *context,
        NTSTATUS status;
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
        if (!fname) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
@@ -1565,16 +1565,16 @@ SMBC_mkdir_ctx(SMBCCTX *context,
                             &user,
                             &password,
                             NULL)) {
-                errno = EINVAL;
                TALLOC_FREE(frame);
+                errno = EINVAL;
                return -1;
         }
 
        if (!user || user[0] == (char)0) {
                user = talloc_strdup(frame, smbc_getUser(context));
                if (!user) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }
@@ -1583,10 +1583,10 @@ SMBC_mkdir_ctx(SMBCCTX *context,
                           server, port, share, &workgroup, &user, &password);
 
        if (!srv) {
-
+               int err = errno; /* errno set by SMBC_server */
                TALLOC_FREE(frame);
-               return -1;  /* errno set by SMBC_server */
-
+               errno = err;
+               return -1;
        }
 
        creds = context->internal->creds;
@@ -1597,8 +1597,8 @@ SMBC_mkdir_ctx(SMBCCTX *context,
                                  srv->cli, path, &targetcli, &targetpath);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Could not resolve %s\n", path);
-                errno = ENOENT;
                 TALLOC_FREE(frame);
+               errno = ENOENT;
                return -1;
        }
        /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
@@ -1656,14 +1656,14 @@ SMBC_rmdir_ctx(SMBCCTX *context,
        NTSTATUS status;
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
        if (!fname) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
@@ -1680,16 +1680,16 @@ SMBC_rmdir_ctx(SMBCCTX *context,
                             &user,
                             &password,
                             NULL)) {
-                errno = EINVAL;
                TALLOC_FREE(frame);
+                errno = EINVAL;
                return -1;
         }
 
        if (!user || user[0] == (char)0) {
                user = talloc_strdup(frame, smbc_getUser(context));
                if (!user) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }
@@ -1698,10 +1698,10 @@ SMBC_rmdir_ctx(SMBCCTX *context,
                           server, port, share, &workgroup, &user, &password);
 
        if (!srv) {
-
+               int err = errno; /* errno set by SMBC_server */
                TALLOC_FREE(frame);
-               return -1;  /* errno set by SMBC_server */
-
+               errno = err;
+               return -1;
        }
 
        creds = context->internal->creds;
@@ -1712,8 +1712,8 @@ SMBC_rmdir_ctx(SMBCCTX *context,
                                  srv->cli, path, &targetcli, &targetpath);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Could not resolve %s\n", path);
-                errno = ENOENT;
                TALLOC_FREE(frame);
+               errno = ENOENT;
                return -1;
        }
        /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
@@ -1733,8 +1733,8 @@ SMBC_rmdir_ctx(SMBCCTX *context,
                        lpath = talloc_asprintf(frame, "%s\\*",
                                                targetpath);
                        if (!lpath) {
-                               errno = ENOMEM;
                                TALLOC_FREE(frame);
+                               errno = ENOMEM;
                                return -1;
                        }
 
@@ -1780,24 +1780,24 @@ SMBC_telldir_ctx(SMBCCTX *context,
 
        if (!context || !context->internal->initialized) {
 
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
 
        }
 
        if (!SMBC_dlist_contains(context->internal->files, dir)) {
 
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
 
        }
 
        if (dir->file != False) { /* FIXME, should be dir, perhaps */
 
-               errno = ENOTDIR;
                TALLOC_FREE(frame);
+               errno = ENOTDIR;
                return -1;
 
        }
@@ -1871,16 +1871,16 @@ SMBC_lseekdir_ctx(SMBCCTX *context,
 
        if (!context || !context->internal->initialized) {
 
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
 
        }
 
        if (dir->file != False) { /* FIXME, should be dir, perhaps */
 
-               errno = ENOTDIR;
                TALLOC_FREE(frame);
+               errno = ENOTDIR;
                return -1;
 
        }
@@ -1916,8 +1916,8 @@ SMBC_lseekdir_ctx(SMBCCTX *context,
 
        ok = update_dir_ents(dir, dirent);
        if (!ok) {
-               errno = EINVAL;   /* Bad entry */
                TALLOC_FREE(frame);
+               errno = EINVAL; /* Bad entry */
                return -1;
        }
 
@@ -1967,14 +1967,14 @@ SMBC_chmod_ctx(SMBCCTX *context,
 
        if (!context || !context->internal->initialized) {
 
-               errno = EINVAL;  /* Best I can think of ... */
                TALLOC_FREE(frame);
+               errno = EINVAL; /* Best I can think of ... */
                return -1;
        }
 
        if (!fname) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
@@ -1991,16 +1991,16 @@ SMBC_chmod_ctx(SMBCCTX *context,
                             &user,
                             &password,
                             NULL)) {
-                errno = EINVAL;
                TALLOC_FREE(frame);
+                errno = EINVAL;
                return -1;
         }
 
        if (!user || user[0] == (char)0) {
                user = talloc_strdup(frame, smbc_getUser(context));
                if (!user) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }
@@ -2009,8 +2009,10 @@ SMBC_chmod_ctx(SMBCCTX *context,
                           server, port, share, &workgroup, &user, &password);
 
        if (!srv) {
+               int err = errno; /* errno set by SMBC_server */
                TALLOC_FREE(frame);
-               return -1;  /* errno set by SMBC_server */
+               errno = err;
+               return -1;
        }
 
        creds = context->internal->creds;
@@ -2021,8 +2023,8 @@ SMBC_chmod_ctx(SMBCCTX *context,
                                  srv->cli, path, &targetcli, &targetpath);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Could not resolve %s\n", path);
-                errno = ENOENT;
                TALLOC_FREE(frame);
+               errno = ENOENT;
                return -1;
        }
 
@@ -2063,14 +2065,14 @@ SMBC_utimes_ctx(SMBCCTX *context,
 
        if (!context || !context->internal->initialized) {
 
-               errno = EINVAL;  /* Best I can think of ... */
                TALLOC_FREE(frame);
+               errno = EINVAL; /* Best I can think of ... */
                return -1;
        }
 
        if (!fname) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
@@ -2101,16 +2103,16 @@ SMBC_utimes_ctx(SMBCCTX *context,
                             &user,
                             &password,
                             NULL)) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
         }
 
        if (!user || user[0] == (char)0) {
                user = talloc_strdup(frame, smbc_getUser(context));
                if (!user) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }
@@ -2119,8 +2121,10 @@ SMBC_utimes_ctx(SMBCCTX *context,
                           server, port, share, &workgroup, &user, &password);
 
        if (!srv) {
+               int err = errno; /* errno set by SMBC_server */
                TALLOC_FREE(frame);
-               return -1;      /* errno set by SMBC_server */
+               errno = err;
+               return -1;
        }
 
        ok = SMBC_setatr(
@@ -2133,8 +2137,10 @@ SMBC_utimes_ctx(SMBCCTX *context,
                (struct timespec) { .tv_nsec = SAMBA_UTIME_OMIT },
                0);
        if (!ok) {
+               int err = errno; /* errno set by SMBC_setatr */
                TALLOC_FREE(frame);
-                return -1;      /* errno set by SMBC_setatr */
+               errno = err;
+               return -1;
         }
 
        TALLOC_FREE(frame);
@@ -2165,15 +2171,15 @@ SMBC_unlink_ctx(SMBCCTX *context,
 
        if (!context || !context->internal->initialized) {
 
-               errno = EINVAL;  /* Best I can think of ... */
                TALLOC_FREE(frame);
+               errno = EINVAL; /* Best I can think of ... */
                return -1;
 
        }
 
        if (!fname) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
 
        }
@@ -2189,16 +2195,16 @@ SMBC_unlink_ctx(SMBCCTX *context,
                             &user,
                             &password,
                             NULL)) {
-                errno = EINVAL;
                TALLOC_FREE(frame);
+                errno = EINVAL;
                 return -1;
         }
 
        if (!user || user[0] == (char)0) {
                user = talloc_strdup(frame, smbc_getUser(context));
                if (!user) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }
@@ -2207,9 +2213,10 @@ SMBC_unlink_ctx(SMBCCTX *context,
                           server, port, share, &workgroup, &user, &password);
 
        if (!srv) {
+               int err = errno; /* SMBC_server sets errno */
                TALLOC_FREE(frame);
-               return -1;  /* SMBC_server sets errno */
-
+               errno = err;
+               return -1;
        }
 
        creds = context->internal->creds;
@@ -2220,8 +2227,8 @@ SMBC_unlink_ctx(SMBCCTX *context,
                                  srv->cli, path, &targetcli, &targetpath);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Could not resolve %s\n", path);
-                errno = ENOENT;
                TALLOC_FREE(frame);
+               errno = ENOENT;
                return -1;
        }
        /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
@@ -2306,14 +2313,14 @@ SMBC_rename_ctx(SMBCCTX *ocontext,
            !ocontext->internal->initialized ||
            !ncontext->internal->initialized) {
 
-               errno = EINVAL;  /* Best I can think of ... */
                TALLOC_FREE(frame);
+               errno = EINVAL; /* Best I can think of ... */
                return -1;
        }
 
        if (!oname || !nname) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
@@ -2330,41 +2337,42 @@ SMBC_rename_ctx(SMBCCTX *ocontext,
                             &user1,
                             &password1,
                             NULL)) {
-                errno = EINVAL;
                TALLOC_FREE(frame);
+                errno = EINVAL;
                return -1;
        }
 
        if (!user1 || user1[0] == (char)0) {
                user1 = talloc_strdup(frame, smbc_getUser(ocontext));
                if (!user1) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }
 
        if (SMBC_parse_path(frame,
-                            ncontext,
-                            nname,
-                            NULL,
-                            &server2,
-                            &port2,
-                            &share2,
-                            &path2,
-                            &user2,
-                            &password2,
-                            NULL)) {
-                errno = EINVAL;
+                           ncontext,
+                           nname,
+                           NULL,
+                           &server2,
+                           &port2,
+                           &share2,
+                           &path2,
+                           &user2,
+                           &password2,
+                           NULL))
+       {
                TALLOC_FREE(frame);
-                return -1;
+               errno = EINVAL;
+               return -1;
        }
 
        if (!user2 || user2[0] == (char)0) {
                user2 = talloc_strdup(frame, smbc_getUser(ncontext));
                if (!user2) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }
@@ -2372,15 +2380,17 @@ SMBC_rename_ctx(SMBCCTX *ocontext,
        if (strcmp(server1, server2) || strcmp(share1, share2) ||
            strcmp(user1, user2)) {
                /* Can't rename across file systems, or users?? */
-               errno = EXDEV;
                TALLOC_FREE(frame);
+               errno = EXDEV;
                return -1;
        }
 
        srv = SMBC_server(frame, ocontext, True,
                           server1, port1, share1, &workgroup, &user1, &password1);
        if (!srv) {
+               int err = errno;
                TALLOC_FREE(frame);
+               errno = err;
                return -1;
 
        }
@@ -2399,8 +2409,8 @@ SMBC_rename_ctx(SMBCCTX *ocontext,
                                  srv->cli, path1, &targetcli1, &targetpath1);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Could not resolve %s\n", path1);
-                errno = ENOENT;
                TALLOC_FREE(frame);
+               errno = ENOENT;
                return -1;
        }
 
@@ -2419,8 +2429,8 @@ SMBC_rename_ctx(SMBCCTX *ocontext,
                                  srv->cli, path2, &targetcli2, &targetpath2);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Could not resolve %s\n", path2);
-                errno = ENOENT;
                TALLOC_FREE(frame);
+               errno = ENOENT;
                return -1;
        }
        /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
@@ -2429,8 +2439,8 @@ SMBC_rename_ctx(SMBCCTX *ocontext,
             strcmp(targetcli1->share, targetcli2->share))
        {
                /* can't rename across file systems */
-               errno = EXDEV;
                TALLOC_FREE(frame);
+               errno = EXDEV;
                return -1;
        }
 
@@ -2445,8 +2455,8 @@ SMBC_rename_ctx(SMBCCTX *ocontext,
                    !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1,
                                                targetpath2, false))) {
 
-                       errno = eno;
                        TALLOC_FREE(frame);
+                       errno = eno;
                        return -1;
 
                }
index c39e02fcb6a488a7c9632bbc515bb4800e1aff4e..f329763edd54fcfaa8fca570d1832c949ff9d59b 100644 (file)
@@ -55,14 +55,14 @@ SMBC_open_ctx(SMBCCTX *context,
        TALLOC_CTX *frame = talloc_stackframe();
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;  /* Best I can think of ... */
                TALLOC_FREE(frame);
+               errno = EINVAL; /* Best I can think of ... */
                return NULL;
        }
 
        if (!fname) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return NULL;
        }
 
@@ -77,16 +77,16 @@ SMBC_open_ctx(SMBCCTX *context,
                             &user,
                             &password,
                             NULL)) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return NULL;
         }
 
        if (!user || user[0] == (char)0) {
                user = talloc_strdup(frame, smbc_getUser(context));
                if (!user) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return NULL;
                }
        }
@@ -94,8 +94,14 @@ SMBC_open_ctx(SMBCCTX *context,
        srv = SMBC_server(frame, context, True,
                           server, port, share, &workgroup, &user, &password);
        if (!srv) {
-               if (errno == EPERM) errno = EACCES;
+               int err = errno;
+
                TALLOC_FREE(frame);
+
+               errno = err;
+               if (errno == EPERM) {
+                       errno = EACCES;
+               }
                return NULL;  /* SMBC_server sets errno */
        }
 
@@ -112,8 +118,8 @@ SMBC_open_ctx(SMBCCTX *context,
 
        file = SMB_CALLOC_ARRAY(SMBCFILE, 1);
        if (!file) {
-               errno = ENOMEM;
                TALLOC_FREE(frame);
+               errno = ENOMEM;
                return NULL;
        }
 
@@ -125,9 +131,9 @@ SMBC_open_ctx(SMBCCTX *context,
                srv->cli, path, &targetcli, &targetpath);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Could not resolve %s\n", path);
-               errno = ENOENT;
                SAFE_FREE(file);
                TALLOC_FREE(frame);
+               errno = ENOENT;
                return NULL;
        }
        /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
@@ -236,16 +242,16 @@ SMBC_read_ctx(SMBCCTX *context,
         off_t offset;
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
        DEBUG(4, ("smbc_read(%p, %zu)\n", file, count));
 
        if (!SMBC_dlist_contains(context->internal->files, file)) {
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
        }
 
@@ -254,8 +260,8 @@ SMBC_read_ctx(SMBCCTX *context,
        /* Check that the buffer exists ... */
 
        if (buf == NULL) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
@@ -288,20 +294,20 @@ SMBC_splice_ctx(SMBCCTX *context,
        NTSTATUS status;
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
        if (!SMBC_dlist_contains(context->internal->files, srcfile)) {
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
        }
 
        if (!SMBC_dlist_contains(context->internal->files, dstfile)) {
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
        }
 
@@ -339,22 +345,22 @@ SMBC_write_ctx(SMBCCTX *context,
        /* First check all pointers before dereferencing them */
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
        if (!SMBC_dlist_contains(context->internal->files, file)) {
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
        }
 
        /* Check that the buffer exists ... */
 
        if (buf == NULL) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
@@ -363,8 +369,8 @@ SMBC_write_ctx(SMBCCTX *context,
        status = cli_writeall(file->targetcli, file->cli_fd,
                              0, (const uint8_t *)buf, offset, count, NULL);
        if (!NT_STATUS_IS_OK(status)) {
-               errno = map_errno_from_nt_status(status);
                TALLOC_FREE(frame);
+               errno = map_errno_from_nt_status(status);
                return -1;
        }
 
@@ -386,14 +392,14 @@ SMBC_close_ctx(SMBCCTX *context,
        NTSTATUS status;
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
        if (!SMBC_dlist_contains(context->internal->files, file)) {
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
        }
 
@@ -699,20 +705,20 @@ SMBC_lseek_ctx(SMBCCTX *context,
        TALLOC_CTX *frame = talloc_stackframe();
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
        if (!SMBC_dlist_contains(context->internal->files, file)) {
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
        }
 
        if (!file->file) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;      /* Can't lseek a dir ... */
        }
 
@@ -728,8 +734,8 @@ SMBC_lseek_ctx(SMBCCTX *context,
                                             file->targetcli, file->cli_fd, NULL,
                                             &size, NULL, NULL, NULL, NULL,
                                             NULL))) {
-                       errno = EINVAL;
                        TALLOC_FREE(frame);
+                       errno = EINVAL;
                        return -1;
                }
                file->offset = size + offset;
@@ -757,26 +763,26 @@ SMBC_ftruncate_ctx(SMBCCTX *context,
        TALLOC_CTX *frame = talloc_stackframe();
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
        if (!SMBC_dlist_contains(context->internal->files, file)) {
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
        }
 
        if (!file->file) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
         if (!NT_STATUS_IS_OK(cli_ftruncate(file->targetcli, file->cli_fd, (uint64_t)size))) {
-                errno = EINVAL;
                 TALLOC_FREE(frame);
+                errno = EINVAL;
                 return -1;
         }
 
index 2c5eddbffdee1748a778c9cd511efe06d04ac017..1e886fc9920d6a923d8114b9e83ff36147e2b0ee 100644 (file)
@@ -135,14 +135,14 @@ SMBC_stat_ctx(SMBCCTX *context,
        TALLOC_CTX *frame = talloc_stackframe();
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;  /* Best I can think of ... */
                TALLOC_FREE(frame);
+               errno = EINVAL;  /* Best I can think of ... */
                return -1;
        }
 
        if (!fname) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
@@ -159,16 +159,16 @@ SMBC_stat_ctx(SMBCCTX *context,
                             &user,
                             &password,
                             NULL)) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                 return -1;
         }
 
        if (!user || user[0] == (char)0) {
                user = talloc_strdup(frame, smbc_getUser(context));
                if (!user) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }
@@ -176,7 +176,9 @@ SMBC_stat_ctx(SMBCCTX *context,
        srv = SMBC_server(frame, context, True,
                           server, port, share, &workgroup, &user, &password);
        if (!srv) {
+               int err = errno;
                TALLOC_FREE(frame);
+               errno = err;
                return -1;  /* errno set by SMBC_server */
        }
 
@@ -219,14 +221,14 @@ SMBC_fstat_ctx(SMBCCTX *context,
        NTSTATUS status;
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
        if (!SMBC_dlist_contains(context->internal->files, file)) {
-               errno = EBADF;
                TALLOC_FREE(frame);
+               errno = EBADF;
                return -1;
        }
 
@@ -247,8 +249,8 @@ SMBC_fstat_ctx(SMBCCTX *context,
                             &user,
                             &password,
                             NULL)) {
-                errno = EINVAL;
                TALLOC_FREE(frame);
+                errno = EINVAL;
                 return -1;
         }
 
@@ -261,8 +263,8 @@ SMBC_fstat_ctx(SMBCCTX *context,
                                  &targetcli, &targetpath);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Could not resolve %s\n", path);
-                errno = ENOENT;
                TALLOC_FREE(frame);
+                errno = ENOENT;
                return -1;
        }
        /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
@@ -274,8 +276,8 @@ SMBC_fstat_ctx(SMBCCTX *context,
                                     &write_time_ts,
                                     &change_time_ts,
                                     &ino))) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
@@ -310,7 +312,9 @@ SMBC_statvfs_ctx(SMBCCTX *context,
 
         /* Determine if the provided path is a file or a folder */
         if (SMBC_stat_ctx(context, path, &statbuf) < 0) {
+               int err = errno;
                TALLOC_FREE(frame);
+               errno = err;
                 return -1;
         }
 
@@ -318,7 +322,9 @@ SMBC_statvfs_ctx(SMBCCTX *context,
         if (S_ISDIR(statbuf.st_mode)) {
                 /* It's a directory. */
                 if ((pFile = SMBC_opendir_ctx(context, path)) == NULL) {
+                       int err = errno;
                        TALLOC_FREE(frame);
+                       errno = err;
                         return -1;
                 }
                 bIsDir = true;
@@ -326,7 +332,9 @@ SMBC_statvfs_ctx(SMBCCTX *context,
                 /* It's a file. */
                 if ((pFile = SMBC_open_ctx(context, path,
                                            O_RDONLY, 0)) == NULL) {
+                       int err = errno;
                        TALLOC_FREE(frame);
+                       errno = err;
                         return -1;
                 }
                 bIsDir = false;
index 3ce2f6aff0f84ba4441f28fee9779403e20a5535..8ebddec96bba8c1ff505bb69c062513d3d4bfdd9 100644 (file)
@@ -1747,14 +1747,14 @@ SMBC_setxattr_ctx(SMBCCTX *context,
         TALLOC_CTX *frame = talloc_stackframe();
 
        if (!context || !context->internal->initialized) {
-               errno = EINVAL;  /* Best I can think of ... */
                TALLOC_FREE(frame);
+               errno = EINVAL;  /* Best I can think of ... */
                return -1;
        }
 
        if (!fname) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
        }
 
@@ -1772,16 +1772,16 @@ SMBC_setxattr_ctx(SMBCCTX *context,
                             &user,
                             &password,
                             NULL)) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
         }
 
        if (!user || user[0] == (char)0) {
                user = talloc_strdup(frame, smbc_getUser(context));
                if (!user) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }
@@ -1812,9 +1812,9 @@ SMBC_setxattr_ctx(SMBCCTX *context,
                         talloc_asprintf(talloc_tos(), "%s:%s",
                                         name+7, (const char *) value);
                 if (! namevalue) {
-                        errno = ENOMEM;
                         ret = -1;
                        TALLOC_FREE(frame);
+                        errno = ENOMEM;
                         return -1;
                 }
 
@@ -2031,8 +2031,8 @@ SMBC_setxattr_ctx(SMBCCTX *context,
         }
 
         /* Unsupported attribute name */
-        errno = EINVAL;
        TALLOC_FREE(frame);
+        errno = EINVAL;
         return -1;
 }
 
@@ -2062,14 +2062,14 @@ SMBC_getxattr_ctx(SMBCCTX *context,
        TALLOC_CTX *frame = talloc_stackframe();
 
        if (!context || !context->internal->initialized) {
-                errno = EINVAL;  /* Best I can think of ... */
                TALLOC_FREE(frame);
+                errno = EINVAL;  /* Best I can think of ... */
                 return -1;
         }
 
         if (!fname) {
-                errno = EINVAL;
                TALLOC_FREE(frame);
+                errno = EINVAL;
                 return -1;
         }
 
@@ -2086,16 +2086,16 @@ SMBC_getxattr_ctx(SMBCCTX *context,
                             &user,
                             &password,
                             NULL)) {
-               errno = EINVAL;
                TALLOC_FREE(frame);
+               errno = EINVAL;
                return -1;
         }
 
         if (!user || user[0] == '\0') {
                user = talloc_strdup(frame, smbc_getUser(context));
                if (!user) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }
@@ -2250,8 +2250,8 @@ SMBC_removexattr_ctx(SMBCCTX *context,
         if (!user || user[0] == (char)0) {
                user = talloc_strdup(frame, smbc_getUser(context));
                if (!user) {
-                       errno = ENOMEM;
                        TALLOC_FREE(frame);
+                       errno = ENOMEM;
                        return -1;
                }
        }