1 From: Werner Koch <wk@gnupg.org>
2 Date: Mon, 2 Jan 2017 13:29:18 +0100
3 Subject: Replace use of variable-length-arrays.
5 * common/t-iobuf.c (main): Replace variable-length-array.
6 * g10/gpgcompose.c (mksubpkt_callback): Ditto.
8 * g10/t-stutter.c (log_hexdump): Ditto.
10 * g10/tofu.c (get_policy): Ditto. Use "%zu" for size_t.
11 * scd/app-openpgp.c (ecc_writekey): Replace variable-length-array.
12 Check for zero length OID_LEN.
14 Signed-off-by: Werner Koch <wk@gnupg.org>
15 (cherry picked from commit 6b84ecbf312d98ac8cce9fe5facdc815bc742fa1)
17 common/t-iobuf.c | 6 ++++--
18 g10/gpgcompose.c | 17 ++++++++++++-----
19 g10/t-stutter.c | 17 ++++++++++-------
21 scd/app-openpgp.c | 14 +++++++++++++-
22 5 files changed, 42 insertions(+), 18 deletions(-)
24 diff --git a/common/t-iobuf.c b/common/t-iobuf.c
25 index 0e6f508a5..bdeab99a4 100644
26 --- a/common/t-iobuf.c
27 +++ b/common/t-iobuf.c
28 @@ -362,10 +362,12 @@ main (int argc, char *argv[])
32 - char *content = "0123456789";
33 + char content[] = "0123456789";
36 - char buffer[strlen (content)];
39 + assert (sizeof buffer == sizeof content - 1);
41 iobuf = iobuf_temp_with_content (content, strlen (content));
43 diff --git a/g10/gpgcompose.c b/g10/gpgcompose.c
44 index 512cb450a..fafbfd274 100644
45 --- a/g10/gpgcompose.c
46 +++ b/g10/gpgcompose.c
47 @@ -1654,13 +1654,17 @@ mksubpkt_callback (PKT_signature *sig, void *cookie)
49 if (si->reason_for_revocation)
51 - int l = 1 + strlen (si->reason_for_revocation);
53 + int len = 1 + strlen (si->reason_for_revocation);
56 + buf = xmalloc (len);
58 buf[0] = si->reason_for_revocation_code;
59 - memcpy (&buf[1], si->reason_for_revocation, l - 1);
60 + memcpy (&buf[1], si->reason_for_revocation, len - 1);
62 + build_sig_subpkt (sig, SIGSUBPKT_REVOC_REASON, buf, len);
64 - build_sig_subpkt (sig, SIGSUBPKT_REVOC_REASON, buf, l);
69 @@ -2540,10 +2544,13 @@ encrypted (const char *option, int argc, char *argv[], void *cookie)
73 - char buf[2 * session_key.keylen + 1];
76 + buf = xmalloc (2 * session_key.keylen + 1);
77 debug ("session key: algo: %d; keylen: %d; key: %s\n",
78 session_key.algo, session_key.keylen,
79 bin2hex (session_key.key, session_key.keylen, buf));
83 if (strcmp (option, "--encrypted-mdc") == 0)
84 diff --git a/g10/t-stutter.c b/g10/t-stutter.c
85 index a2e9666bf..359cdf622 100644
88 @@ -68,8 +68,8 @@ log_hexdump (byte *buffer, int length)
90 int have = length > 16 ? 16 : length;
92 - char formatted[2 * have + 1];
93 - char text[have + 1];
94 + char formatted[2 * 16 + 1];
97 fprintf (stderr, "%-8d ", written);
98 bin2hex (buffer, have, formatted);
99 @@ -87,10 +87,12 @@ log_hexdump (byte *buffer, int length)
102 for (i = 0; i < have; i ++)
103 - if (isprint (buffer[i]))
104 - text[i] = buffer[i];
108 + if (isprint (buffer[i]))
109 + text[i] = buffer[i];
115 fprintf (stderr, " ");
116 @@ -347,8 +349,9 @@ oracle (int debug, byte *ciphertext, int len, byte **plaintextp, byte **cfbp)
118 oracle_test (unsigned int d, int b, int debug)
120 - byte probe[blocksize + 2];
121 + byte probe[32 + 2];
123 + log_assert (blocksize + 2 <= sizeof probe);
124 log_assert (d < 256 * 256);
127 diff --git a/g10/tofu.c b/g10/tofu.c
128 index 2bded9e8d..8d535fa6c 100644
131 @@ -2457,16 +2457,16 @@ get_policy (tofu_dbs_t dbs, PKT_public_key *pk,
132 /* See if the key is signed by an ultimately trusted key. */
134 int fingerprint_raw_len = strlen (fingerprint) / 2;
135 - char fingerprint_raw[fingerprint_raw_len];
136 + char fingerprint_raw[20];
139 - if (fingerprint_raw_len != 20
140 + if (fingerprint_raw_len != sizeof fingerprint_raw
141 || ((len = hex2bin (fingerprint,
142 fingerprint_raw, fingerprint_raw_len))
143 != strlen (fingerprint)))
146 - log_debug ("TOFU: Bad fingerprint: %s (len: %zd, parsed: %d)\n",
147 + log_debug ("TOFU: Bad fingerprint: %s (len: %zu, parsed: %d)\n",
148 fingerprint, strlen (fingerprint), len);
151 diff --git a/scd/app-openpgp.c b/scd/app-openpgp.c
152 index 5fa4fd294..4d8b1bc9e 100644
153 --- a/scd/app-openpgp.c
154 +++ b/scd/app-openpgp.c
155 @@ -3580,11 +3580,23 @@ ecc_writekey (app_t app, gpg_error_t (*pincb)(void*, const char *, char **),
157 if (app->app_local->extcap.algo_attr_change)
159 - unsigned char keyattr[oid_len];
160 + unsigned char *keyattr;
164 + err = gpg_error (GPG_ERR_INTERNAL);
167 + keyattr = xtrymalloc (oid_len);
170 + err = gpg_error_from_syserror ();
174 memcpy (keyattr+1, oidbuf+1, oid_len-1);
175 err = change_keyattr (app, keyno, keyattr, oid_len, pincb, pincb_arg);