nanogui@linuxhacker.org

nanogui@linuxhacker.org


Subject: Re: textfield_settext free bug !
From: Greg Haerr
Date: Wed, 2 Feb 2000 20:21:24 -0700


: I noticed this in `ntextfield.c'. Is this a bug ?

Yep 

: 
: static void textfield_settext (NTEXTFIELD * this, const char * text)
: {   
:    int maxsize;
: 
:    if (text && strlen(text) > 256) maxsize = strlen(text) + 1;
:    else maxsize = 256;
: 
:    if (this->maxsize < maxsize) {
:       if (this->textbuf) free(this->textbuf);
:       if (this->textbuf = malloc(maxsize)) {
: this->textbuf[0] = '\0';
: this->maxsize = maxsize;
:       } else this->maxsize = 0;
:    }
:    
:    if (!this->textbuf) free(this->textbuf); <<<< !!!!
:    if (text) this->textbuf = strdup(text);
:    else this->textbuf = 0;
: 
:    if (text && this->textbuf) strcpy(this->textbuf,text);
:    this->curpos = 0;
:    this->firstpos = 0;
:    
:    n_widget_repaint(this);
: 
: }
: 
: I fail to follow what this section of code does :
: 
:    if (!this->textbuf) free(this->textbuf);
:    if (text) this->textbuf = strdup(text);
:    else this->textbuf = 0;

Well, the first if statement is wrong.  the second just sets
this->textbuf to NULL or strdup(text).  The first if
stmt is meant to free this->textbuf before overwriting it.

Vidar wrote the code some time ago, you might ask him about it.

Greg


nanogui@linuxhacker.org