From 599bd1bc0887011b2d0924e7d86d60aac25298a9 Mon Sep 17 00:00:00 2001
From: chrfranke <chrfranke@4ea69e1a-61f1-4043-bf83-b5c94c648137>
Date: Thu, 1 Apr 2010 17:40:39 +0000
Subject: [PATCH] smartctl -l xselftest,selftest: Print old log if extended
 self-test log index is out of range (ticket #66).

git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@3078 4ea69e1a-61f1-4043-bf83-b5c94c648137
---
 smartmontools/CHANGELOG    |  4 ++++
 smartmontools/ataprint.cpp | 13 ++++++++-----
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/smartmontools/CHANGELOG b/smartmontools/CHANGELOG
index 17b8057d4..f10dca324 100644
--- a/smartmontools/CHANGELOG
+++ b/smartmontools/CHANGELOG
@@ -43,6 +43,10 @@ NOTES FOR FUTURE RELEASES: see TODO file.
 
 <DEVELOPERS: ADDITIONS TO THE CHANGE LOG GO JUST BELOW HERE, PLEASE>
 
+  [CF] smartctl -l xselftest,selftest: Print old log if extended self-test
+       log index is out of range.  Workaround for bad log data from Intel
+       X25-M G2 (ticket #66).
+
   [CF] USB ID updates:
        - LaCie Desktop Hard Drive
        - Prolific PL2507 (unsupported)
diff --git a/smartmontools/ataprint.cpp b/smartmontools/ataprint.cpp
index 9b185acae..ecad52a17 100644
--- a/smartmontools/ataprint.cpp
+++ b/smartmontools/ataprint.cpp
@@ -1392,7 +1392,7 @@ static int PrintSmartExtErrorLog(const ata_smart_exterrlog * log,
 }
 
 // Print SMART Extended Self-test Log (GP Log 0x07)
-static void PrintSmartExtSelfTestLog(const ata_smart_extselftestlog * log,
+static bool PrintSmartExtSelfTestLog(const ata_smart_extselftestlog * log,
                                      unsigned nsectors, unsigned max_entries)
 {
   pout("SMART Extended Self-test Log Version: %u (%u sectors)\n",
@@ -1400,7 +1400,7 @@ static void PrintSmartExtSelfTestLog(const ata_smart_extselftestlog * log,
 
   if (!log->log_desc_index){
     pout("No self-tests have been logged.  [To run self-tests, use: smartctl -t]\n\n");
-    return;
+    return true;
   }
 
   // Check index
@@ -1408,7 +1408,7 @@ static void PrintSmartExtSelfTestLog(const ata_smart_extselftestlog * log,
   unsigned logidx = log->log_desc_index;
   if (logidx > nentries) {
     pout("Invalid Self-test Log index = 0x%04x (reserved = 0x%02x)\n", logidx, log->reserved1);
-    return;
+    return false;
   }
 
   // Index base is not clearly specified by ATA8-ACS (T13/1699-D Revision 6a),
@@ -1443,6 +1443,7 @@ static void PrintSmartExtSelfTestLog(const ata_smart_extselftestlog * log,
       false /*!print_error_only*/, print_header);
   }
   pout("\n");
+  return true;
 }
 
 static void ataPrintSelectiveSelfTestLog(const ata_selective_self_test_log * log, const ata_smart_values * sv)
@@ -2248,8 +2249,10 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
       if (!ataReadExtSelfTestLog(device, log_07, nsectors))
         failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
       else {
-        PrintSmartExtSelfTestLog(log_07, nsectors, options.smart_ext_selftest_log);
-        ok = true;
+        if (!PrintSmartExtSelfTestLog(log_07, nsectors, options.smart_ext_selftest_log))
+          returnval |= FAILLOG;
+        else
+          ok = true;
       }
     }
 
-- 
GitLab