summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--Makefile.in4
-rw-r--r--pch.c24
-rwxr-xr-xtests/no-newline-triggers-assert37
-rw-r--r--util.c4
5 files changed, 63 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index fa145fa..5ad21d6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -36,6 +36,12 @@
* tests/preserve-c-function-names: New test case.
* Makefile.in (TESTS): Add test case.
+ * util.c (savebuf): Return NULL if size == 0.
+ * pch.c (another_hunk): Check size for size != 0 before checking
+ savebuf()'s return value.
+ * tests/no-newline-triggers-assert: New test case.
+ * Makefile.in (TESTS): Add test case.
+
2009-03-19 Andreas Gruenbacher <agruen@suse.de>
Imported from Paul Eggert's working directory:
diff --git a/Makefile.in b/Makefile.in
index 595289a..fd5124f 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -88,8 +88,8 @@ HDRS = argmatch.h backupfile.h common.h dirname.h \
MISC = AUTHORS COPYING ChangeLog INSTALL Makefile.in NEWS README VERSION \
aclocal.m4 config.hin configure configure.ac install-sh \
mkinstalldirs patch.man stdbool_.h timespec.h
-TESTS = tests/crlf-handling tests/preserve-c-function-names \
- tests/remember-backup-files
+TESTS = tests/no-newline-triggers-assert tests/preserve-c-function-names \
+ tests/remember-backup-files tests/crlf-handling
DISTFILES = $(MISC) $(SRCS) $(HDRS) $(M4FILES) $(TESTS) \
pc/chdirsaf.c pc/djgpp/config.sed pc/djgpp/configure.bat \
pc/djgpp/configure.sed pc/djgpp/README
diff --git a/pch.c b/pch.c
index 72c374d..b5d700e 100644
--- a/pch.c
+++ b/pch.c
@@ -1094,10 +1094,11 @@ another_hunk (enum diff difftype, bool rev)
&& p_end == (repl_beginning ? p_max : p_ptrn_lines)
&& incomplete_line ());
p_len[p_end] = chars_read;
- if (! (p_line[p_end] = savebuf (s, chars_read))) {
+ p_line[p_end] = savebuf (s, chars_read);
+ if (chars_read && ! p_line[p_end]) {
p_end--;
return -1;
- }
+ }
context = 0;
break;
case '\t': case '\n': /* assume spaces got eaten */
@@ -1116,7 +1117,8 @@ another_hunk (enum diff difftype, bool rev)
&& p_end == (repl_beginning ? p_max : p_ptrn_lines)
&& incomplete_line ());
p_len[p_end] = chars_read;
- if (! (p_line[p_end] = savebuf (buf, chars_read))) {
+ p_line[p_end] = savebuf (buf, chars_read);
+ if (chars_read && ! p_line[p_end]) {
p_end--;
return -1;
}
@@ -1156,7 +1158,8 @@ another_hunk (enum diff difftype, bool rev)
&& p_end == (repl_beginning ? p_max : p_ptrn_lines)
&& incomplete_line ());
p_len[p_end] = chars_read;
- if (! (p_line[p_end] = savebuf (buf + 2, chars_read))) {
+ p_line[p_end] = savebuf (buf + 2, chars_read);
+ if (chars_read && ! p_line[p_end]) {
p_end--;
return -1;
}
@@ -1357,12 +1360,13 @@ another_hunk (enum diff difftype, bool rev)
ch = *buf;
s = savebuf (buf+1, --chars_read);
}
- if (!s) {
+ if (chars_read && ! s)
+ {
while (--filldst > p_ptrn_lines)
free(p_line[filldst]);
p_end = fillsrc-1;
return -1;
- }
+ }
switch (ch) {
case '-':
if (fillsrc > p_ptrn_lines) {
@@ -1392,7 +1396,7 @@ another_hunk (enum diff difftype, bool rev)
p_line[fillsrc] = s;
p_len[fillsrc++] = chars_read;
s = savebuf (s, chars_read);
- if (!s) {
+ if (chars_read && ! s) {
while (--filldst > p_ptrn_lines)
free(p_line[filldst]);
p_end = fillsrc-1;
@@ -1488,7 +1492,8 @@ another_hunk (enum diff difftype, bool rev)
format_linenum (numbuf0, p_input_line));
chars_read -= 2 + (i == p_ptrn_lines && incomplete_line ());
p_len[i] = chars_read;
- if (! (p_line[i] = savebuf (buf + 2, chars_read))) {
+ p_line[i] = savebuf (buf + 2, chars_read);
+ if (chars_read && ! p_line[i]) {
p_end = i-1;
return -1;
}
@@ -1532,7 +1537,8 @@ another_hunk (enum diff difftype, bool rev)
format_linenum (numbuf0, p_input_line));
chars_read -= 2 + (i == p_end && incomplete_line ());
p_len[i] = chars_read;
- if (! (p_line[i] = savebuf (buf + 2, chars_read))) {
+ p_line[i] = savebuf (buf + 2, chars_read);
+ if (chars_read && ! p_line[i]) {
p_end = i-1;
return -1;
}
diff --git a/tests/no-newline-triggers-assert b/tests/no-newline-triggers-assert
new file mode 100755
index 0000000..72e9464
--- /dev/null
+++ b/tests/no-newline-triggers-assert
@@ -0,0 +1,37 @@
+#! /bin/bash
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+#
+# Copying and distribution of this file, with or without modification,
+# in any medium, are permitted without royalty provided the copyright
+# notice and this notice are preserved.
+
+# Regression test for Assertion `s && size' failed in savebuf
+
+. $srcdir/test-lib.sh
+
+require_cat
+require_diff
+use_local_patch
+use_tmpdir
+
+# ==============================================================
+
+echo one > f
+
+cat > f.diff <<EOF
+--- f
++++ f
+@@ -1,2 +1,2 @@
+-1
++1a
+
+\ No newline at end of file
+EOF
+
+echo 1 > f
+
+check 'patch -p0 < f.diff' <<EOF
+patching file f
+Hunk #1 succeeded at 1 with fuzz 1.
+EOF
diff --git a/util.c b/util.c
index cc81076..38c0438 100644
--- a/util.c
+++ b/util.c
@@ -514,7 +514,9 @@ savebuf (register char const *s, register size_t size)
{
register char *rv;
- assert (s && size);
+ if (! size)
+ return NULL;
+
rv = malloc (size);
if (! rv)