Hello,
as reported in https://bugzilla.redhat.com/show_bug.cgi?id=502026, in dd
is an unnecessary memory allocation for input block even if input block
buffer is not needed (skip and count set to 0). Attached patch should
prevent memory exhaustion error in that very special case. I guess
mentioning in NEWS is not required in that case...

Greetings,
         Ondřej Vašík
From 5c9811896936341f462f67fd192748cac2f49dbd Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Ond=C5=99ej=20Va=C5=A1=C3=ADk?= <[email protected]>
Date: Fri, 22 May 2009 09:47:34 +0200
Subject: [PATCH] dd: do not unnecesarilly allocate memory for input block size, when not needed

* src/dd.c: do not allocate memory when count=0 and skip=0
  (and input block buffer is not needed)
* tests/dd/no-allocate: new test to check this change
* tests/Makefile.am: run that test
---
 src/dd.c             |    4 ++++
 tests/Makefile.am    |    1 +
 tests/dd/no-allocate |   34 ++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 0 deletions(-)
 create mode 100755 tests/dd/no-allocate

diff --git a/src/dd.c b/src/dd.c
index 3ba616b..be6d544 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -1553,6 +1553,10 @@ dd_copy (void)
   int exit_status = EXIT_SUCCESS;
   size_t n_bytes_read;
 
+  /* Do not unnecessarily allocate memory */
+  if (max_records == 0 && skip_records == 0)
+    return exit_status;
+
   /* Leave at least one extra byte at the beginning and end of `ibuf'
      for conv=swab, but keep the buffer address even.  But some peculiar
      device drivers work only with word-aligned buffers, so leave an
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5591331..fd2fc90 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -291,6 +291,7 @@ TESTS =						\
   cp/symlink-slash				\
   cp/thru-dangling				\
   dd/misc					\
+  dd/no-allocate				\
   dd/not-rewound				\
   dd/reblock					\
   dd/skip-seek					\
diff --git a/tests/dd/no-allocate b/tests/dd/no-allocate
new file mode 100755
index 0000000..19629f2
--- /dev/null
+++ b/tests/dd/no-allocate
@@ -0,0 +1,34 @@
+#!/bin/sh
+# make sure that dd doesn't allocate memory unnecessarily
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+  set -x
+  dd --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+#count and skip is zero, we don't need to allocate memory for input block
+(ulimit -v 10000;dd if=/dev/zero of=x bs=10M seek=1 count=0) || fail=1
+
+#skip is not zero, we need to allocate input block size (and we should fail)
+(ulimit -v 10000;dd if=/dev/zero of=x bs=10M seek=1 skip=1 count=0) && fail=1
+
+Exit $fail
-- 
1.5.6.1.156.ge903b

Attachment: signature.asc
Description: Toto je digitálně podepsaná část zprávy

_______________________________________________
Bug-coreutils mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-coreutils

Reply via email to