[cc65] calloc() not failing

From: Marc 'BlackJack' Rintsch <marc1rintsch.de>
Date: 2012-11-12 10:19:02
Hi *,

Is it me or the library?  I have a problem with `calloc()` either not 
failing with a NULL return value or even crashing the system with 
writing zeroes in places it shouldn't.

I reduced the real program to the following::

 #include <stdio.h>
 #include <stdlib.h>

 int main(void)
 {
     if (!calloc(250, 104)) {
         puts("Error");
     }
     return 0;
 }

First time I run this, the program returns to the BASIC interpreter 
without printing the "Error" I expect.  On the next run the system hangs 
after half of the video memory is filled with @ signs, so it looks like 
`calloc()` fills memory it clearly shouldn't.  This pattern of the two 
runs is reproducible regardless of the compiler options for 
optimizations.  A glance at the `calloc()` source reveals there is no 
check for overflow of the multiplication of the arguments.  IMHO it 
should‽

Background of the code snippet: Of course both values are not hard coded 
in the real program.  The count is user input and the size belongs to a 
struct.  I let the user input a count and want to tell him the count is 
too high if `calloc()` fails.  As there is no function in the C standard 
to ask beforehand how much free memory there is, I thought that is the 
way to go.

Ciao,
	Marc 'BlackJack' Rintsch

-- 
“Money is a new form of slavery, and distinguishable from
 the old simply by the fact that it is impersonal --
 that there is no human relation between master and slave.”
                     -- Leo Tolstoy, What shall We Do Then?
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Mon Nov 12 10:19:30 2012

This archive was generated by hypermail 2.1.8 : 2012-11-12 10:19:36 CET