Skip to content

Commit

Permalink
alloca: remove in favour of malloc and friends
Browse files Browse the repository at this point in the history
alloca() has never been part of the ANSI-C standard, and its use in fvwm
has always been limited.

Rather than keep this around, switch to use malloc() and similar.
  • Loading branch information
ThomasAdam committed Oct 30, 2022
1 parent 8d52d10 commit 4549690
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 76 deletions.
12 changes: 0 additions & 12 deletions fvwm/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@

/* ---------------------------- local definitions -------------------------- */

#ifndef C_ALLOCA
#undef alloca
#define alloca(x) do { } while (0)
#endif

#ifndef XUrgencyHint
#define XUrgencyHint (1L << 8)
#endif
Expand Down Expand Up @@ -4244,13 +4239,6 @@ void dispatch_event(XEvent *e)
(*event_group->jump_table[e->type - event_group->base])(&ea);
exc_destroy_context(ea.exc);
}

/* If we're using the C version of alloca, see if anything needs to be
* freed up.
*/
alloca(0);

return;
}

/* ewmh configure request */
Expand Down
38 changes: 10 additions & 28 deletions fvwm/menustyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ static MenuStyle *menustyle_parse_old_style(F_CMD_ARGS)
char *buffer, *rest;
char *fore, *back, *stipple, *font, *style, *animated;
MenuStyle *ms = NULL;
size_t len;

rest = GetNextToken(action,&fore);
rest = GetNextToken(rest,&back);
Expand All @@ -345,9 +344,7 @@ static MenuStyle *menustyle_parse_old_style(F_CMD_ARGS)
}
else
{
len = strlen(action) + 100;
buffer = alloca(len);
snprintf(buffer, len,
xasprintf(&buffer,
"* \"%s\", Foreground \"%s\", Background \"%s\", "
"Greyed \"%s\", Font \"%s\", \"%s\"",
style, fore, back, stipple, font,
Expand All @@ -357,35 +354,20 @@ static MenuStyle *menustyle_parse_old_style(F_CMD_ARGS)
"The old MenuStyle syntax has been deprecated. "
"Use 'MenuStyle %s' instead of 'MenuStyle %s'\n",
buffer, action);

action = buffer;
ms = menustyle_parse_style(F_PASS_ARGS);
}

if (fore)
{
free(fore);
}
if (back)
{
free(back);
}
if (stipple)
{
free(stipple);
}
if (font)
{
free(font);
}
if (style)
{
free(style);
}
if (animated)
{
free(animated);
free(buffer);
}

free(fore);
free(back);
free(stipple);
free(font);
free(style);
free(animated);

return ms;
}

Expand Down
13 changes: 6 additions & 7 deletions fvwm/module_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -1115,8 +1115,7 @@ void CMD_ModuleSynchronous(F_CMD_ARGS)
token = PeekToken(next, &next);
if (token)
{
expect = alloca(strlen(token) + 1);
strcpy(expect, token);
expect = fxstrdup(token);
}
action = next;
token = PeekToken(action, &next);
Expand All @@ -1131,21 +1130,21 @@ void CMD_ModuleSynchronous(F_CMD_ARGS)
else
{
fvwm_debug(__func__, "illegal timeout");
return;
goto done;
}
}

if (!action)
{
/* no module name */
return;
goto done;
}

module = do_execute_module(F_PASS_ARGS, False, False);
if (module == NULL)
{
/* executing the module failed, just return */
return;
goto done;
}

/* Busy cursor stuff */
Expand Down Expand Up @@ -1267,8 +1266,8 @@ void CMD_ModuleSynchronous(F_CMD_ARGS)
{
UngrabEm(GRAB_BUSY);
}

return;
done:
free(expect);
}

/* mask handling - does this belong here? */
Expand Down
8 changes: 4 additions & 4 deletions fvwm/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ set_sm_properties(FSmcConn sm_conn, char *filename, char hint)
FSmPropValue prop1val, prop2val, prop3val, prop4val, prop7val;
struct passwd *pwd;
char *user_id;
char *discardCommand;
char screen_num[32];
int numVals, i, priority = 30;
Bool is_xsm_detected = False;
Expand Down Expand Up @@ -890,9 +891,7 @@ set_sm_properties(FSmcConn sm_conn, char *filename, char hint)
should be LISTofARRAY8 on posix systems, but xsm
demands that it be ARRAY8.
*/
size_t len = 10 + strlen(filename);
char *discardCommand = alloca(len);
snprintf (discardCommand, len, "rm -f '%s'", filename);
xasprintf(&discardCommand, "rm -f '%s'", filename);
prop7.type = FSmARRAY8;
prop7.num_vals = 1;
prop7.vals = &prop7val;
Expand Down Expand Up @@ -937,7 +936,8 @@ set_sm_properties(FSmcConn sm_conn, char *filename, char hint)
{
FSmcSetProperties (sm_conn, 5, props);
}
free ((char *) prop5.vals);
free((char *) prop5.vals);
free(discardCommand);
}

static void
Expand Down
4 changes: 2 additions & 2 deletions libs/Bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,7 @@ KeySym FvwmStringToKeysym(Display *dpy, char *key)
}
else
{
s = alloca(strlen(key) + 1);
strcpy(s, key);
s = fxstrdup(key);
/* always prefer the lower case spelling if it exists */
*s = tolower(*s);
keysym = XStringToKeysym(s);
Expand All @@ -837,6 +836,7 @@ KeySym FvwmStringToKeysym(Display *dpy, char *key)
*s = toupper(*s);
keysym = XStringToKeysym(s);
}
free(s);
}
if (keysym == NoSymbol || XKeysymToKeycode(dpy, keysym) == 0)
{
Expand Down
25 changes: 18 additions & 7 deletions libs/Graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void do_relieve_rectangle_with_rotation(
}
max_w = min((w + 1) / 2, line_width);
max_h = min((h + 1) / 2, line_width);
seg = (XSegment*)alloca((sizeof(XSegment) * line_width) * 2);
seg = fxmalloc(sizeof(XSegment) * line_width * 2);
/* from 0 to the lesser of line_width & just over half w */

/* left */
Expand Down Expand Up @@ -147,6 +147,9 @@ void do_relieve_rectangle_with_rotation(
}
XDrawSegments(dpy, d, shadow_gc, seg, cur);

if (seg)
XFree(seg);

return;
}

Expand Down Expand Up @@ -535,7 +538,7 @@ static XColor *AllocNonlinearGradient(
XColor *xcs = fxmalloc(sizeof(XColor) * npixels);
int i;
int curpixel = 0;
int *seg_end_colors;
int *seg_end_colors = NULL;
int seg_sum = 0;
float color_sum = 0.0;

Expand All @@ -545,7 +548,8 @@ static XColor *AllocNonlinearGradient(
"Gradients must specify at least one segment and"
" two colors\n");
free(xcs);
return NULL;
xcs = NULL;
goto done;
}
for (i = 0; i < npixels; i++)
{
Expand All @@ -559,7 +563,7 @@ static XColor *AllocNonlinearGradient(
}

/* calculate the index of a segment's las color */
seg_end_colors = alloca(nsegs * sizeof(int));
seg_end_colors = fxmalloc(nsegs * sizeof(int));
if (nsegs == 1)
{
seg_end_colors[0] = npixels - 1;
Expand All @@ -580,7 +584,9 @@ static XColor *AllocNonlinearGradient(
" > npixels - 1 (%d)."
" Gradient drawing aborted\n",
seg_end_colors[nsegs - 1], npixels - 1);
return NULL;
free(xcs);
xcs = NULL;
goto done;
}
/* take care of rounding errors */
seg_end_colors[nsegs - 1] = npixels - 1;
Expand Down Expand Up @@ -610,7 +616,8 @@ static XColor *AllocNonlinearGradient(
if (!c && (n - skip_first_color) != 0)
{
free(xcs);
return NULL;
xcs = NULL;
goto done;
}
for (j = skip_first_color; j < n; ++j)
{
Expand All @@ -632,9 +639,13 @@ static XColor *AllocNonlinearGradient(
" npixels %d, n %d\n",
nsegs, i, curpixel,
seg_end_colors[i],npixels,n);
return NULL;
free(xcs);
xcs = NULL;
goto done;
}
}
done:
free(seg_end_colors);
return xcs;
}

Expand Down
13 changes: 8 additions & 5 deletions libs/Module.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void SendText(int *fd, const char *message, unsigned long window)

/* Get enough memory to store the entire message. */
len = strlen(message);
p = buf = alloca(sizeof(long) * (3 + 1 + (len / sizeof(long))));
p = buf = fxmalloc(sizeof(long) * (3 + 1 + (len / sizeof(long))));

/* Put the message in the buffer, and... */
*((unsigned long *)p) = window;
Expand All @@ -166,6 +166,7 @@ void SendText(int *fd, const char *message, unsigned long window)
n = write(fd[0], buf, p - buf);
(void)n;
}
free(buf);
}

/*
Expand All @@ -183,13 +184,14 @@ void SendFvwmPipe(int *fd, const char *message, unsigned long window)

while ((temp = strchr(hold, ',')) != NULL)
{
char *temp_msg = (char*)alloca(temp - hold + 1);
char *temp_msg = fxmalloc(temp - hold + 1);

strncpy(temp_msg, hold, (temp - hold));
temp_msg[(temp - hold)] = '\0';
hold = temp + 1;

SendText(fd, temp_msg, window);
free(temp_msg);
}

/*
Expand Down Expand Up @@ -241,11 +243,12 @@ static int first_pass = 1;

void InitGetConfigLine(int *fd, char *match)
{
size_t len = strlen(match) + 32;
char *buffer = alloca(len);
char *buffer;

first_pass = 0; /* make sure get wont do this */
snprintf(buffer, len, "Send_ConfigInfo %s", match);
xasprintf(&buffer, "Send_ConfigInfo %s", match);
SendText(fd, buffer, 0);
free(buffer);
}


Expand Down
7 changes: 0 additions & 7 deletions libs/fvwmsignal.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,6 @@ fvwmSelect(fd_set_size_t nfds,
{
volatile int iRet = -1; /* This variable MUST NOT be in a register */

#ifdef C_ALLOCA
/* If we're using the C version of alloca, see if anything needs to be
* freed up.
*/
alloca(0);
#endif

/*
* Yes, we trash errno here, but you're only supposed to check
* errno immediately after a function fails anyway. If we fail,
Expand Down
6 changes: 2 additions & 4 deletions modules/FvwmPager/x_pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -1047,10 +1047,7 @@ void UpdateWindowShape(void)
shape_count =
ndesks + ((m->virtual_scr.CurrentDesk < desk1 || m->virtual_scr.CurrentDesk >desk2) ? 0 : 1);

shape = (XRectangle *)alloca (shape_count * sizeof (XRectangle));

if (shape == NULL)
return;
shape = fxmalloc(shape_count * sizeof (XRectangle));

cnt = 0;
y_pos = (LabelsBelow ? 0 : label_h);
Expand Down Expand Up @@ -1084,6 +1081,7 @@ void UpdateWindowShape(void)

FShapeCombineRectangles(
dpy, Scr.Pager_w, FShapeBounding, 0, 0, shape, shape_count, FShapeSet, 0);
free(shape);
}
}

Expand Down

0 comments on commit 4549690

Please sign in to comment.