Skip to content

Commit

Permalink
nanoidgen: remove dynamic allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukateras committed Jan 3, 2025
1 parent c876317 commit 09177f1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions nanoidgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <stdio.h>
#include <stdlib.h>

#define NANOIDMAXLEN 256 // GETENTROPY_MAX

int main(int argc, char *argv[]) {
size_t length = NANOIDLEN;
if (argc == 2) {
Expand All @@ -15,8 +17,9 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "nanoidgen: Minimum length 1, got: %zu\n", length);
return EXIT_FAILURE;
}
if (length > /* GETENTROPY_MAX */ 256) {
fprintf(stderr, "nanoidgen: Maximum length 256, got: %zu\n", length);
if (length > NANOIDMAXLEN) {
fprintf(stderr, "nanoidgen: Maximum length %u, got: %zu\n", NANOIDMAXLEN,
length);
return EXIT_FAILURE;
}
} else if (argc > 2) {
Expand All @@ -25,8 +28,8 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

char *id = calloc(length + 1, 1);
if (!id || nanoidgen(id, length) || puts(id) < 0) {
char id[NANOIDMAXLEN + 1] = "";
if (nanoidgen(id, length) || puts(id) < 0) {
perror("nanoidgen");
return EXIT_FAILURE;
}
Expand Down

0 comments on commit 09177f1

Please sign in to comment.