www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/11/07/12:41:05

Date: Sun, 7 Nov 1999 17:28:53 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Dave Dribin <drib AT enteract DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: Allegro 3.12: page fault in generate_optimized_palette()
In-Reply-To: <800gho$aut$1@eve.enteract.com>
Message-ID: <Pine.SUN.3.91.991107172835.12805h-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On 6 Nov 1999, Dave Dribin wrote:

> As you can see (max - j) ends up being negative and causing havoc on
> the memcpy().  Is this a known problem?  Am I using the function
> wrong?

Yes and yes.  memcpy has the following prototype (see <string.h> and
the documentation of memcpy):

  void * memcpy(void *_dest, const void *_src, size_t _n);

The last argument is type size_t, which is an unisigned integral
type.  So your negative value is interpreted as a very large positive
value, and causes your program to crash because you are writing
outside the limits of the array common[].

In other words, your code has a bug.

In addition, please note that memcpy is not supposed to be used for
copying overlapping regions, and your code does precisely that.  Use
memmove if the source and destination overlap.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019