From d34695b63e65f16f285df07f9aa13ad145dbc42e Mon Sep 17 00:00:00 2001 From: martijn Date: Fri, 7 May 2021 14:31:27 +0000 Subject: [PATCH] Fix the \x escape sequence to be limited to max 2 characters, instead of consuming as long as there are isxdigit(3) characters available. While here document it and mark it as an extension. OK millert@ --- usr.bin/printf/printf.1 | 12 +++++++++--- usr.bin/printf/printf.c | 12 ++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/usr.bin/printf/printf.1 b/usr.bin/printf/printf.1 index 73ea4645a60..37e2697810f 100644 --- a/usr.bin/printf/printf.1 +++ b/usr.bin/printf/printf.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: printf.1,v 1.34 2020/01/16 16:46:47 schwarze Exp $ +.\" $OpenBSD: printf.1,v 1.35 2021/05/07 14:31:27 martijn Exp $ .\" .\" Copyright (c) 1989, 1990 The Regents of the University of California. .\" All rights reserved. @@ -32,7 +32,7 @@ .\" .\" from: @(#)printf.1 5.11 (Berkeley) 7/24/91 .\" -.Dd $Mdocdate: January 16 2020 $ +.Dd $Mdocdate: May 7 2021 $ .Dt PRINTF 1 .Os .Sh NAME @@ -103,6 +103,11 @@ Write a backslash character. Write an 8-bit character whose ASCII value is the 1-, 2-, or 3-digit octal number .Ar num . +.It Cm \ex Ns Ar num +Write an 8-bit character whose ASCII value is +the 1- or 2-digit hexadecimal +number +.Ar num . .El .Pp Each format specification is introduced by the percent @@ -383,7 +388,8 @@ and always operates as if were set. .Pp The escape sequences -.Cm \ee +.Cm \ee , +.Cm \ex and .Cm \e' , as well as omitting the leading digit diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index 30feac0559a..ecbf4ba5927 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: printf.c,v 1.26 2016/11/18 15:53:16 schwarze Exp $ */ +/* $OpenBSD: printf.c,v 1.27 2021/05/07 14:31:27 martijn Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. @@ -275,7 +275,7 @@ static int print_escape(const char *str) { const char *start = str; - int value; + int value = 0; int c; str++; @@ -283,7 +283,7 @@ print_escape(const char *str) switch (*str) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': - for (c = 3, value = 0; c-- && isodigit(*str); str++) { + for (c = 3; c-- && isodigit(*str); str++) { value <<= 3; value += octtobin(*str); } @@ -293,14 +293,10 @@ print_escape(const char *str) case 'x': str++; - for (value = 0; isxdigit((unsigned char)*str); str++) { + for (c = 2; c-- && isxdigit((unsigned char)*str); str++) { value <<= 4; value += hextobin(*str); } - if (value > UCHAR_MAX) { - warnx ("escape sequence out of range for character"); - rval = 1; - } putchar (value); return str - start - 1; /* NOTREACHED */ -- 2.20.1