[<<] [<] Page 1 of 1 [>] [>>] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Subject:
textfield_settext free bug !
From: Chris Johns ####@####.#### Date: 2 Feb 2000 23:55:15 -0000 Message-Id: <3898C173.A6F4CDBB@acm.org> Hi all, I noticed this in `ntextfield.c'. Is this a bug ? 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; -- Chris Johns, ####@####.#### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Subject:
Re: textfield_settext free bug !
From: "Greg Haerr" ####@####.#### Date: 3 Feb 2000 03:32:19 -0000 Message-Id: <050c01bf6df5$c0926d20$15320cd0@gregh> : 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[<<] [<] Page 1 of 1 [>] [>>] |