source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: my $buf = "string"; return $string;  is cool in Perl, but not in
Date: Mon, 14 Mar 2011 23:03:54 -0400 (EDT)	[thread overview]
Message-ID: <201103150303.p2F33sT0009469@krisdoz.my.domain> (raw)

Log Message:
-----------
my $buf = "string"; return $string;  is cool in Perl, but not in C;
found by Ulrich Spoerlein <uqs at freebsd> using the clang static analyzer;
"ok, but please document the numbers" kristaps@

Modified Files:
--------------
    mdocml:
        mandoc.c

Revision Data
-------------
Index: mandoc.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mandoc.c,v
retrieving revision 1.37
retrieving revision 1.38
diff -Lmandoc.c -Lmandoc.c -u -p -r1.37 -r1.38
--- mandoc.c
+++ mandoc.c
@@ -386,29 +386,35 @@ static char *
 time2a(time_t t)
 {
 	struct tm	 tm;
-	char		 buf[DATESIZE];
-	char		*p;
-	size_t		 nsz, rsz;
+	char		*buf, *p;
+	size_t		 ssz;
 	int		 isz;
 
 	localtime_r(&t, &tm);
 
-	p = buf;
-	rsz = DATESIZE;
-
-	if (0 == (nsz = strftime(p, rsz, "%B ", &tm)))
-		return(NULL);
-
-	p += (int)nsz;
-	rsz -= nsz;
-
-	if (-1 == (isz = snprintf(p, rsz, "%d, ", tm.tm_mday)))
-		return(NULL);
+	/*
+	 * Reserve space:
+	 * up to 9 characters for the month (September) + blank
+	 * up to 2 characters for the day + comma + blank
+	 * 4 characters for the year and a terminating '\0'
+	 */
+	p = buf = mandoc_malloc(10 + 4 + 4 + 1);
+
+	if (0 == (ssz = strftime(p, 10 + 1, "%B ", &tm)))
+		goto fail;
+	p += (int)ssz;
 
+	if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm.tm_mday)))
+		goto fail;
 	p += isz;
-	rsz -= isz;
 
-	return(strftime(p, rsz, "%Y", &tm) ? buf : NULL);
+	if (0 == strftime(p, 4 + 1, "%Y", &tm))
+		goto fail;
+	return(buf);
+
+fail:
+	free(buf);
+	return(NULL);
 }
 
 
@@ -430,7 +436,7 @@ mandoc_normdate(char *in, mandocmsg msg,
 		t = 0;
 	}
 	out = t ? time2a(t) : NULL;
-	return(mandoc_strdup(out ? out : in));
+	return(out ? out : mandoc_strdup(in));
 }
 
 
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

                 reply	other threads:[~2011-03-15  3:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201103150303.p2F33sT0009469@krisdoz.my.domain \
    --to=schwarze@mdocml.bsd.lv \
    --cc=source@mdocml.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).