# [PATCH 1/2] builtin-mailinfo.c: check error status from rewind and ftruncate

3 messages from 2009-09-29 to 2009-09-29. Participants: Junio C Hamano, Shawn O. Pearce.
Thread: https://gitlist.dev/t/21086

## Junio C Hamano, 2009-09-29 06:40

Subject: [PATCH 1/2] builtin-mailinfo.c: check error status from rewind and ftruncate
Message-ID: <1254206409-13256-1-git-send-email-gitster@pobox.com>
URL: https://gitlist.dev/e/1254206409-13256-1-git-send-email-gitster%40pobox.com

```
A recent "cut at scissors" implementation rewinds and truncates the output
file to store the message when it sees a scissors mark, but it did not
check if these library calls succeeded.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-mailinfo.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
index d498b1c..3306d9e 100644
--- a/builtin-mailinfo.c
+++ b/builtin-mailinfo.c
@@ -785,8 +785,10 @@ static int handle_commit_msg(struct strbuf *line)
 
 	if (use_scissors && is_scissors_line(line)) {
 		int i;
-		rewind(cmitmsg);
-		ftruncate(fileno(cmitmsg), 0);
+		if (rewind(cmitmsg))
+			die_errno("Could not rewind output message file");
+		if (ftruncate(fileno(cmitmsg), 0))
+			die_errno("Could not truncate output message file at scissors");
 		still_looking = 1;
 
 		/*
-- 
1.6.3

```

## Junio C Hamano, 2009-09-29 06:40

Subject: [PATCH 2/2] fast-import.c::validate_raw_date(): really validate the value
Message-ID: <1254206409-13256-2-git-send-email-gitster@pobox.com>
URL: https://gitlist.dev/e/1254206409-13256-2-git-send-email-gitster%40pobox.com
In-Reply-To: <1254206409-13256-1-git-send-email-gitster@pobox.com>

```
When reading the "raw format" timestamp from the input stream, make sure
that the timezone offset is a reasonable value by imitating 7122f82
(date.c: improve guess between timezone offset and year., 2006-06-08).

We _might_ want to also check if the timestamp itself is reasonable, but
that is left for a separate commit.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 fast-import.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/fast-import.c b/fast-import.c
index 7ef9865..6faaaac 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1744,10 +1744,12 @@ static int validate_raw_date(const char *src, char *result, int maxlen)
 {
 	const char *orig_src = src;
 	char *endp;
+	unsigned long num;
 
 	errno = 0;
 
-	strtoul(src, &endp, 10);
+	num = strtoul(src, &endp, 10);
+	/* NEEDSWORK: perhaps check for reasonable values? */
 	if (errno || endp == src || *endp != ' ')
 		return -1;
 
@@ -1755,8 +1757,9 @@ static int validate_raw_date(const char *src, char *result, int maxlen)
 	if (*src != '-' && *src != '+')
 		return -1;
 
-	strtoul(src + 1, &endp, 10);
-	if (errno || endp == src || *endp || (endp - orig_src) >= maxlen)
+	num = strtoul(src + 1, &endp, 10);
+	if (errno || endp == src + 1 || *endp || (endp - orig_src) >= maxlen ||
+	    1400 < num)
 		return -1;
 
 	strcpy(result, orig_src);
-- 
1.6.3

```

## Shawn O. Pearce, 2009-09-29 16:40

Subject: Re: [PATCH 1/2] builtin-mailinfo.c: check error status from rewind and ftruncate
Message-ID: <20090929164046.GB25756@spearce.org>
URL: https://gitlist.dev/e/20090929164046.GB25756%40spearce.org
In-Reply-To: <1254206409-13256-1-git-send-email-gitster@pobox.com>

```
Junio C Hamano <gitster@pobox.com> wrote:
> A recent "cut at scissors" implementation rewinds and truncates the output
> file to store the message when it sees a scissors mark, but it did not
> check if these library calls succeeded.
...
> diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
> index d498b1c..3306d9e 100644
> --- a/builtin-mailinfo.c
> +++ b/builtin-mailinfo.c
> @@ -785,8 +785,10 @@ static int handle_commit_msg(struct strbuf *line)
>  
>  	if (use_scissors && is_scissors_line(line)) {
>  		int i;
> -		rewind(cmitmsg);
> -		ftruncate(fileno(cmitmsg), 0);
> +		if (rewind(cmitmsg))

Uh...

builtin-mailinfo.c: In function 'handle_commit_msg':
builtin-mailinfo.c:788: error: void value not ignored as it ought to be

I think you mean to squash this in, and use fseek instead:

diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
index 3306d9e..c90cd31 100644
--- a/builtin-mailinfo.c
+++ b/builtin-mailinfo.c
@@ -785,7 +785,7 @@ static int handle_commit_msg(struct strbuf *line)
 
 	if (use_scissors && is_scissors_line(line)) {
 		int i;
-		if (rewind(cmitmsg))
+		if (fseek(cmitmsg, 0L, SEEK_SET))
 			die_errno("Could not rewind output message file");
 		if (ftruncate(fileno(cmitmsg), 0))
 			die_errno("Could not truncate output message file at scissors");

-- 
Shawn.

```
