From: Reinhard Meyer <u-boot@emk-elektronik.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH] Prevent malloc with size 0
Date: Fri, 22 Oct 2010 09:18:02 +0200 [thread overview]
Message-ID: <4CC13AAA.6090601@emk-elektronik.de> (raw)
In-Reply-To: <OFFEF252D4.0F48F752-ONC12577C4.001FD864-C12577C4.0021E5D5@transmode.se>
Dear Joakim Tjernlund,
> Mike Frysinger <vapier@gentoo.org> wrote on 2010/10/21 21:51:53:
>> On Thursday, October 21, 2010 07:45:10 Joakim Tjernlund wrote:
>>> Wolfgang Denk wrote on 2010/10/21 13:32:54:
>>>> Joakim Tjernlund you wrote:
>>>>>> - if ((long)bytes < 0) return 0;
>>>>>> + if ((long)bytes <= 0) return 0;
>>>>> I think you should return some impossible ptr value =! NULL
>>>>> Size 0 not really an error.
>>>> It is legal for malloc() to return NULL in case of size==0,
>>>> and for the sake of simplicity I recommend we do just that.
>>> Yes, but not very useful. Glibc does not return NULL
>> it is useful for malloc(0) == NULL. the glibc behavior is downright
>> obnoxious. we disable this for uClibc and dont see problems. if
> anything, we
>> catch accidental programming mistakes which then get fixed.
My five cents:
> There is a value in having the possibility to express a
> 0 bytes data set. Consider this simple example:
> An app read lines from a file and mallocs each line read and builds an
> array with malloced pointers. The last entry is a NULL ptr to
> signal EOF. This breaks down for empty lines if malloc(0)
> returns NULL.
Your example is in the right way, but a bit flawed in its simplicity.
Even empty lines need some form of information that they are of length zero,
be it a 0x00 in the memory line itself (requiring malloc(length+1)) or that same
information in a variable somewhere else:
struct line {
byte *buf;
int length;
} lines[...];
As an (undercover) Mathematician:
Out of principle I would say that malloc(0) should return a non-NULL
pointer of an area where exactly 0 bytes may be used. And, of course,
free() of that area shall not fail or crash the system.
> Not to mention error handling, as I recall, a malloc(0) that returns NULL
> does not set errno which screws error handling. One have to bend over
> just to cope with this.
>> why exactly do you want malloc(0) to return valid memory ? i would
> rather
>> have u-boot return an error.
In the case of u-boot, where a driver or whatever should never really need
to allocate zero memory, such a programming error should be made obvious by
at least a warning message.
> Ideally it should return a ptr to invalid memory so you get a SEGV if you
> try to defer the ptr but I take anything over a NULL ptr.
Makes sense only if any access outside of any allocated memory would behave
the same, otherwise this is a special case again.
Best Regards,
Reinhard
next prev parent reply other threads:[~2010-10-22 7:18 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-10-21 9:24 [U-Boot] [PATCH] Prevent malloc with size 0 Kostaras Nikolaos
2010-10-21 11:25 ` Joakim Tjernlund
2010-10-21 11:32 ` Wolfgang Denk
2010-10-21 11:45 ` Joakim Tjernlund
2010-10-21 11:51 ` Wolfgang Denk
2010-10-21 11:56 ` Joakim Tjernlund
2010-10-21 12:02 ` Wolfgang Denk
2010-10-21 12:54 ` Joakim Tjernlund
2010-10-21 19:51 ` Mike Frysinger
2010-10-21 21:10 ` Graeme Russ
2010-10-21 21:27 ` Mike Frysinger
2012-03-31 19:59 ` Marek Vasut
2012-04-01 12:25 ` Joakim Tjernlund
2012-04-01 14:01 ` Marek Vasut
2012-04-01 14:15 ` Joakim Tjernlund
2012-04-01 14:21 ` Marek Vasut
2012-04-01 22:40 ` Graeme Russ
2012-04-01 23:45 ` Marek Vasut
2012-04-01 23:52 ` Graeme Russ
2012-04-02 0:13 ` Marek Vasut
2012-04-02 0:25 ` Graeme Russ
2012-04-02 1:04 ` Marek Vasut
2012-04-02 1:40 ` Graeme Russ
2012-04-02 2:51 ` Marek Vasut
2012-04-02 3:05 ` Graeme Russ
2012-04-02 6:39 ` Joakim Tjernlund
2012-04-02 3:12 ` Mike Frysinger
2012-04-02 3:16 ` Graeme Russ
2012-04-02 3:36 ` Marek Vasut
2012-04-02 3:43 ` Graeme Russ
2012-04-02 4:23 ` Marek Vasut
2012-04-02 4:27 ` Graeme Russ
2012-04-02 6:55 ` Joakim Tjernlund
2012-04-02 7:17 ` Graeme Russ
2012-04-02 7:40 ` Joakim Tjernlund
2012-04-02 14:05 ` Marek Vasut
2012-04-02 14:26 ` Joakim Tjernlund
2012-04-02 14:42 ` Marek Vasut
2012-04-02 15:08 ` Joakim Tjernlund
2012-04-02 15:23 ` Marek Vasut
2012-04-02 16:00 ` Joakim Tjernlund
2012-04-02 16:39 ` Marek Vasut
2012-04-02 17:22 ` Joakim Tjernlund
2012-04-02 18:00 ` Marek Vasut
2012-04-02 18:40 ` Joakim Tjernlund
2012-04-02 19:14 ` Mike Frysinger
2012-04-02 21:02 ` Joakim Tjernlund
2012-04-02 19:23 ` Marek Vasut
2012-04-02 20:28 ` Graeme Russ
2012-04-02 20:56 ` Joakim Tjernlund
2012-04-02 20:59 ` Graeme Russ
2012-04-02 21:14 ` Joakim Tjernlund
2012-04-02 23:35 ` Graeme Russ
2012-04-03 10:35 ` Graeme Russ
2012-10-16 6:31 ` Marek Vasut
2012-10-16 9:22 ` Joakim Tjernlund
2012-10-16 10:43 ` Marek Vasut
2012-10-16 11:46 ` Joakim Tjernlund
2012-10-16 10:43 ` Wolfgang Denk
2012-10-16 22:41 ` Graeme Russ
2012-04-02 3:10 ` Mike Frysinger
2012-04-02 3:36 ` Marek Vasut
2010-10-22 6:10 ` Joakim Tjernlund
2010-10-22 7:18 ` Reinhard Meyer [this message]
2010-10-22 7:47 ` Joakim Tjernlund
2010-10-22 7:20 ` Mike Frysinger
2010-10-22 7:37 ` Joakim Tjernlund
2010-10-22 7:55 ` Mike Frysinger
2010-10-22 8:34 ` Joakim Tjernlund
2010-10-22 15:18 ` Mike Frysinger
2010-10-22 16:40 ` Joakim Tjernlund
2010-10-22 17:06 ` Mike Frysinger
2010-10-23 9:14 ` Joakim Tjernlund
2010-10-22 17:36 ` Scott Wood
2010-10-23 9:23 ` Joakim Tjernlund
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=4CC13AAA.6090601@emk-elektronik.de \
--to=u-boot@emk-elektronik.de \
--cc=u-boot@lists.denx.de \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.