[tz] [PROPOSED] Don’t assume nonempty argv

Paul Eggert eggert at cs.ucla.edu
Sat Oct 29 06:56:18 UTC 2022


Don’t dump core if argv[0] is NULL, which is allowed on
GNU/Linux if the invoker is sufficiently perverse.
* zdump.c (progname): Now char const *, so that it can be given
the address of a string constant.
(tzalloc): Use optarg, not progname, since progname’s type is no
longer correct.
* zdump.c, zic.c (main): Initialize progname to non-null.
---
 zdump.c | 6 +++---
 zic.c   | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/zdump.c b/zdump.c
index 512ba8cc..f0461ade 100644
--- a/zdump.c
+++ b/zdump.c
@@ -84,7 +84,7 @@ static time_t const absolute_max_time =
    ? (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift))
    : -1);
 static int	longest;
-static char *	progname;
+static char const *progname;
 static bool	warned;
 static bool	errout;
 
@@ -234,7 +234,7 @@ tzalloc(char const *val)
     exit(EXIT_FAILURE);
   }
   tzset();
-  return &progname;  /* Any valid non-null char ** will do.  */
+  return &optarg;  /* Any valid non-null char ** will do.  */
 # else
   enum { TZeqlen = 3 };
   static char const TZeq[TZeqlen] = "TZ=";
@@ -463,7 +463,7 @@ main(int argc, char *argv[])
 # endif /* defined TEXTDOMAINDIR */
 	textdomain(TZ_DOMAIN);
 #endif /* HAVE_GETTEXT */
-	progname = argv[0];
+	progname = argv[0] ? argv[0] : "zdump";
 	for (i = 1; i < argc; ++i)
 		if (strcmp(argv[i], "--version") == 0) {
 			printf("zdump %s%s\n", PKGVERSION, TZVERSION);
diff --git a/zic.c b/zic.c
index 501718f4..f3b32ecc 100644
--- a/zic.c
+++ b/zic.c
@@ -943,7 +943,7 @@ main(int argc, char **argv)
 	textdomain(TZ_DOMAIN);
 #endif /* HAVE_GETTEXT */
 	main_argv = argv;
-	progname = argv[0];
+	progname = argv[0] ? argv[0] : "zic";
 	if (TYPE_BIT(zic_t) < 64) {
 		fprintf(stderr, "%s: %s\n", progname,
 			_("wild compilation-time specification of zic_t"));
-- 
2.37.2



More information about the tz mailing list