Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove VLA #1273

Merged
merged 5 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions OpenFilesScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,8 @@ static void OpenFilesScreen_scan(InfoScreen* this) {
OpenFiles_FileData* fdata = pdata->files;
while (fdata) {
OpenFiles_Data* data = &fdata->data;
size_t lenN = strlen(getDataForType(data, 'n'));
size_t sizeEntry = 5 + 7 + 4 + 10 + 10 + 10 + 10 + lenN + 8 /*spaces*/ + 1 /*null*/;
char entry[sizeEntry];
xSnprintf(entry, sizeof(entry), "%5.5s %-7.7s %-4.4s %-10.10s %10.10s %10.10s %10.10s %s",
char* entry = NULL;
xAsprintf(&entry, "%5.5s %-7.7s %-4.4s %-10.10s %10.10s %10.10s %10.10s %s",
getDataForType(data, 'f'),
getDataForType(data, 't'),
getDataForType(data, 'a'),
Expand All @@ -265,6 +263,7 @@ static void OpenFilesScreen_scan(InfoScreen* this) {
getDataForType(data, 'i'),
getDataForType(data, 'n'));
InfoScreen_addLine(this, entry);
free(entry);
OpenFiles_Data_clear(data);
OpenFiles_FileData* old = fdata;
fdata = fdata->next;
Expand Down
5 changes: 2 additions & 3 deletions darwin/PlatformHelpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ double Platform_calculateNanosecondsPerMachTick(void) {
* the "Apple M1" chip specifically when running under Rosetta 2.
*/

size_t cpuBrandStringSize = 1024;
char cpuBrandString[cpuBrandStringSize];
Platform_getCPUBrandString(cpuBrandString, cpuBrandStringSize);
char cpuBrandString[1024] = "";
Platform_getCPUBrandString(cpuBrandString, sizeof(cpuBrandString));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically not VLA but this change helps readability anyway :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um, actually … ;-) Although this uses a constant number (which the compiler can deduce), syntactically it iswas.

BTW: I did a grep --color=always -nRP '\b(?!return)\w+ \w+\[[^\]0-9A-Z_]'|less -R through the source and had a look at the remaining locations found by this. Quite a nice way to quickly get through the source without straight up working with the AST …


bool isRunningUnderRosetta2 = Platform_isRunningTranslated();

Expand Down
8 changes: 6 additions & 2 deletions linux/LibSensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sensors/sensors.h>

#include "Macros.h"
Expand Down Expand Up @@ -143,7 +144,8 @@ static int tempDriverPriority(const sensors_chip_name* chip) {

void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, unsigned int activeCPUs) {
assert(existingCPUs > 0 && existingCPUs < 16384);
double data[existingCPUs + 1];

double* data = xMallocArray(existingCPUs + 1, sizeof(double));
for (size_t i = 0; i < existingCPUs + 1; i++)
data[i] = NAN;

Expand Down Expand Up @@ -264,6 +266,8 @@ void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, uns
out:
for (unsigned int i = 0; i <= existingCPUs; i++)
cpus[i].temperature = data[i];

free(data);
}

#endif /* HAVE_SENSORS_SENSORS_H */
4 changes: 3 additions & 1 deletion openbsd/OpenBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static void OpenBSDMachine_scanMemoryInfo(OpenBSDMachine* this) {
*/
int nswap = swapctl(SWAP_NSWAP, 0, 0);
if (nswap > 0) {
struct swapent swdev[nswap];
struct swapent* swdev = xMallocArray(nswap, sizeof(struct swapent));
int rnswap = swapctl(SWAP_STATS, swdev, nswap);

/* Total things up */
Expand All @@ -177,6 +177,8 @@ static void OpenBSDMachine_scanMemoryInfo(OpenBSDMachine* this) {

super->totalSwap = total;
super->usedSwap = used;

free(swdev);
} else {
super->totalSwap = super->usedSwap = 0;
}
Expand Down