Trimming top's header to display width

Giorgos Keramidas keramida at freebsd.org
Fri May 13 05:39:56 PDT 2005


On 2005-05-13 08:33, Andre Guibert de Bruet <andy at siliconlandmark.com> wrote:
> On Thu, 12 May 2005, Giorgos Keramidas wrote:
> >Does the diff below look like a good way to fix this?
> >
> >%%%
> >+char *
> >+trim_header(text)
> >+
> >+char *text;
> >+
> >+{
> >+	char *s;
> >+	int width;
> >+
> >+	s = NULL;
>
> Setting s to NULL is useless because malloc returns either NULL on failure
> or a pointer to the allocated memory segment.

Setting s to NULL unconditionally means that even if the if block right
below doesn't run, the pointer *WILL* be initialized to something
meaningful.

>
> >+	width = display_width;
> >+	header_length = strlen(text);
> >+	if (header_length >= width) {

This part won't run all the time.  If it doesn't, then `s' could
potentially hold garbage from the stack and the callers of trim_header()
would find it very surprising that their non-NULL pointer causes a
segfault in free() :-)

> Other than that pet-peeve, the patch looks good to me...

Cool :)



More information about the freebsd-current mailing list