copy-blockdev: Fix to handle error case first 33/314633/3 accepted/tizen/unified/20240718.143635 accepted/tizen/unified/x/20240719.012643
authorSangYoun Kwak <sy.kwak@samsung.com>
Tue, 16 Jul 2024 11:47:02 +0000 (20:47 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Thu, 18 Jul 2024 01:50:39 +0000 (10:50 +0900)
The previous code works ok but it can be lead to the reader
misunderstand its behavior.
To make it clear that error case is handled, some codes are rearranged.
Also, to clarify the error handling and flow of the code, switch
statement is replaced to if statement.

Change-Id: I65de34aab245c20ba3b6869cea1931edf99594d5
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
src/copy-blockdev/lib.c

index 629e8852f9c5a0bb9afd2abdd76cda430fa84fb4..a576b8de50ac32ab93a31cf533559658b9028574 100644 (file)
@@ -332,21 +332,18 @@ int calculate_sha1(const char *dest, unsigned char *sha1, size_t read_bytes) {
                // to not use too many syscalls.
                unsigned char buf[1 << 16];
 
-               int r_read = read(dest_fd, buf, read_bytes < sizeof(buf) ? read_bytes : sizeof(buf));
-               read_bytes -= r_read;
-               switch (r_read) {
-               case -1:
+               ssize_t r_read = read(dest_fd, buf, read_bytes < sizeof(buf) ? read_bytes : sizeof(buf));
+
+               if (r_read < 0)
                        return -1;
-               case 0:
-                       // We read all the data. Let's check the results!
-                       // ... wait, there is no two-layer (switch and while) break in C.
-                       // Ugh, we have to goto instead.
-                       goto while_done;
-               }
+
+               if (r_read == 0)
+                       break;
+
+               read_bytes -= r_read;
 
                SHA1Update(&context, buf, r_read);
        }
-while_done:;
 
        SHA1Final(sha1, &context);
        return 0;