b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | From 4c39cb09735a494099fba0474d25ff26800de952 Mon Sep 17 00:00:00 2001 |
| 2 | From: Lee Duncan <lduncan@suse.com> |
| 3 | Date: Wed, 29 Jan 2020 12:47:16 -0800 |
| 4 | Subject: [PATCH] Do not ignore write() return value. |
| 5 | |
| 6 | Some distros set the warn_unused_result attribute for the write() |
| 7 | system call, so check the return value. |
| 8 | --- |
| 9 | pki.c | 37 ++++++++++++++++++++++++++++++++----- |
| 10 | 1 file changed, 32 insertions(+), 5 deletions(-) |
| 11 | |
| 12 | --- a/pki.c |
| 13 | +++ b/pki.c |
| 14 | @@ -9,12 +9,13 @@ |
| 15 | #include <unistd.h> |
| 16 | #include <limits.h> |
| 17 | #include "config.h" |
| 18 | +#include <fcntl.h> |
| 19 | +#include <assert.h> |
| 20 | #ifdef WITH_SECURITY |
| 21 | #include <openssl/pem.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/evp.h> |
| 24 | #endif |
| 25 | -#include <fcntl.h> |
| 26 | #include <libisns/isns.h> |
| 27 | #include "security.h" |
| 28 | #include <libisns/util.h> |
| 29 | @@ -431,17 +432,43 @@ isns_dsa_load_params(const char *filenam |
| 30 | return dsa; |
| 31 | } |
| 32 | |
| 33 | +/* |
| 34 | + * write one 'status' character to stdout |
| 35 | + */ |
| 36 | +static void |
| 37 | +write_status_byte(int ch) |
| 38 | +{ |
| 39 | + static int stdout_fd = 1; /* fileno(stdout) */ |
| 40 | + char buf[2]; |
| 41 | + int res; |
| 42 | + |
| 43 | + /* |
| 44 | + * We don't actually care about the return value here, since |
| 45 | + * we are just dumping a status byte to stdout, but |
| 46 | + * some linux distrubutions set the warn_unused_result attribute |
| 47 | + * for the write() API, so we might as well use the return value |
| 48 | + * to make sure the write command isn't broken. |
| 49 | + */ |
| 50 | + assert(ch); |
| 51 | + buf[0] = ch; |
| 52 | + buf[1] = '\0'; |
| 53 | + res = write(stdout_fd, buf, 1); |
| 54 | + assert(res == 1); |
| 55 | +} |
| 56 | + |
| 57 | static int |
| 58 | isns_dsa_param_gen_callback(int stage, |
| 59 | __attribute__((unused))int index, |
| 60 | __attribute__((unused))void *dummy) |
| 61 | { |
| 62 | if (stage == 0) |
| 63 | - write(1, "+", 1); |
| 64 | + write_status_byte('+'); |
| 65 | else if (stage == 1) |
| 66 | - write(1, ".", 1); |
| 67 | + write_status_byte('.'); |
| 68 | else if (stage == 2) |
| 69 | - write(1, "/", 1); |
| 70 | + write_status_byte('/'); |
| 71 | + |
| 72 | + /* as a callback, we must return a value, so just return success */ |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | @@ -478,7 +505,7 @@ isns_dsa_init_params(const char *filenam |
| 77 | dsa = DSA_generate_parameters(dsa_key_bits, NULL, 0, |
| 78 | NULL, NULL, isns_dsa_param_gen_callback, NULL); |
| 79 | #endif |
| 80 | - write(1, "\n", 1); |
| 81 | + write_status_byte('\n'); |
| 82 | |
| 83 | if (dsa == NULL) { |
| 84 | isns_dsasig_report_errors("Error generating DSA parameters", |