Skip to content

Commit

Permalink
*: code cleanup: remove if (x) free checks
Browse files Browse the repository at this point in the history
The C standard has always allowed the following to work:

free(NULL);

So there's no longer a need to do this:

if (x)
    free(x);

Via "elfring" who provided the cocci script on which this change is
based.

Fixes #108
  • Loading branch information
ThomasAdam committed Aug 30, 2023
1 parent 58f2d04 commit 15bd91b
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 108 deletions.
5 changes: 5 additions & 0 deletions contrib/coccinelle/remove_unneccessary_pointer_checks.cocci
@@ -0,0 +1,5 @@
@Remove_unnecessary_pointer_checks@
expression x;
@@
-if (\(x != 0 \| x != NULL\))
free(x);
12 changes: 4 additions & 8 deletions libs/Flocale.c
Expand Up @@ -1474,8 +1474,7 @@ FlocaleFont *FlocaleLoadFont(Display *dpy, char *fontname, char *module)
default:
break;
}
if (opt_str != NULL)
free(opt_str);
free(opt_str);
}
if (str && *str)
{
Expand Down Expand Up @@ -1647,10 +1646,8 @@ void FlocaleUnloadFont(Display *dpy, FlocaleFont *flf)
}
if (flf->flags.must_free_fc)
{
if (flf->fc->x)
free(flf->fc->x);
if (flf->fc->bidi)
free(flf->fc->bidi);
free(flf->fc->x);
free(flf->fc->bidi);
if (flf->fc->locale != NULL)
{
while (FLC_GET_LOCALE_CHARSET(flf->fc,i) != NULL)
Expand Down Expand Up @@ -2105,8 +2102,7 @@ void FlocaleDrawString(
if(comb_chars != NULL)
{
free(comb_chars);
if(pixel_pos)
free(pixel_pos);
free(pixel_pos);
}

return;
Expand Down
8 changes: 3 additions & 5 deletions modules/FvwmButtons/parse.c
Expand Up @@ -728,7 +728,7 @@ static void ParseContainer(char **ss,button_info *b)
}
break;
case 5: /* Fore */
if (b->c->fore) free(b->c->fore);
free(b->c->fore);
b->c->fore = seekright(&s);
b->c->flags.b_Fore = (b->c->fore ? 1 : 0);
break;
Expand Down Expand Up @@ -807,8 +807,7 @@ static void ParseContainer(char **ss,button_info *b)
char *temp;

temp = seekright(&s);
if (temp)
free(temp);
free(temp);
}
}
break;
Expand Down Expand Up @@ -854,8 +853,7 @@ static void ParseContainer(char **ss,button_info *b)
t = seekright(&s);
fvwm_debug(__func__, "%s: Illegal container option \"%s\"\n",MyName,
(t)?t:"");
if (t)
free(t);
free(t);
}
}
if (*s)
Expand Down
36 changes: 12 additions & 24 deletions modules/FvwmForm/FvwmForm.c
Expand Up @@ -468,21 +468,18 @@ static void ct_Geometry(char *cp)
}
static void ct_Fore(char *cp)
{
if (color_names[c_fg])
free(color_names[c_fg]);
free(color_names[c_fg]);
color_names[c_fg] = fxstrdup(cp);
colorset = -1;
myfprintf((stderr, "ColorFore: %s\n", color_names[c_fg]));
}
static void ct_Back(char *cp)
{
if (color_names[c_bg])
free(color_names[c_bg]);
free(color_names[c_bg]);
color_names[c_bg] = fxstrdup(cp);
if (bg_state == 'd')
{
if (screen_background_color)
free(screen_background_color);
free(screen_background_color);
screen_background_color = fxstrdup(color_names[c_bg]);
bg_state = 's'; /* indicate set by command */
}
Expand All @@ -497,16 +494,14 @@ static void ct_Colorset(char *cp)
}
static void ct_ItemFore(char *cp)
{
if (color_names[c_item_fg])
free(color_names[c_item_fg]);
free(color_names[c_item_fg]);
color_names[c_item_fg] = fxstrdup(cp);
itemcolorset = -1;
myfprintf((stderr, "ColorItemFore: %s\n", color_names[c_item_fg]));
}
static void ct_ItemBack(char *cp)
{
if (color_names[c_item_bg])
free(color_names[c_item_bg]);
free(color_names[c_item_bg]);
color_names[c_item_bg] = fxstrdup(cp);
itemcolorset = -1;
myfprintf((stderr, "ColorItemBack: %s\n", color_names[c_item_bg]));
Expand All @@ -518,22 +513,19 @@ static void ct_ItemColorset(char *cp)
}
static void ct_Font(char *cp)
{
if (font_names[f_text])
free(font_names[f_text]);
free(font_names[f_text]);
CopyStringWithQuotes(&font_names[f_text], cp);
myfprintf((stderr, "Font: %s\n", font_names[f_text]));
}
static void ct_TimeoutFont(char *cp)
{
if (font_names[f_timeout])
free(font_names[f_timeout]);
free(font_names[f_timeout]);
CopyStringWithQuotes(&font_names[f_timeout], cp);
myfprintf((stderr, "TimeoutFont: %s\n", font_names[f_timeout]));
}
static void ct_ButtonFont(char *cp)
{
if (font_names[f_button])
free(font_names[f_button]);
free(font_names[f_button]);
CopyStringWithQuotes(&font_names[f_button], cp);
myfprintf((stderr, "ButtonFont: %s\n", font_names[f_button]));
}
Expand All @@ -559,8 +551,7 @@ static void ct_ButtonPointer(char *cp)
}
static void ct_InputFont(char *cp)
{
if (font_names[f_input])
free(font_names[f_input]);
free(font_names[f_input]);
CopyStringWithQuotes(&font_names[f_input], cp);
myfprintf((stderr, "InputFont: %s\n", font_names[f_input]));
}
Expand Down Expand Up @@ -1051,12 +1042,10 @@ static void PutDataInForm(char *cp)
do {
if (strcasecmp(var_name,item->header.name) == 0) {
var_len = strlen(cp);
if (item->input.init_value)
free(item->input.init_value);
free(item->input.init_value);
item->input.init_value = fxmalloc(var_len + 1);
strcpy(item->input.init_value,cp); /* new initial value in field */
if (item->input.value)
free(item->input.value);
free(item->input.value);
item->input.buf = var_len+1;
item->input.value = fxmalloc(item->input.buf);
strcpy(item->input.value,cp); /* new value in field */
Expand Down Expand Up @@ -1666,8 +1655,7 @@ void RedrawTimeout(Item *item)
item->header.dt_ptr->dt_Fstr->colorset = &Colorset[colorset];
item->header.dt_ptr->dt_Fstr->flags.has_colorset = True;
}
if (item->header.dt_ptr->dt_Fstr->str != NULL)
free(item->header.dt_ptr->dt_Fstr->str);
free(item->header.dt_ptr->dt_Fstr->str);
item->header.dt_ptr->dt_Fstr->str = fxstrdup(tmpbuf);
item->header.dt_ptr->dt_Fstr->x = item->header.pos_x + TEXT_SPC;
item->header.dt_ptr->dt_Fstr->y = item->header.pos_y + ( CF.padVText / 2 ) +
Expand Down
72 changes: 24 additions & 48 deletions modules/FvwmPager/FvwmPager.c
Expand Up @@ -837,14 +837,10 @@ void list_destroy(unsigned long *body)
XDestroyWindow(dpy,t->IconView);
if(FocusWin == t)
FocusWin = NULL;
if(t->res_class != NULL)
free(t->res_class);
if(t->res_name != NULL)
free(t->res_name);
if(t->window_name != NULL)
free(t->window_name);
if(t->icon_name != NULL)
free(t->icon_name);
free(t->res_class);
free(t->res_name);
free(t->window_name);
free(t->icon_name);
free(t);
}
}
Expand Down Expand Up @@ -1309,18 +1305,15 @@ void list_window_name(unsigned long *body,unsigned long type)
{
switch (type) {
case M_RES_CLASS:
if(t->res_class != NULL)
free(t->res_class);
free(t->res_class);
CopyString(&t->res_class,(char *)(&body[3]));
break;
case M_RES_NAME:
if(t->res_name != NULL)
free(t->res_name);
free(t->res_name);
CopyString(&t->res_name,(char *)(&body[3]));
break;
case M_VISIBLE_NAME:
if(t->window_name != NULL)
free(t->window_name);
free(t->window_name);
CopyString(&t->window_name,(char *)(&body[3]));
break;
}
Expand Down Expand Up @@ -1368,8 +1361,7 @@ void list_icon_name(unsigned long *body)
}
if(t!= NULL)
{
if(t->icon_name != NULL)
free(t->icon_name);
free(t->icon_name);
CopyString(&t->icon_name,(char *)(&body[3]));
/* repaint by clearing window */
if ((FwindowFont != NULL) && (t->icon_name != NULL)
Expand Down Expand Up @@ -2025,8 +2017,7 @@ void ParseOptions(void)
}
else if (StrEquals(resource, "Font"))
{
if (font_string)
free(font_string);
free(font_string);
CopyStringWithQuotes(&font_string, next);
if(strncasecmp(font_string,"none",4) == 0)
{
Expand All @@ -2042,17 +2033,15 @@ void ParseOptions(void)
{
if(Pdepth > 1)
{
if (PagerFore)
free(PagerFore);
free(PagerFore);
CopyString(&PagerFore,arg1);
}
}
else if (StrEquals(resource, "Back"))
{
if(Pdepth > 1)
{
if (PagerBack)
free(PagerBack);
free(PagerBack);
CopyString(&PagerBack,arg1);
}
}
Expand Down Expand Up @@ -2203,15 +2192,13 @@ void ParseOptions(void)
{
if(Pdepth > 1)
{
if (HilightC)
free(HilightC);
free(HilightC);
CopyString(&HilightC,arg1);
}
}
else if (StrEquals(resource, "SmallFont"))
{
if (smallFont)
free(smallFont);
free(smallFont);
CopyStringWithQuotes(&smallFont, next);
if (strncasecmp(smallFont,"none",4) == 0)
{
Expand Down Expand Up @@ -2263,14 +2250,10 @@ void ParseOptions(void)
{
if (Pdepth > 1)
{
if (WindowFore)
free(WindowFore);
if (WindowBack)
free(WindowBack);
if (WindowHiFore)
free(WindowHiFore);
if (WindowHiBack)
free(WindowHiBack);
free(WindowFore);
free(WindowBack);
free(WindowHiFore);
free(WindowHiBack);
CopyString(&WindowFore, arg1);
CopyString(&WindowBack, arg2);
tline2 = GetNextToken(tline2, &WindowHiFore);
Expand Down Expand Up @@ -2311,8 +2294,7 @@ void ParseOptions(void)
}
else if (StrEquals(resource,"WindowLabelFormat"))
{
if (WindowLabelFormat)
free(WindowLabelFormat);
free(WindowLabelFormat);
CopyString(&WindowLabelFormat,arg1);
}
else if (StrEquals(resource,"UseSkipList"))
Expand Down Expand Up @@ -2345,8 +2327,7 @@ void ParseOptions(void)
-- ric@giccs.georgetown.edu */
else if (StrEquals(resource, "Balloons"))
{
if (BalloonTypeString)
free(BalloonTypeString);
free(BalloonTypeString);
CopyString(&BalloonTypeString, arg1);

if ( strncasecmp(BalloonTypeString, "Pager", 5) == 0 ) {
Expand All @@ -2372,8 +2353,7 @@ void ParseOptions(void)
{
if (Pdepth > 1)
{
if (BalloonBack)
free(BalloonBack);
free(BalloonBack);
CopyString(&BalloonBack, arg1);
}
}
Expand All @@ -2382,23 +2362,20 @@ void ParseOptions(void)
{
if (Pdepth > 1)
{
if (BalloonFore)
free(BalloonFore);
free(BalloonFore);
CopyString(&BalloonFore, arg1);
}
}

else if (StrEquals(resource, "BalloonFont"))
{
if (BalloonFont)
free(BalloonFont);
free(BalloonFont);
CopyStringWithQuotes(&BalloonFont, next);
}

else if (StrEquals(resource, "BalloonBorderColor"))
{
if (BalloonBorderColor)
free(BalloonBorderColor);
free(BalloonBorderColor);
CopyString(&BalloonBorderColor, arg1);
}

Expand All @@ -2424,8 +2401,7 @@ void ParseOptions(void)
}
else if (StrEquals(resource,"BalloonStringFormat"))
{
if (BalloonFormatString)
free(BalloonFormatString);
free(BalloonFormatString);
CopyString(&BalloonFormatString,arg1);
}

Expand Down
5 changes: 1 addition & 4 deletions modules/FvwmPager/x_pager.c
Expand Up @@ -2800,10 +2800,7 @@ static void do_label_window(PagerWindow *t, Window w)
{
return;
}

/* Update the window label for this window */
if (t->window_label)
free(t->window_label);
free(t->window_label);
t->window_label = GetBalloonLabel(t, WindowLabelFormat);
if (w != None)
{
Expand Down

0 comments on commit 15bd91b

Please sign in to comment.