Skip to content

Commit

Permalink
lesskey: don't ignore HAVE_SNPRINTF
Browse files Browse the repository at this point in the history
While defines.h is included at lesskey.h, and it does define
HAVE_SNPRINTF, till now it was ignored while building lesskey, which
resulted in snprintf being used even when HAVE_SNPRINTF is 0.

Now we have the SNPRINTF<N> macros, copied from less.h, and used
instead of snprintf in lesskey source files.

Note that lesstest also ignores HAVE_SNPRINTF, but it's not mandatory
so currently not handled.

TODO: maybe add lesscommon.h with unified handling of snprintf.
  • Loading branch information
avih authored and gwsw committed Sep 25, 2023
1 parent 70e6374 commit 235d943
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions lesskey.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ struct lesskey_tables
};

extern int parse_lesskey(char *infile, struct lesskey_tables *tables);

/* keep in sync with less.h */
#if HAVE_SNPRINTF
#define SNPRINTF1(str, size, fmt, v1) snprintf((str), (size), (fmt), (v1))
#define SNPRINTF2(str, size, fmt, v1, v2) snprintf((str), (size), (fmt), (v1), (v2))
#define SNPRINTF3(str, size, fmt, v1, v2, v3) snprintf((str), (size), (fmt), (v1), (v2), (v3))
#define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) snprintf((str), (size), (fmt), (v1), (v2), (v3), (v4))
#else
/* Use unsafe sprintf if we don't have snprintf. */
#define SNPRINTF1(str, size, fmt, v1) sprintf((str), (fmt), (v1))
#define SNPRINTF2(str, size, fmt, v1, v2) sprintf((str), (fmt), (v1), (v2))
#define SNPRINTF3(str, size, fmt, v1, v2, v3) sprintf((str), (fmt), (v1), (v2), (v3))
#define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) sprintf((str), (fmt), (v1), (v2), (v3), (v4))
#endif
6 changes: 3 additions & 3 deletions lesskey_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ static struct lesskey_cmdname editnames[] =
static void parse_error(char *fmt, char *arg1)
{
char buf[1024];
int n = snprintf(buf, sizeof(buf), "%s: line %d: ", lesskey_file, linenum);
int n = SNPRINTF2(buf, sizeof(buf), "%s: line %d: ", lesskey_file, linenum);
if (n >= 0 && n < sizeof(buf))
snprintf(buf+n, sizeof(buf)-n, fmt, arg1);
SNPRINTF1(buf+n, sizeof(buf)-n, fmt, arg1);
++errors;
lesskey_parse_error(buf);
}
Expand Down Expand Up @@ -167,7 +167,7 @@ static char * char_string(char *buf, int ch, int lit)
buf[1] = '\0';
} else
{
snprintf(buf, CHAR_STRING_LEN, "\\x%02x", ch);
SNPRINTF1(buf, CHAR_STRING_LEN, "\\x%02x", ch);
}
return buf;
}
Expand Down

0 comments on commit 235d943

Please sign in to comment.