[m-rev.] diff: avoid an abort when writing trace count flies on Windows

Julien Fischer jfischer at opturion.com
Sun Oct 22 17:28:37 AEDT 2023


Avoid an abort when writing trace count files on Windows.

Writing trace counts the default trace count file aborts on Windows due to the
program basename not being stripped of drive and directory qualifications.
Use the machinery introduced in commit 54de823 to avoid this.

Add XXX WINDOWS comments about other potential problems with the trace counts
mechanism on Windows.

runtime/mercury_trace_base.c:
     When constructing the default name for trace counts files, use the recently
     add MR_get_program-basename() function to extract the program basename.

     Add XXX WINDOWS comments about some other things. Resolving these will
     be the subject of future changes.

runtime/mercury_runtime_util.c:
     Adjust an error message.

Julien.

diff --git a/runtime/mercury_runtime_util.c b/runtime/mercury_runtime_util.c
index fa727c7..3bbdb64 100644
--- a/runtime/mercury_runtime_util.c
+++ b/runtime/mercury_runtime_util.c
@@ -171,7 +171,7 @@ MR_get_program_basename(const char *program_name)
              NULL, 0   // Ignore .exe extension.
          );
          if (err != 0) {
-            MR_fatal_error("Could not split path");
+            MR_fatal_error("MR_get_program_basename: could not split path");
          }
          basename = MR_wide_to_utf8(wname, NULL);

diff --git a/runtime/mercury_trace_base.c b/runtime/mercury_trace_base.c
index d48fe4e..18839be 100644
--- a/runtime/mercury_trace_base.c
+++ b/runtime/mercury_trace_base.c
@@ -1,7 +1,7 @@
  // vim: ts=4 sw=4 expandtab ft=c

  // Copyright (C) 1997-2008, 2011 The University of Melbourne.
-// Copyright (C) 2014-2016, 2018 The Mercury team.
+// Copyright (C) 2014-2016, 2018, 2022-2023 The Mercury team.
  // This file is distributed under the terms specified in COPYING.LIB.

  // mercury_trace_base.c implements the interface between the main part
@@ -274,11 +274,7 @@ MR_trace_record_label_exec_counts(void *dummy)
      const char  *program_name;
      char        errbuf[MR_STRERROR_BUF_SIZE];

-    program_name = MR_copy_string(MR_progname);
-    slash = strrchr(program_name, '/');
-    if (slash != NULL) {
-        program_name = slash + 1;
-    }
+    program_name = MR_get_program_basename(MR_progname);

      summarize = MR_FALSE;
      keep = MR_FALSE;
@@ -325,6 +321,7 @@ MR_trace_record_label_exec_counts(void *dummy)
              program_name, getpid());

          // Make sure name is an acceptable filename.
+        // XXX WINDOWS: this is not the right thing to do on Windows.
          for (s = name; *s != '\0'; s++) {
              if (*s == '/') {
                  *s = '_';
@@ -332,6 +329,7 @@ MR_trace_record_label_exec_counts(void *dummy)
          }
      }

+    // XXX WINDOWS: probably wants to be _wfopen on Windows.
      fp = fopen(name, "w");
      if (fp != NULL) {
          unsigned    num_written;


More information about the reviews mailing list