Hello Nikos Nikoleris,

I'd like you to do a code review. Please visit

    https://gem5-review.googlesource.com/2962

to review the following change.


Change subject: util: Correctly handle short writes in m5 (read|exec)file
......................................................................

util: Correctly handle short writes in m5 (read|exec)file

The m5 tool has subcommands that writes a file to the simulated file
system. The implementation of this command currently doesn't check the
return value from write, which leads to compiler warnings and
potentially incorrect behavior. Add the necessary checks.

Change-Id: If558534d3245aa24cf15edf06bd0af4c6ba3908c
Signed-off-by: Andreas Sandberg <[email protected]>
Reviewed-by: Nikos Nikoleris <[email protected]>
---
M util/m5/m5.c
1 file changed, 19 insertions(+), 3 deletions(-)



diff --git a/util/m5/m5.c b/util/m5/m5.c
index ee92134..8151560 100644
--- a/util/m5/m5.c
+++ b/util/m5/m5.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 ARM Limited
+ * Copyright (c) 2011, 2017 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -109,7 +109,7 @@
 {
     char buf[256*1024];
     int offset = 0;
-    int len;
+    int len, ret;

     // Touch all buffer pages to ensure they are mapped in the
     // page table. This is required in the case of X86_FS, where
@@ -117,9 +117,25 @@
     memset(buf, 0, sizeof(buf));

     while ((len = m5_readfile(buf, sizeof(buf), offset)) > 0) {
-        write(dest_fid, buf, len);
+        char *base = buf;
         offset += len;
+        do {
+            ret = write(dest_fid, base, len);
+            if (ret < 0) {
+                perror("Failed to write file");
+                exit(2);
+            } else if (ret == 0) {
+                fprintf(stderr, "Failed to write file: "
+                        "Unhandled short write\n");
+                exit(2);
+            }
+
+            base += ret;
+            len -= ret;
+        } while (len);
     }
+
+    return offset;
 }

 void

--
To view, visit https://gem5-review.googlesource.com/2962
To unsubscribe, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: If558534d3245aa24cf15edf06bd0af4c6ba3908c
Gerrit-Change-Number: 2962
Gerrit-PatchSet: 1
Gerrit-Owner: Andreas Sandberg <[email protected]>
Gerrit-Reviewer: Nikos Nikoleris <[email protected]>
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to