hexdump(1): Exit gracefully on format strings missing conversion

PR:		237263
Submitted by:	Bojan Petrovic <bojan_petrovic AT fastmail.fm>
This commit is contained in:
Conrad Meyer 2019-04-13 16:51:48 +00:00
parent 084e97b866
commit 8fb93ac95d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=346187
3 changed files with 33 additions and 3 deletions

View file

@ -88,6 +88,7 @@ void addfile(const char *);
void badcnt(const char *);
void badconv(const char *);
void badfmt(const char *);
void badnoconv(void);
void badsfmt(void);
void bpad(PR *);
void conv_c(PR *, u_char *, size_t);

View file

@ -169,7 +169,10 @@ size(FS *fs)
* skip any special chars -- save precision in
* case it's a %s format.
*/
while (strchr(spec + 1, *++fmt));
while (*++fmt != 0 && strchr(spec + 1, *fmt) != NULL)
;
if (*fmt == 0)
badnoconv();
if (*fmt == '.' && isdigit(*++fmt)) {
prec = atoi(fmt);
while (isdigit(*++fmt));
@ -241,10 +244,16 @@ rewrite(FS *fs)
if (fu->bcnt) {
sokay = USEBCNT;
/* Skip to conversion character. */
for (++p1; strchr(spec, *p1); ++p1);
while (*++p1 != 0 && strchr(spec, *p1) != NULL)
;
if (*p1 == 0)
badnoconv();
} else {
/* Skip any special chars, field width. */
while (strchr(spec + 1, *++p1));
while (*++p1 != 0 && strchr(spec + 1, *p1) != NULL)
;
if (*p1 == 0)
badnoconv();
if (*p1 == '.' && isdigit(*++p1)) {
sokay = USEPREC;
prec = atoi(p1);
@ -512,3 +521,9 @@ badconv(const char *ch)
{
errx(1, "%%%s: bad conversion character", ch);
}
void
badnoconv(void)
{
errx(1, "missing conversion character");
}

View file

@ -176,6 +176,19 @@ x_flag_body()
hexdump -x "$(atf_get_srcdir)/d_hexdump_c.in"
}
atf_test_case no_conv_err
no_conv_err()
{
atf_set "descr" "Verify missing conversion char error handling"
}
no_conv_err_body()
{
atf_check -s exit:1 -e ignore \
hexdump -e '"%"'
atf_check -s exit:1 -e ignore \
hexdump -e '4/2 "%"'
}
atf_init_test_cases()
{
atf_add_test_case b_flag
@ -188,4 +201,5 @@ atf_init_test_cases()
atf_add_test_case s_flag
atf_add_test_case v_flag
atf_add_test_case x_flag
atf_add_test_case no_conv_err
}