libfile: copy_file: prevent spurious error message
PATCH 1/1 libfile: copy_file: prevent spurious error message
--- lib/libfile.c | 1 + 1 file changed, 1 insertion(+)
lib/libfile.c +1 -0
@@ -349,6 +349,7 @@ int copy_file(const char *src, const char *dst, int verbose) ret =stat (dst, &dststat);if (ret && ret != -ENOENT ) goto out; + ret =0 ;/* Set O_TRUNC only if file exist and is a regular file */ if (!ret &&S_ISREG (dststat.st_mode))
Signature · 7 lines
2.7.4 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 1/1 libfile: copy_file: prevent spurious error message
Before this, if the function bails out somewhere at a later point, this return value will be outdated and will produce a misleading error message down the line.
---
Changes from v1:
- commit message
lib/libfile.c | 1 + 1 file changed, 1 insertion(+)
lib/libfile.c +1 -0
@@ -349,6 +349,7 @@ int copy_file(const char *src, const char *dst, int verbose) ret =stat (dst, &dststat);if (ret && ret != -ENOENT ) goto out; + ret =0 ;/* Set O_TRUNC only if file exist and is a regular file */ if (!ret &&S_ISREG (dststat.st_mode))
Signature · 7 lines
2.7.4 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Hi Robert,
Quoted 17 lines · Robert Karszniewicz
Before this, if the function bails out somewhere at a later point, this return value will be outdated and will produce a misleading error message down the line.
Signed-off-by Robert Karszniewicz <[email protected]>---
Changes from v1:
- commit messagelib/libfile.c | 1 + 1 file changed, 1 insertion(+)lib/libfile.c +0
-0
lib/libfile.c int copy_file(const char *src, const char *dst, int verbose) · line 352
Quoted 4 lines
lib/libfile.c +1
-0@@ -349,6 +349,7 @@ int copy_file(const char *src, const char *dst, int verbose) ret =stat (dst, &dststat);if (ret && ret != -ENOENT ) goto out; + ret =0 ;
Setting ret = 0 here is wrong as we test for ret one line further:
lib/libfile.c int copy_file(const char *src, const char *dst, int verbose) · line 355
Quoted 3 lines
lib/libfile.c +0
-0/* Set O_TRUNC only if file exist and is a regular file */ if (!ret &&S_ISREG (dststat.st_mode))
Anyway, it seems the error handling in copy_file is broken since 0ec6bd3e1b ("libfile: copy_file: Only open regular files with O_TRUNC"). Before that 'ret' was initialized to 1 and only when all was done it was set to 0 and returned. After this commit 'ret' was set to the return value of the stat() call and accidently returned after each 'goto out'. 'ret' should be set explicitly to the desired return value right before each 'goto out'.
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v3 1/1 libfile: copy_file: prevent spurious error message
Before this, ret was falsely polluted, which caused a misleading error message if the function bailed out at a later point.
--- lib/libfile.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
lib/libfile.c +4 -4
@@ -330,7 +330,7 @@ int copy_file(const char *src, const char *dst, int verbose) {char *rw_buf =NULL ;int srcfd =0 , dstfd =0 ; -int r; +int r, s;int ret =1 , err1 =0 ;int mode;int total =0 ;
@@ -346,12 +346,12 @@ int copy_file(const char *src, const char *dst, int verbose) mode =O_WRONLY |O_CREAT ; - ret =stat (dst, &dststat); -if (ret && ret != -ENOENT ) + s =stat (dst, &dststat); +if (s && s != -ENOENT ) goto out;/* Set O_TRUNC only if file exist and is a regular file */ -if (!ret &&S_ISREG (dststat.st_mode)) +if (!s &&S_ISREG (dststat.st_mode)) mode |=O_TRUNC ; dstfd =open (dst, mode);
Signature · 7 lines
2.7.4 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
In all this haste, I forgot my question.
What error codes should be used? For each goto its own code, in sequence, starting from -1?
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Quoted 4 lines · Robert Karszniewicz
In all this haste, I forgot my question.
What error codes should be used? For each goto its own code, in sequence, starting from -1?
Please propagate the error code returned from the call that failed. i.e. something like:
r = read(srcfd, rw_buf, RW_BUF_SIZE);
if (r < 0) {
perror("read");
ret = r;
goto out;
}Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v4 1/1 libfile: copy_file: fix error handling
Before this, ret was falsely polluted, which caused a misleading error message if the function bailed out at a later point.
--- lib/libfile.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
lib/libfile.c +12 -5
@@ -330,7 +330,7 @@ int copy_file(const char *src, const char *dst, int verbose) {char *rw_buf =NULL ;int srcfd =0 , dstfd =0 ; -int r; +int r, s;int ret =1 , err1 =0 ;int mode;int total =0 ;
@@ -341,22 +341,27 @@ int copy_file(const char *src, const char *dst, int verbose) srcfd =open (src,O_RDONLY );if (srcfd <0 ) {printf ("could not open %s: %s\n" , src,errno_str ()); + ret = srcfd; goto out; } mode =O_WRONLY |O_CREAT ; - ret =stat (dst, &dststat); -if (ret && ret != -ENOENT ) + s =stat (dst, &dststat); +if (s && s != -ENOENT ) { +printf ("could not stat %s: %s\n" , dst,errno_str ()); + ret = s; goto out; + }/* Set O_TRUNC only if file exist and is a regular file */ -if (!ret &&S_ISREG (dststat.st_mode)) +if (!s &&S_ISREG (dststat.st_mode)) mode |=O_TRUNC ; dstfd =open (dst, mode);if (dstfd <0 ) {printf ("could not open %s: %s\n" , dst,errno_str ()); + ret = dstfd; goto out; }
@@ -371,12 +376,14 @@ int copy_file(const char *src, const char *dst, int verbose) r =read (srcfd, rw_buf,RW_BUF_SIZE );if (r <0 ) {perror ("read" ); + ret = r; goto out; }if (!r)break ; -if (write_full (dstfd, rw_buf, r) <0 ) { + ret =write_full (dstfd, rw_buf, r); +if (ret <0 ) {perror ("write" ); goto out; }
Signature · 7 lines
2.7.4 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Sascha Hauer
AppliedRe: [PATCH v4] libfile: copy_file: fix error handling
Quoted 7 lines · Robert Karszniewicz
Before this, ret was falsely polluted, which caused a misleading error message if the function bailed out at a later point.
Signed-off-by Robert Karszniewicz <[email protected]>--- lib/libfile.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
Applied, thanks
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox