From f096ba37af81540616ba545101915fcf8889bb9a Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 10 Sep 2001 11:09:36 +0000 Subject: added xfile (This used to be commit 7c2fe172c3322433571e73a0bef5c67168dd7c80) --- source3/lib/xfile.c | 340 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 340 insertions(+) create mode 100644 source3/lib/xfile.c (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c new file mode 100644 index 0000000000..3a86d2469d --- /dev/null +++ b/source3/lib/xfile.c @@ -0,0 +1,340 @@ +/* + Unix SMB/Netbios implementation. + Version 3.0 + stdio replacement + Copyright (C) Andrew Tridgell 2001 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +/* + stdio is very convenient, but on some systems the file descriptor + in FILE* is 8 bits, so it fails when more than 255 files are open. + + XFILE replaces stdio. It is less efficient, but at least it works + when you have lots of files open + + The main restriction on XFILE is that it doesn't support seeking, + and doesn't support O_RDWR. That keeps the code simple. +*/ + +#include "includes.h" + +static XFILE _x_stdin = { 0, NULL, NULL, 0, 0, O_RDONLY, X_IOFBF, 0 }; +static XFILE _x_stdout = { 1, NULL, NULL, 0, 0, O_WRONLY, X_IOLBF, 0 }; +static XFILE _x_stderr = { 2, NULL, NULL, 0, 0, O_WRONLY, X_IONBF, 0 }; + +XFILE *x_stdin = &_x_stdin; +XFILE *x_stdout = &_x_stdout; +XFILE *x_stderr = &_x_stderr; + +#define XBUFSIZE BUFSIZ + +#define X_FLAG_EOF 1 +#define X_FLAG_ERROR 2 + +/* simulate setvbuf() */ +int x_setvbuf(XFILE *f, char *buf, int mode, size_t size) +{ + x_fflush(f); + if (f->bufused) return -1; + + /* on files being read full buffering is the only option */ + if ((f->open_flags & O_ACCMODE) == O_RDONLY) { + mode = X_IOFBF; + } + + /* destroy any earlier buffer */ + if (f->buf) free(f->buf); + f->buf = 0; + f->bufsize = 0; + f->next = NULL; + f->bufused = 0; + f->buftype = mode; + + if (f->buftype == X_IONBF) return 0; + + /* if buffering then we need some size */ + if (size == 0) size = XBUFSIZE; + + f->bufsize = size; + f->bufused = 0; + + return 0; +} + +/* allocate the buffer */ +static int x_allocate_buffer(XFILE *f) +{ + if (f->buf) return 1; + if (f->bufsize == 0) return 0; + f->buf = malloc(f->bufsize); + if (!f->buf) return 0; + f->next = f->buf; + return 1; +} + + +/* this looks more like open() than fopen(), but that is quite deliberate. + I want programmers to *think* about O_EXCL, O_CREAT etc not just + get them magically added +*/ +XFILE *x_fopen(const char *fname, int flags, mode_t mode) +{ + XFILE *ret; + + ret = (XFILE *)malloc(sizeof(XFILE)); + if (!ret) return NULL; + + memset(ret, 0, sizeof(XFILE)); + + if ((flags & O_ACCMODE) == O_RDWR) { + /* we don't support RDWR in XFILE - use file + descriptors instead */ + errno = EINVAL; + return NULL; + } + + ret->open_flags = flags; + + ret->fd = sys_open(fname, flags, mode); + if (ret->fd == -1) { + free(ret); + return NULL; + } + + x_setvbuf(ret, NULL, X_IOFBF, XBUFSIZE); + + return ret; +} + +/* simulate fclose() */ +int x_fclose(XFILE *f) +{ + int ret; + + /* make sure we flush any buffered data */ + x_fflush(f); + + ret = close(f->fd); + f->fd = -1; + if (f->buf) { + /* make sure data can't leak into a later malloc */ + memset(f->buf, 0, f->bufsize); + free(f->buf); + } + free(f); + return ret; +} + +/* simulate fwrite() */ +int x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) +{ + int ret, total=0; + + /* we might be writing unbuffered */ + if (f->buftype == X_IONBF || + (!f->buf && !x_allocate_buffer(f))) { + ret = write(f->fd, p, size*nmemb); + if (ret == -1) return -1; + return ret/size; + } + + + while (total < size*nmemb) { + int n = f->bufsize - f->bufused; + n = MIN(n, (size*nmemb)-total); + + if (n == 0) { + /* it's full, flush it */ + x_fflush(f); + continue; + } + + memcpy(f->buf + f->bufused, total+(char *)p, n); + f->bufused += n; + total += n; + } + + /* when line buffered we need to flush at the last linefeed. This can + flush a bit more than necessary, but that is harmless */ + if (f->buftype == X_IOLBF && f->bufused) { + int i; + for (i=size-1; i>=0; i--) { + if (*(i+(char *)p) == '\n') { + x_fflush(f); + break; + } + } + } + + return total/size; +} + +/* thank goodness for asprintf() */ +int x_vfprintf(XFILE *f, const char *format, va_list ap) +{ + char *p; + int len, ret; + len = vasprintf(&p, format, ap); + if (len <= 0) return len; + ret = x_fwrite(p, 1, len, f); + free(p); + return ret; +} + +int x_fprintf(XFILE *f, const char *format, ...) +{ + va_list ap; + int ret; + + va_start(ap, format); + ret = x_vfprintf(f, format, ap); + va_end(ap); + return ret; +} + +/* at least fileno() is simple! */ +int x_fileno(XFILE *f) +{ + return f->fd; +} + +/* simulate fflush() */ +int x_fflush(XFILE *f) +{ + int ret; + + if (f->flags & X_FLAG_ERROR) return -1; + + if ((f->open_flags & O_ACCMODE) != O_WRONLY) { + errno = EINVAL; + return -1; + } + + if (f->bufused == 0) return 0; + + ret = write(f->fd, f->buf, f->bufused); + if (ret == -1) return -1; + + f->bufused -= ret; + if (f->bufused > 0) { + f->flags |= X_FLAG_ERROR; + memmove(f->buf, ret + (char *)f->buf, f->bufused); + return -1; + } + + return 0; +} + +/* simulate setbuffer() */ +void x_setbuffer(XFILE *f, char *buf, size_t size) +{ + x_setvbuf(f, buf, buf?X_IOFBF:X_IONBF, size); +} + +/* simulate setbuf() */ +void x_setbuf(XFILE *f, char *buf) +{ + x_setvbuf(f, buf, buf?X_IOFBF:X_IONBF, XBUFSIZE); +} + +/* simulate setlinebuf() */ +void x_setlinebuf(XFILE *f) +{ + x_setvbuf(f, NULL, X_IOLBF, 0); +} + + +/* simulate feof() */ +int x_feof(XFILE *f) +{ + if (f->flags & X_FLAG_EOF) return 1; + return 0; +} + +/* simulate ferror() */ +int x_ferror(XFILE *f) +{ + if (f->flags & X_FLAG_ERROR) return 1; + return 0; +} + +/* fill the read buffer */ +static void x_fillbuf(XFILE *f) +{ + int n; + + if (f->bufused) return; + + if (!f->buf && !x_allocate_buffer(f)) return; + + n = read(f->fd, f->buf, f->bufsize); + if (n <= 0) return; + f->bufused = n; + f->next = f->buf; +} + +/* simulate fgetc() */ +int x_fgetc(XFILE *f) +{ + int ret; + + if (f->flags & (X_FLAG_EOF | X_FLAG_ERROR)) return EOF; + + if (f->bufused == 0) x_fillbuf(f); + + if (f->bufused == 0) { + f->flags |= X_FLAG_EOF; + return EOF; + } + + ret = *(unsigned char *)(f->next); + f->next++; + f->bufused--; + return ret; +} + +/* simulate fread */ +size_t x_fread(void *p, size_t size, size_t nmemb, XFILE *f) +{ + size_t total = 0; + while (total < size*nmemb) { + int c = x_fgetc(f); + if (c == EOF) break; + (total+(char *)p)[0] = (char)c; + total++; + } + return total/size; +} + +/* simulate fgets() */ +char *x_fgets(char *s, int size, XFILE *stream) +{ + char *s0 = s; + int l = size; + while (l>1) { + int c = x_fgetc(stream); + if (c == EOF) break; + *s++ = (char)c; + l--; + if (c == '\n') break; + } + if (l==size || x_ferror(stream)) { + return 0; + } + *s = 0; + return s0; +} -- cgit From 484a7c0341fe033fe26fe1e6b597ed1c456c39d4 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 17 Sep 2001 02:19:44 +0000 Subject: move to SAFE_FREE() (This used to be commit 60e907b7e8e1c008463a88ed2b076344278986ef) --- source3/lib/xfile.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 3a86d2469d..6e21aeca58 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -57,7 +57,7 @@ int x_setvbuf(XFILE *f, char *buf, int mode, size_t size) } /* destroy any earlier buffer */ - if (f->buf) free(f->buf); + SAFE_FREE(f->buf); f->buf = 0; f->bufsize = 0; f->next = NULL; @@ -111,7 +111,7 @@ XFILE *x_fopen(const char *fname, int flags, mode_t mode) ret->fd = sys_open(fname, flags, mode); if (ret->fd == -1) { - free(ret); + SAFE_FREE(ret); return NULL; } @@ -133,9 +133,9 @@ int x_fclose(XFILE *f) if (f->buf) { /* make sure data can't leak into a later malloc */ memset(f->buf, 0, f->bufsize); - free(f->buf); + SAFE_FREE(f->buf); } - free(f); + SAFE_FREE(f); return ret; } @@ -191,7 +191,7 @@ int x_vfprintf(XFILE *f, const char *format, va_list ap) len = vasprintf(&p, format, ap); if (len <= 0) return len; ret = x_fwrite(p, 1, len, f); - free(p); + SAFE_FREE(p); return ret; } -- cgit From f8e2baf39eb864481dd48f61404136b325cd73c2 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 3 Nov 2001 23:34:24 +0000 Subject: Added NT_USER_TOKEN into server_info to fix extra groups problem. Got "medieval on our ass" about const warnings (as many as I could :-). Jeremy. (This used to be commit ee5e7ca547eff016818ba5c43b8ea0c9fa69b808) --- source3/lib/xfile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 6e21aeca58..7fc519e451 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -163,7 +163,7 @@ int x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) continue; } - memcpy(f->buf + f->bufused, total+(char *)p, n); + memcpy(f->buf + f->bufused, total+(const char *)p, n); f->bufused += n; total += n; } @@ -173,7 +173,7 @@ int x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) if (f->buftype == X_IOLBF && f->bufused) { int i; for (i=size-1; i>=0; i--) { - if (*(i+(char *)p) == '\n') { + if (*(i+(const char *)p) == '\n') { x_fflush(f); break; } -- cgit From cd68afe31256ad60748b34f7318a180cfc2127cc Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Wed, 30 Jan 2002 06:08:46 +0000 Subject: Removed version number from file header. Changed "SMB/Netbios" to "SMB/CIFS" in file header. (This used to be commit 6a58c9bd06d0d7502a24bf5ce5a2faf0a146edfa) --- source3/lib/xfile.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 7fc519e451..00ea6e5cac 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -1,6 +1,5 @@ /* - Unix SMB/Netbios implementation. - Version 3.0 + Unix SMB/CIFS implementation. stdio replacement Copyright (C) Andrew Tridgell 2001 -- cgit From e90b65284812aaa5ff9e9935ce9bbad7791cbbcd Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 15 Jul 2002 10:35:28 +0000 Subject: updated the 3.0 branch from the head branch - ready for alpha18 (This used to be commit 03ac082dcb375b6f3ca3d810a6a6367542bc23ce) --- source3/lib/xfile.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 00ea6e5cac..903dfb1ae0 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -31,16 +31,16 @@ #include "includes.h" -static XFILE _x_stdin = { 0, NULL, NULL, 0, 0, O_RDONLY, X_IOFBF, 0 }; -static XFILE _x_stdout = { 1, NULL, NULL, 0, 0, O_WRONLY, X_IOLBF, 0 }; +#define XBUFSIZE BUFSIZ + +static XFILE _x_stdin = { 0, NULL, NULL, XBUFSIZE, 0, O_RDONLY, X_IOFBF, 0 }; +static XFILE _x_stdout = { 1, NULL, NULL, XBUFSIZE, 0, O_WRONLY, X_IOLBF, 0 }; static XFILE _x_stderr = { 2, NULL, NULL, 0, 0, O_WRONLY, X_IONBF, 0 }; XFILE *x_stdin = &_x_stdin; XFILE *x_stdout = &_x_stdout; XFILE *x_stderr = &_x_stderr; -#define XBUFSIZE BUFSIZ - #define X_FLAG_EOF 1 #define X_FLAG_ERROR 2 @@ -187,7 +187,11 @@ int x_vfprintf(XFILE *f, const char *format, va_list ap) { char *p; int len, ret; - len = vasprintf(&p, format, ap); + va_list ap2; + + VA_COPY(ap2, ap); + + len = vasprintf(&p, format, ap2); if (len <= 0) return len; ret = x_fwrite(p, 1, len, f); SAFE_FREE(p); -- cgit From b2edf254eda92f775e7d3d9b6793b4d77f9000b6 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sat, 17 Aug 2002 17:00:51 +0000 Subject: sync 3.0 branch with head (This used to be commit 3928578b52cfc949be5e0ef444fce1558d75f290) --- source3/lib/xfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 903dfb1ae0..b5710f3a39 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -171,7 +171,7 @@ int x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) flush a bit more than necessary, but that is harmless */ if (f->buftype == X_IOLBF && f->bufused) { int i; - for (i=size-1; i>=0; i--) { + for (i=(size*nmemb)-1; i>=0; i--) { if (*(i+(const char *)p) == '\n') { x_fflush(f); break; -- cgit From a834a73e341059be154426390304a42e4a011f72 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 25 Sep 2002 15:19:00 +0000 Subject: sync'ing up for 3.0alpha20 release (This used to be commit 65e7b5273bb58802bf0c389b77f7fcae0a1f6139) --- source3/lib/xfile.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index b5710f3a39..7621712e9a 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -43,6 +43,7 @@ XFILE *x_stderr = &_x_stderr; #define X_FLAG_EOF 1 #define X_FLAG_ERROR 2 +#define X_FLAG_EINVAL 3 /* simulate setvbuf() */ int x_setvbuf(XFILE *f, char *buf, int mode, size_t size) @@ -341,3 +342,36 @@ char *x_fgets(char *s, int size, XFILE *stream) *s = 0; return s0; } + +/* trivial seek, works only for SEEK_SET and SEEK_END if SEEK_CUR is + * set then an error is returned */ +off_t x_tseek(XFILE *f, off_t offset, int whence) +{ + if (f->flags & X_FLAG_ERROR) + return -1; + + /* only SEEK_SET and SEEK_END are supported */ + /* SEEK_CUR needs internal offset counter */ + if (whence != SEEK_SET && whence != SEEK_END) { + f->flags |= X_FLAG_EINVAL; + errno = EINVAL; + return -1; + } + + /* empty the buffer */ + switch (f->open_flags & O_ACCMODE) { + case O_RDONLY: + f->bufused = 0; + break; + case O_WRONLY: + if (x_fflush(f) != 0) + return -1; + break; + default: + errno = EINVAL; + return -1; + } + + f->flags &= ~X_FLAG_EOF; + return (off_t)sys_lseek(f->fd, offset, whence); +} -- cgit From 43059acb95837fce3c8fdf5fc71b96c403e61939 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Fri, 3 Jan 2003 03:24:23 +0000 Subject: Merge from HEAD - add PRINTF_ATTRIBUTE to a few more functions. (This used to be commit 9e5297131cc53d7161aa74566f147b98e1c27aaa) --- source3/lib/xfile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 7621712e9a..57f3e27638 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -184,7 +184,7 @@ int x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) } /* thank goodness for asprintf() */ -int x_vfprintf(XFILE *f, const char *format, va_list ap) + int x_vfprintf(XFILE *f, const char *format, va_list ap) { char *p; int len, ret; @@ -199,7 +199,7 @@ int x_vfprintf(XFILE *f, const char *format, va_list ap) return ret; } -int x_fprintf(XFILE *f, const char *format, ...) + int x_fprintf(XFILE *f, const char *format, ...) { va_list ap; int ret; -- cgit From 266ec4aac04cb8666234f18baa38ff6387f40cb3 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 24 Feb 2003 03:09:08 +0000 Subject: Merge doxygen, signed/unsigned, const and other small fixes from HEAD to 3.0. Andrew Bartlett (This used to be commit 9ef0d40c3f8aef52ab321dc065264c42065bc876) --- source3/lib/xfile.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 57f3e27638..1534dd855e 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -140,9 +140,10 @@ int x_fclose(XFILE *f) } /* simulate fwrite() */ -int x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) +size_t x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) { - int ret, total=0; + ssize_t ret; + size_t total=0; /* we might be writing unbuffered */ if (f->buftype == X_IONBF || @@ -154,7 +155,7 @@ int x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) while (total < size*nmemb) { - int n = f->bufsize - f->bufused; + size_t n = f->bufsize - f->bufused; n = MIN(n, (size*nmemb)-total); if (n == 0) { -- cgit From 70ac6b6b55f0423eb9296fbdc7db0d2aa634a601 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Fri, 24 Sep 2004 17:38:23 +0000 Subject: r2599: avoid free()ing our static unalloceted memory that ends up in memory corruption. (This used to be commit 557e12d1b593b582ea1157d278bcdde6aba5a879) --- source3/lib/xfile.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 1534dd855e..da5ec126c1 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -135,7 +135,12 @@ int x_fclose(XFILE *f) memset(f->buf, 0, f->bufsize); SAFE_FREE(f->buf); } - SAFE_FREE(f); + /* check the file descriptor given to the function is NOT one of the static + * descriptor of this libreary or we will free unallocated memory + * --sss */ + if (f != x_stdin && f != x_stdout && f != x_stderr) { + SAFE_FREE(f); + } return ret; } -- cgit From acf9d61421faa6c0055d57fdee7db300dc5431aa Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 7 Dec 2004 18:25:53 +0000 Subject: r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation functions so we can funnel through some well known functions. Should help greatly with malloc checking. HEAD patch to follow. Jeremy. (This used to be commit 620f2e608f70ba92f032720c031283d295c5c06a) --- source3/lib/xfile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index da5ec126c1..5bb93179af 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -80,7 +80,7 @@ static int x_allocate_buffer(XFILE *f) { if (f->buf) return 1; if (f->bufsize == 0) return 0; - f->buf = malloc(f->bufsize); + f->buf = SMB_MALLOC(f->bufsize); if (!f->buf) return 0; f->next = f->buf; return 1; @@ -95,7 +95,7 @@ XFILE *x_fopen(const char *fname, int flags, mode_t mode) { XFILE *ret; - ret = (XFILE *)malloc(sizeof(XFILE)); + ret = SMB_MALLOC_P(XFILE); if (!ret) return NULL; memset(ret, 0, sizeof(XFILE)); -- cgit From 43ad653211114ddd7c85e7d9dfe13e957a97c71d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 8 Mar 2006 06:36:40 +0000 Subject: r14020: Coverity bug CID #66. Missing free on error exit. Jeremy. (This used to be commit b9980bddf5ee74b2887196d6d1a0cf393720ba3a) --- source3/lib/xfile.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 5bb93179af..71f8bdbcbb 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -96,13 +96,16 @@ XFILE *x_fopen(const char *fname, int flags, mode_t mode) XFILE *ret; ret = SMB_MALLOC_P(XFILE); - if (!ret) return NULL; + if (!ret) { + return NULL; + } memset(ret, 0, sizeof(XFILE)); if ((flags & O_ACCMODE) == O_RDWR) { /* we don't support RDWR in XFILE - use file descriptors instead */ + SAFE_FREE(ret); errno = EINVAL; return NULL; } -- cgit From 06491a4cb12d8753c63525b3ba4d754d2ab1e822 Mon Sep 17 00:00:00 2001 From: James Peach Date: Thu, 15 Jun 2006 23:51:20 +0000 Subject: r16274: Fix the smbclient prompting behaviour for both systems that have libreadline and those that don't. We always use the built-in readline replacement for non-interactive mode. Interactive prompts are always emitted to stdout and non-interactive mode never prompts at all. Introduce x_fdup to avoid spuriously closing stdout when a logfile is specified on the command line and setup_logging is called a second time. (This used to be commit 848ac756f651a4be231e5635580c0fd5f3d3fa0e) --- source3/lib/xfile.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 71f8bdbcbb..ef33c7894f 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -123,6 +123,28 @@ XFILE *x_fopen(const char *fname, int flags, mode_t mode) return ret; } +XFILE *x_fdup(const XFILE *f) +{ + XFILE *ret; + int fd; + + fd = dup(x_fileno(f)); + if (fd < 0) { + return NULL; + } + + ret = SMB_CALLOC_ARRAY(XFILE, 1); + if (!ret) { + close(fd); + return NULL; + } + + ret->fd = fd; + ret->open_flags = f->open_flags; + x_setvbuf(ret, NULL, X_IOFBF, XBUFSIZE); + return ret; +} + /* simulate fclose() */ int x_fclose(XFILE *f) { @@ -220,7 +242,7 @@ size_t x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) } /* at least fileno() is simple! */ -int x_fileno(XFILE *f) +int x_fileno(const XFILE *f) { return f->fd; } -- cgit From 1cf1e648feed823244731eef5f56bd34e15cb045 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 31 Jul 2006 04:30:55 +0000 Subject: r17334: Some C++ warnings (This used to be commit 8ae7ed1f3cecbb5285313d17b5f9511e2e622f0b) --- source3/lib/xfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index ef33c7894f..8a6776b5f9 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -80,7 +80,7 @@ static int x_allocate_buffer(XFILE *f) { if (f->buf) return 1; if (f->bufsize == 0) return 0; - f->buf = SMB_MALLOC(f->bufsize); + f->buf = (char *)SMB_MALLOC(f->bufsize); if (!f->buf) return 0; f->next = f->buf; return 1; -- cgit From 8be645836834720967d21d96e0e9e300adfcafaf Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 28 Aug 2006 02:24:15 +0000 Subject: r17865: Fix what the Stanford checker reported as a possible deref. I think this is a false positive, but it's an easy extra check to add here. Jeremy. (This used to be commit 7d3b11ab18e2c793d43b6eed1803f2183b7b2daa) --- source3/lib/xfile.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 8a6776b5f9..503e8c8359 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -267,7 +267,9 @@ int x_fflush(XFILE *f) f->bufused -= ret; if (f->bufused > 0) { f->flags |= X_FLAG_ERROR; - memmove(f->buf, ret + (char *)f->buf, f->bufused); + if (f->buf) { + memmove(f->buf, ret + (char *)f->buf, f->bufused); + } return -1; } -- cgit From 28369f04b4974092e7fff6d33afeefe4e3a184e1 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 28 Aug 2006 03:15:06 +0000 Subject: r17869: More sensible fix for Stanford Checker null deref. Jeremy. (This used to be commit 2a73e82f9663fc0e14b4c2af94de3b4216f1eece) --- source3/lib/xfile.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 503e8c8359..8824ca2c3c 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -259,7 +259,7 @@ int x_fflush(XFILE *f) return -1; } - if (f->bufused == 0) return 0; + if (f->bufused == 0 || !f->buf) return 0; ret = write(f->fd, f->buf, f->bufused); if (ret == -1) return -1; @@ -267,9 +267,7 @@ int x_fflush(XFILE *f) f->bufused -= ret; if (f->bufused > 0) { f->flags |= X_FLAG_ERROR; - if (f->buf) { - memmove(f->buf, ret + (char *)f->buf, f->bufused); - } + memmove(f->buf, ret + (char *)f->buf, f->bufused); return -1; } -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/lib/xfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index 8824ca2c3c..c90cf2078f 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -5,7 +5,7 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/lib/xfile.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index c90cf2078f..c98522200b 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -14,8 +14,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ /* -- cgit From 54db1839878641be1a9987ad3e0ddedbd6123b7c Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 27 Jan 2008 17:31:56 +1100 Subject: Adding missing calls to va_end(). Just a small commit to get a handle on this git thingy. This patch fixes some missing calls to va_end() to match various calls to va_start() and VA_COPY(). Tim. (This used to be commit ec367f307dff7948722b9ac97beb960efd91991f) --- source3/lib/xfile.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index c98522200b..ee6e581332 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -223,9 +223,15 @@ size_t x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) VA_COPY(ap2, ap); len = vasprintf(&p, format, ap2); - if (len <= 0) return len; + if (len <= 0) { + va_end(ap2); + return len; + } ret = x_fwrite(p, 1, len, f); SAFE_FREE(p); + + va_end(ap2); + return ret; } -- cgit From b360cdfeb9c70832cb022936ae41e94519db6e04 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 8 Apr 2008 15:08:47 -0700 Subject: Check x_fflush returns. Jeremy. (This used to be commit 5525dc5f09a79d30f6c52fc3a88c8bce3060e642) --- source3/lib/xfile.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index ee6e581332..d20a95b03a 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -47,7 +47,7 @@ XFILE *x_stderr = &_x_stderr; /* simulate setvbuf() */ int x_setvbuf(XFILE *f, char *buf, int mode, size_t size) { - x_fflush(f); + if (x_fflush(f) != 0) return -1; if (f->bufused) return -1; /* on files being read full buffering is the only option */ @@ -150,7 +150,7 @@ int x_fclose(XFILE *f) int ret; /* make sure we flush any buffered data */ - x_fflush(f); + (void)x_fflush(f); ret = close(f->fd); f->fd = -1; @@ -189,7 +189,9 @@ size_t x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) if (n == 0) { /* it's full, flush it */ - x_fflush(f); + if (x_fflush(f) != 0) { + return -1; + } continue; } @@ -204,7 +206,9 @@ size_t x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f) int i; for (i=(size*nmemb)-1; i>=0; i--) { if (*(i+(const char *)p) == '\n') { - x_fflush(f); + if (x_fflush(f) != 0) { + return -1; + } break; } } -- cgit From 73b65bcd1416cf0f692f861fdb2b92e951f0ea7c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 8 May 2008 22:07:35 -0700 Subject: Fix bug #5452 - smbclient put always creates zero length files. Thanks to Kai Engert for reporting. Jeremy. (This used to be commit 687275cd532f8f8ad710acd222a0c76625da53c6) --- source3/lib/xfile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/xfile.c') diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c index d20a95b03a..e44a92d34d 100644 --- a/source3/lib/xfile.c +++ b/source3/lib/xfile.c @@ -263,13 +263,13 @@ int x_fflush(XFILE *f) if (f->flags & X_FLAG_ERROR) return -1; + if (f->bufused == 0 || !f->buf) return 0; + if ((f->open_flags & O_ACCMODE) != O_WRONLY) { errno = EINVAL; return -1; } - if (f->bufused == 0 || !f->buf) return 0; - ret = write(f->fd, f->buf, f->bufused); if (ret == -1) return -1; -- cgit