Skip to contentSkip to conversation

LZMA: Avoid free on null pointer.

PATCH New Luigi 'Comio' Mantellini 5 participants 13 messages Dec 5, 2010 – Dec 6, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

Hi ML,

This patch has been moved out from the previous "[PATCH 0/6 v2] Support for Bootstrap Code" patchset.

This change should be safe because just add some check in order to avoid some possible free on null pointers in the LZMA decoder initialization. I noticed the error tracing all malloc/free of the code during a code profiling session.

Kindly, add this fix in the master branch.

best regards,

luigi

Luigi 'Comio' Mantellini (1):
LZMA: Avoid free on null pointer.

 lib/lzma/LzmaDec.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
1.7.2.3
#1 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

On structure Initialization, LZMA code tries to free the dictionary and probs buffers, also when these are null pointers. Add some check in order to prevent the free on null pointers.

---
 lib/lzma/LzmaDec.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
lib/lzma/LzmaDec.c +4 -2
index f941da2..e2dab44 100644
@@ -960,7 +960,8 @@ static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAl
   UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
   if (p->probs == 0 || numProbs != p->numProbs)
   {
-    LzmaDec_FreeProbs(p, alloc);
+    if (p->probs)
+      LzmaDec_FreeProbs(p, alloc);
     p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
     p->numProbs = numProbs;
     if (p->probs == 0)
@@ -987,7 +988,8 @@ SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAll
   dicBufSize = propNew.dicSize;
   if (p->dic == 0 || dicBufSize != p->dicBufSize)
   {
-    LzmaDec_FreeDict(p, alloc);
+    if (p->dic)
+      LzmaDec_FreeDict(p, alloc);
     p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
     if (p->dic == 0)
     {
Signature · 1 line
1.7.2.3
#2 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

Quoted 6 lines

On structure Initialization, LZMA code tries to free the dictionary and probs buffers, also when these are null pointers. Add some check in order to prevent the free on null pointers.

Why not move these NULL checks inside LzmaDec_FreeProbs? Then you don't have to litter the code with NULL checks and LzmaDec_FreeProbs behaves like the standard free() function.

Jocke

#3 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

Quoted 10 lines · Joakim Tjernlund
Quoted 6 lines

On structure Initialization, LZMA code tries to free the dictionary and probs buffers, also when these are null pointers. Add some check in order to prevent the free on null pointers.

Why not move these NULL checks inside LzmaDec_FreeProbs? Then you don't have to litter the code with NULL checks and LzmaDec_FreeProbs behaves like the standard free() function.

I don't agree with this. We have problem only on initialization and should be the caller to check if buffer are already allocated (then free them) or not.
This check is on the "init behavior" and not on "robustness" of free code.

My 2cents.

luigi

Quoted 8 lines

?Jocke

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Signature · 10 lines
Luigi 'Comio' Mantellini
R&D - Software
Industrie Dial Face S.p.A.
Via Canzo, 4
20068 Peschiera Borromeo (MI), Italy

Tel.: +39 02 5167 2813
Fax: +39 02 5167 2459
web: www.idf-hit.com
mail: luigi.mantellini at idf-hit.com
#4 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

Luigi Mantellini <[email protected]> wrote on 2010/12/05 11:19:58:

Quoted 9 lines
Quoted 7 lines · Joakim Tjernlund
Quoted 6 lines

On structure Initialization, LZMA code tries to free the dictionary and probs buffers, also when these are null pointers. Add some check in order to prevent the free on null pointers.

Quoted 8 lines
Quoted 3 lines

Why not move these NULL checks inside LzmaDec_FreeProbs? Then you don't have to litter the code with NULL checks and LzmaDec_FreeProbs behaves like the standard free() function.

I don't agree with this. We have problem only on initialization and should be the caller to check if buffer are already allocated (then free them) or not.
This check is on the "init behavior" and not on "robustness" of free code.

Well, that is not how malloc/free works. It would be nice if LZMA followed the same rules to minimize any confusion. There may be other places that need this check too and you end up with lots of NULL checks that just litters the code.

Jocke

#5 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

In reply to Luigi 'Comio' Mantellini
Quoted 3 lines · On Sunday, December 05, 2010 04:18:44 Luigi 'Comio' Mantellini wrote:

On structure Initialization, LZMA code tries to free the dictionary and probs buffers, also when these are null pointers. Add some check in order to prevent the free on null pointers.

your patch only checks p->probs, not any dictionary buffer. if you follow the code path:

...
void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc) {
alloc->Free(alloc, p->probs);
p->probs = 0;
}
...

    g_Alloc.Free = SzFree;

...
static void SzFree(void *p, void *address) { p = p; free(address); }
...

this only ends up doing free(p->probs) which is free(NULL) which isnt a bug.

so you're going to need to provide some more details.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20101206/90633527/attachment.pgp

#6 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

Quoted 20 lines · Mike Frysinger
Quoted 3 lines · On Sunday, December 05, 2010 04:18:44 Luigi 'Comio' Mantellini wrote:

On structure Initialization, LZMA code tries to free the dictionary and probs buffers, also when these are null pointers. Add some check in order to prevent the free on null pointers.

your patch only checks p->probs, not any dictionary buffer. ?if you follow the code path:

...
void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
{
?alloc->Free(alloc, p->probs);
?p->probs = 0;
}
...
? ?g_Alloc.Free = SzFree;
...
static void SzFree(void *p, void *address) { p = p; free(address); }
...

Quoted 2 lines

this only ends up doing free(p->probs) which is free(NULL) which isnt a bug.

In general I prefer avoid to free a null pointer, and I consider a free on a not-malloc-eted pointer a bug. The submitted patch check the pointers (p->probs and p->dict) only at init time. This is sufficient to avoid the free(NULL). The other *Free(*) calls all called on pointers that are surely not null.

The second way should be to add the null check into the FreeProbs and FreeDict functions.

best regards,

luigi

Quoted 9 lines

so you're going to need to provide some more details. -mike

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Signature · 10 lines
Luigi 'Comio' Mantellini
R&D - Software
Industrie Dial Face S.p.A.
Via Canzo, 4
20068 Peschiera Borromeo (MI), Italy

Tel.: +39 02 5167 2813
Fax: +39 02 5167 2459
web: www.idf-hit.com
mail: luigi.mantellini at idf-hit.com
#7 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

Quoted 21 lines · On Monday, December 06, 2010 03:59:44 Luigi Mantellini wrote:
Quoted 20 lines · Mike Frysinger
Quoted 3 lines · On Sunday, December 05, 2010 04:18:44 Luigi 'Comio' Mantellini wrote:

On structure Initialization, LZMA code tries to free the dictionary and probs buffers, also when these are null pointers. Add some check in order to prevent the free on null pointers.

your patch only checks p->probs, not any dictionary buffer. if you follow the code path:

...
void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
{
alloc->Free(alloc, p->probs);
p->probs = 0;
}
...
g_Alloc.Free = SzFree;
...
static void SzFree(void *p, void *address) { p = p; free(address); }
...

Quoted 5 lines
Quoted 2 lines

this only ends up doing free(p->probs) which is free(NULL) which isnt a bug.

In general I prefer avoid to free a null pointer, and I consider a free on a not-malloc-eted pointer a bug.

sorry, but this is not an acceptable reason. so unless you have an actual error report here, your patch gets NAK-ed.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20101206/9e0e07a3/attachment.pgp

#8 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

Quoted 5 lines · Mike Frysinger

sorry, but this is not an acceptable reason. ?so unless you have an actual error report here, your patch gets NAK-ed.
-mike

Hi mike,

my pov is different: free should (must) be called only on already allocated pointers. I know that free code checks at begin if ptr is null or not. Anyway I don't understand why a null pointer check before to call free cannot be added to the code... it's safe and follows the logical flow of the code.

I received warning from my debugger during activities on other things, and I added this fix to my code to turn-off "possible free on null pointer" warning from my debugger.

my 2Cents,

ciao ciao

luigi

Signature · 10 lines
Luigi 'Comio' Mantellini
R&D - Software
Industrie Dial Face S.p.A.
Via Canzo, 4
20068 Peschiera Borromeo (MI), Italy

Tel.: +39 02 5167 2813
Fax: +39 02 5167 2459
web: www.idf-hit.com
mail: luigi.mantellini at idf-hit.com
#9 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

Quoted 8 lines · Mike Frysinger
Quoted 5 lines

sorry, but this is not an acceptable reason. ?so unless you have an actual error report here, your patch gets NAK-ed.
-mike

Quoted 7 lines

Hi mike,

my pov is different: free should (must) be called only on already allocated pointers. I know that free code checks at begin if ptr is null or not. Anyway I don't understand why a null pointer check before to call free cannot be added to the code... it's safe and follows the logical flow of the code.

I think you will find this is not the majority view and you will have to adapt. free(NULL) is fine and the reason for that is so that you can avoid litter the code with a ton of NULL checks.

Jocke

#10 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

Hi Joakim,

We have not "ton" but just two points to fix. 1) when free dict before to allocate another one, and 2) when free probs before to allocate another one. These scenarios are not used into u-boot code, because the library is "one shot" and the other free are called (if called) just to deallocate all structures (that are surely not null).

best regards,

luigi

Quoted 23 lines · Joakim Tjernlund
Quoted 8 lines · Mike Frysinger
Quoted 5 lines

sorry, but this is not an acceptable reason. ?so unless you have an actual error report here, your patch gets NAK-ed.
-mike

Quoted 7 lines

Hi mike,

my pov is different: free should (must) be called only on already allocated pointers. I know that free code checks at begin if ptr is null or not. Anyway I don't understand why a null pointer check before to call free cannot be added to the code... it's safe and follows the logical flow of the code.

I think you will find this is not the majority view and you will have to adapt. free(NULL) is fine and the reason for that is so that you can avoid litter the code with a ton of NULL checks.

? ?Jocke

Signature · 10 lines
Luigi 'Comio' Mantellini
R&D - Software
Industrie Dial Face S.p.A.
Via Canzo, 4
20068 Peschiera Borromeo (MI), Italy

Tel.: +39 02 5167 2813
Fax: +39 02 5167 2459
web: www.idf-hit.com
mail: luigi.mantellini at idf-hit.com
#11 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

Luigi Mantellini <[email protected]> wrote on 2010/12/06 10:47:21:

Quoted 8 lines

Hi Joakim,

We have not "ton" but just two points to fix. 1) when free dict before to allocate another one, and 2) when free probs before to allocate another one. These scenarios are not used into u-boot code, because the library is "one shot" and the other free are called (if called) just to deallocate all structures (that are surely not null).

There is nothing to fix, just move on. I will and consider this matter closed.

Quoted 44 lines

best regards,

luigi

Quoted 16 lines · Joakim Tjernlund
Quoted 8 lines · Mike Frysinger
Quoted 5 lines

sorry, but this is not an acceptable reason. ?so unless you have an actual error report here, your patch gets NAK-ed.
-mike

Quoted 7 lines

Hi mike,

my pov is different: free should (must) be called only on already allocated pointers. I know that free code checks at begin if ptr is null or not. Anyway I don't understand why a null pointer check before to call free cannot be added to the code... it's safe and follows the logical flow of the code.

Quoted 7 lines

I think you will find this is not the majority view and you will have to adapt. free(NULL) is fine and the reason for that is so that you can avoid litter the code with a ton of NULL checks.

? ?Jocke

--
Luigi 'Comio' Mantellini
R&D - Software
Industrie Dial Face S.p.A.
Via Canzo, 4
20068 Peschiera Borromeo (MI), Italy

Tel.: +39 02 5167 2813
Fax: +39 02 5167 2459
web: www.idf-hit.com
mail: luigi.mantellini at idf-hit.com

#12 lore raw

PATCH 1/1 LZMA: Avoid free on null pointer.

In reply to Luigi Mantellini

Dear Luigi Mantellini,

Quoted 10 lines · In message <[email protected]> you…

my pov is different: free should (must) be called only on already allocated pointers. I know that free code checks at begin if ptr is null or not. Anyway I don't understand why a null pointer check before to call free cannot be added to the code... it's safe and follows the logical flow of the code.

I received warning from my debugger during activities on other things, and I added this fix to my code to turn-off "possible free on null pointer" warning from my debugger.

free(NULL) has a well defined behaviour: "If ptr is NULL, no operation is performed."

Seems your debugger is over-cautious. This may be OK for debugging, but is no good reason to change the code.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
It is easier to change the specification to fit the program than vice
versa.