Skip to content

Commit

Permalink
Slightly improve fp_ident
Browse files Browse the repository at this point in the history
* do error checking.
* also print `SIZEOF_FP_DIGIT` and `DIGIT_SHIFT`.
* cut static buffer in half, currently the output string is 183 chars
  on my machine, so it's still more than enough for all the other options.

Signed-off-by: Steffen Jaeckel <[email protected]>
  • Loading branch information
sjaeckel committed Sep 21, 2024
1 parent c1cc1c3 commit 1519aa1
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/misc/fp_ident.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#define GIT_VERSION TFM_VERSION_S
#endif

#define dnstrcon(D, N, S) dnmemcpy((D), (N), S, sizeof(S)-1)
#define dnstrcon_direct(D, N, S) do { dnmemcpy((D), (N), S, sizeof(S)-1); } while(0)
#define dnstrcon(D, N, S) do { if (dnmemcpy((D), (N), S, sizeof(S)-1) == -1) goto err_out; } while(0)
static signed long dnmemcpy(char **d, size_t *n, const char *s, size_t len) {
if (len >= *n) return -1;
memcpy(*d, s, len);
Expand All @@ -22,7 +23,8 @@ static signed long dnmemcpy(char **d, size_t *n, const char *s, size_t len) {
#define U_DIGITS(T) (1 + ((sizeof(T) * 8UL) * 30103UL) / 100000UL)
#define S_DIGITS(T) (2 + ((sizeof(T) * 8UL - 1) * 30103UL) / 100000UL)

static signed long dnstrul(char **d, size_t *n, unsigned long value) {
#define dnstrul(D, N, V) do { if (dnstrul_impl((D), (N), (V)) == -1) goto err_out; } while(0)
static signed long dnstrul_impl(char **d, size_t *n, unsigned long value) {
char digits[U_DIGITS(unsigned long)+1]; /* fit digits plus null byte */
char *digit = digits + (sizeof(digits) - 1);
size_t len = 0;
Expand All @@ -39,7 +41,7 @@ static signed long dnstrul(char **d, size_t *n, unsigned long value) {

const char *fp_ident(void)
{
static char buf[1024];
static char buf[512];
char *d = buf;
size_t n = sizeof(buf);

Expand All @@ -60,10 +62,20 @@ const char *fp_ident(void)
dnstrul(&d, &n, sizeof(fp_word));
dnstrcon(&d, &n,
"\n\n"
"FP_MAX_SIZE = "
"FP_MAX_SIZE = "
);
dnstrul(&d, &n, FP_MAX_SIZE);
dnstrcon(&d, &n,
"\n"
"SIZEOF_FP_DIGIT = "
);
dnstrul(&d, &n, SIZEOF_FP_DIGIT);
dnstrcon(&d, &n,
"\n"
"DIGIT_SHIFT = "
);
dnstrul(&d, &n, DIGIT_SHIFT);
dnstrcon(&d, &n,
"\n\n"
"Defines: \n"
#ifdef __i386__
Expand Down Expand Up @@ -123,14 +135,26 @@ const char *fp_ident(void)
}

memset(d, 0, n);
return buf;
err_out:
d = buf;
n = sizeof(buf);
*d = '\0';

dnstrcon_direct(&d, &n,
"ERROR: Buffer too small.\n"
);

return buf;
}

#ifdef STANDALONE

int main(void)
{
printf("%s\n", fp_ident());
const char* ident = fp_ident();
printf("%s\n", ident);
printf("ident len: %lu\n", (unsigned long)strlen(ident));
return 0;
}

Expand Down

0 comments on commit 1519aa1

Please sign in to comment.