poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] testsuite: Simplify file processing in poke.mi-json/mi-json.c


From: Mohammad-Reza Nabipoor
Subject: [PATCH] testsuite: Simplify file processing in poke.mi-json/mi-json.c
Date: Mon, 15 Feb 2021 03:30:47 +0330

2021-02-15  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>

        * testsuite/poke.mi-json/mi-json.c (test_json_to_val_to_json): Simplify
        the JSON file selection loop.
---

Hi, Jose.

I couldn't reproduce your reported failure in testsuite/poke.mi-json/mi-json.c
on my machine using gcc-10.2.0.
(And I'm compiling the gcc-8.4.0 which I'll test tomorrow; it's too late now).

But I hope this patch solves the problem (and also improves the readability).


Regards,
Mohammad-Reza


 ChangeLog                        |  5 +++
 testsuite/poke.mi-json/mi-json.c | 55 +++++++++++++-------------------
 2 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 09bd9364..319f4b76 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2021-02-15  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>
+
+       * testsuite/poke.mi-json/mi-json.c (test_json_to_val_to_json): Simplify
+       the JSON file selection loop.
+
 2021-02-14  Jose E. Marchesi  <jose.marchesi@oracle.com>
 
        * poke/poke.c (pk_print_version): Update copyright.
diff --git a/testsuite/poke.mi-json/mi-json.c b/testsuite/poke.mi-json/mi-json.c
index e6ddfaf2..a2befddc 100644
--- a/testsuite/poke.mi-json/mi-json.c
+++ b/testsuite/poke.mi-json/mi-json.c
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <dejagnu.h>
 #include <dirent.h>
+#include <err.h>
 #include <json.h>
 
 #include "pk-mi-msg.h"
@@ -595,45 +596,35 @@ test_json_to_val_to_json ()
   DIR *directory;
   struct dirent *dir;
   const char *extension;
-  char *testdir, *testfile;
-  size_t testdir_len;
+  char *testfile;
 
-  testdir_len = strlen (TESTDIR);
-  testdir = (char *) malloc (testdir_len + 2);
-  memcpy (testdir, TESTDIR, testdir_len + 1);
-  strncat (testdir, "/", 1);
-  testdir_len = strlen (testdir);
+  directory = opendir (TESTDIR);
+  if (!directory)
+    err (1, "opendir (%s) failed", TESTDIR);
 
-  directory = opendir (testdir);
-  if (directory)
+  while ((dir = readdir (directory)) != NULL)
     {
-      while ((dir = readdir (directory)) != NULL)
+      /* Ignore files without `.json` extension */
+      extension = strrchr (dir->d_name, '.');
+      if (!extension)
+        continue;
+      if (strncmp (extension + 1, "json", 4) != 0)
+        continue;
+
+      if (asprintf (&testfile, "%s/%s", TESTDIR, dir->d_name) == -1)
+        err (1, "asprintf () failed");
+
+      ifp = fopen (testfile, "r");
+      if (ifp)
         {
-          /* If this file a .json file, proccess it.  */
-          extension = strrchr (dir->d_name, '.');
-          if (extension)
-            {
-              if (!strncmp (extension + 1, "json", 4))
-                {
-                  testfile = (char *) malloc (testdir_len
-                                                   + strlen (dir->d_name) + 1);
-                  memcpy (testfile, testdir, testdir_len + 1);
-                  strncat (testfile, dir->d_name, strlen (dir->d_name));
-
-                  ifp = fopen (testfile, "r");
-                  if (ifp)
-                    {
-                      test_json_file (dir->d_name, ifp);
-                      fclose (ifp);
-                    }
-                  free (testfile);
-                }
-            }
+          test_json_file (dir->d_name, ifp);
+          fclose (ifp);
         }
-      closedir (directory);
+
+      free (testfile);
     }
 
-  free (testdir);
+  closedir (directory);
 }
 
 void
-- 
2.30.0



reply via email to

[Prev in Thread] Current Thread [Next in Thread]