Skip to content

Commit 09d7005

Browse files
committed
Refactored variable length array code to use alloca()
While VLAs are supported in C99 and C11, they do pose some portability issues so best to avoid their use. alloca() allocates on the stack frame and is free'd on exit from the function so essentially equivalent. Ticket: ENT-14208 Changelog: none (cherry picked from commit 6e95201)
1 parent e84b45e commit 09d7005

5 files changed

Lines changed: 9 additions & 9 deletions

File tree

cf-serverd/server_tls.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ bool ServerTLSPeek(ConnectionInfo *conn_info)
226226

227227
const int peek_size = CF_INBAND_OFFSET + sizeof("CAUTH");
228228

229-
char buf[peek_size];
230-
ssize_t got = recv(ConnectionInfoSocket(conn_info), buf, sizeof(buf), MSG_PEEK);
229+
char *buf = alloca(peek_size);
230+
ssize_t got = recv(ConnectionInfoSocket(conn_info), buf, peek_size, MSG_PEEK);
231231
assert(got <= peek_size);
232232
if (got < 0)
233233
{

libpromises/evalfunction.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7396,9 +7396,8 @@ static FnCallResult FnCallClassFilterCsv(EvalContext *ctx,
73967396
}
73977397
else
73987398
{
7399-
size_t const key_len = PRINTSIZE(size_t);
7400-
char key[key_len];
7401-
xsnprintf(key, key_len, "%zu", i);
7399+
char key[PRINTSIZE(size_t)];
7400+
xsnprintf(key, PRINTSIZE(size_t), "%zu", i);
74027401

74037402
JsonObjectAppendString(class_container,
74047403
key,

libpromises/ornaments.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void PromiseBanner(EvalContext *ctx, const Promise *pp)
156156
}
157157

158158
const size_t n = 2*CF_MAXFRAGMENT + 3;
159-
char pretty_promise_name[n+1];
159+
char *pretty_promise_name = alloca(n+1);
160160
pretty_promise_name[0] = '\0';
161161
StringAppendAbbreviatedPromise(pretty_promise_name, pp->promiser, n, CF_MAXFRAGMENT);
162162
Log(LOG_LEVEL_VERBOSE, "P: Promiser/affected object: '%s'", pretty_promise_name);

libpromises/syslog_client.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#include <cf3.defs.h>
2929

30+
31+
#define RFC3164_LENGTH 1024
3032
/*
3133
* Set by cf-agent/cf-serverd from body agent/server control.
3234
*/
@@ -112,8 +114,7 @@ void RemoteSysLog(int log_priority, const char *log_string)
112114
}
113115
else
114116
{
115-
const size_t rfc3164_len = 1024;
116-
char message[rfc3164_len];
117+
char message[RFC3164_LENGTH];
117118
char timebuffer[26];
118119
pid_t pid = getpid();
119120

tests/acceptance/mock_package_manager.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
static char AVAILABLE_PACKAGES_FILE_NAME[PATH_MAX];
99
static char INSTALLED_PACKAGES_FILE_NAME[PATH_MAX];
1010

11-
static const int MAX_PACKAGE_ENTRY_LENGTH = 256;
11+
#define MAX_PACKAGE_ENTRY_LENGTH 256
1212

1313
#define DEFAULT_ARCHITECTURE "x666"
1414

0 commit comments

Comments
 (0)