public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: "Petrovski, Roman" <RPetrovski@illumina.com>
To: "cygwin-patches@cygwin.com" <cygwin-patches@cygwin.com>
Subject: RtlFillMemory fails on block sizes over 0x7fffffff
Date: Wed, 29 Jul 2015 14:10:00 -0000	[thread overview]
Message-ID: <3BD89E0BA5F96349B765DE1100906A6D016FC0267F@UKCH-PRD-EXMB01.illumina.com> (raw)

Hi, just ran into a problem which boils down to the following at least with Windows 7:

char *p = (char*)malloc(0x80000000UL);	//works fine, allocates memory as requested
memset(p, 0, 0x80000000UL);			//Watch process segfault.

The RtlFillMemory either crashes or underfills the buffer depending on the size given.
Looks like internally it treats size as a signed 4-byte integer.

Please apply the patch below or implement an alternative.

Roman.



From 60ed745b75d16755769653f19882335ef69960dd Mon Sep 17 00:00:00 2001
From: Roman Petrovski <rpetrovski@illumina.com>
Date: Wed, 29 Jul 2015 06:45:45 -0700
Subject: [PATCH] RtlFillMemory fails on block sizes over 0x7fffffff

---
 winsup/cygwin/miscfuncs.cc | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/miscfuncs.cc b/winsup/cygwin/miscfuncs.cc
index 4a7a1b8..7308498 100644
--- a/winsup/cygwin/miscfuncs.cc
+++ b/winsup/cygwin/miscfuncs.cc
@@ -904,17 +904,35 @@ err:
 extern "C" void NTAPI RtlFillMemory (PVOID, SIZE_T, BYTE);
 extern "C" void NTAPI RtlCopyMemory (PVOID, const VOID *, SIZE_T);

+
+static const size_t RTL_MAX_SIZE = 0x7fffffff;
 extern "C" void *
 memset (void *s, int c, size_t n)
 {
-  RtlFillMemory (s, n, c);
+  char *p = (char*)s;
+  while (n)
+  {
+    size_t size = min(RTL_MAX_SIZE, n);
+    RtlFillMemory (p, size, c);
+    p += size;
+    n -= size;
+  }
   return s;
 }

 extern "C" void *
 memcpy(void *__restrict dest, const void *__restrict src, size_t n)
 {
-  RtlCopyMemory (dest, src, n);
+  char *d = (char*)dest;
+  char *s = (char*)src;
+  while (n)
+  {
+    size_t size = min(RTL_MAX_SIZE, n);
+    RtlCopyMemory (d, s, n);
+    d += size;
+    s += size;
+    n -= size;
+  }
   return dest;
 }
 #endif
--
2.4.5

             reply	other threads:[~2015-07-29 14:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-29 14:10 Petrovski, Roman [this message]
2015-07-29 16:21 ` Corinna Vinschen
2015-07-29 16:28   ` Petrovski, Roman
2015-07-29 20:21     ` Corinna Vinschen

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=3BD89E0BA5F96349B765DE1100906A6D016FC0267F@UKCH-PRD-EXMB01.illumina.com \
    --to=rpetrovski@illumina.com \
    --cc=cygwin-patches@cygwin.com \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).