Re: usr/bin/ktrace: replace snprintf(3)/write(2) with #define and write(2)

2017-06-11 Thread Ted Unangst
Adam Wolk wrote:
> Hi tech@,
> 
> Using the GREATSCOTT[1] pattern to output in the ktrace signal handler,
> dropping the need for an snprintf and the 8k stack buffer.
> 
> Brought to attention by BlackFrog on #openbsd-daily
> 
> Feedback, OK's?

so mistake in the original: sizeof() includes the nul, which isn't what you
want. strlen() is likely to be optimized and gives the correct answer.

also, direct write() calls are mostly used in libraries because it can be very
bad to get tangled up in stdio, but this is a secondary concern in application
code. signal handlers are still a special case, however.



Re: usr/bin/ktrace: replace snprintf(3)/write(2) with #define and write(2)

2017-06-11 Thread Adam Wolk
On Sun, Jun 11, 2017 at 11:10:30AM -0600, Theo de Raadt wrote:
> +   write(STDERR_FILENO, NO_KTRACE, sizeof(NO_KTRACE));
> 
> Naw, I dislike that sizeof.
> 
> You can use dprintf, it is signal-safe in OpenBSD as long as the format
> string doesn't contain floating-point strings.

Attaching updated diff.

? ktrace
? ktrace.1.diff
? ktrace.diff
Index: ktrace.c
===
RCS file: /cvs/src/usr.bin/ktrace/ktrace.c,v
retrieving revision 1.33
diff -u -p -r1.33 ktrace.c
--- ktrace.c18 Jul 2016 09:36:50 -  1.33
+++ ktrace.c11 Jun 2017 17:17:59 -
@@ -250,10 +250,7 @@ usage(void)
 static void
 no_ktrace(int signo)
 {
-   char buf[8192];
-
-   snprintf(buf, sizeof(buf),
+   dprintf(STDERR_FILENO,
 "error:\tktrace() system call not supported in the running 
kernel\n\tre-compile kernel with 'option KTRACE'\n");
-   write(STDERR_FILENO, buf, strlen(buf));
_exit(1);
 }


Re: usr/bin/ktrace: replace snprintf(3)/write(2) with #define and write(2)

2017-06-11 Thread Theo de Raadt
+   write(STDERR_FILENO, NO_KTRACE, sizeof(NO_KTRACE));

Naw, I dislike that sizeof.

You can use dprintf, it is signal-safe in OpenBSD as long as the format
string doesn't contain floating-point strings.



usr/bin/ktrace: replace snprintf(3)/write(2) with #define and write(2)

2017-06-11 Thread Adam Wolk
Hi tech@,

Using the GREATSCOTT[1] pattern to output in the ktrace signal handler,
dropping the need for an snprintf and the 8k stack buffer.

Brought to attention by BlackFrog on #openbsd-daily

Feedback, OK's?

Regards,
Adam

[1] - https://marc.info/?l=openbsd-tech=149613049920485=2
Index: ktrace.c
===
RCS file: /cvs/src/usr.bin/ktrace/ktrace.c,v
retrieving revision 1.33
diff -u -p -r1.33 ktrace.c
--- ktrace.c18 Jul 2016 09:36:50 -  1.33
+++ ktrace.c11 Jun 2017 16:58:32 -
@@ -250,10 +250,7 @@ usage(void)
 static void
 no_ktrace(int signo)
 {
-   char buf[8192];
-
-   snprintf(buf, sizeof(buf),
-"error:\tktrace() system call not supported in the running 
kernel\n\tre-compile kernel with 'option KTRACE'\n");
-   write(STDERR_FILENO, buf, strlen(buf));
+#define NO_KTRACE "error:\tktrace() system call not supported in the running 
kernel\n\tre-compile kernel with 'option KTRACE'\n"
+   write(STDERR_FILENO, NO_KTRACE, sizeof(NO_KTRACE));
_exit(1);
 }