chiark / gitweb /
Import gnupg2_2.1.17-3.debian.tar.bz2
[gnupg2.git] / debian / patches / 0017-Replace-use-of-variable-length-arrays.patch
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.
4
5 * common/t-iobuf.c (main): Replace variable-length-array.
6 * g10/gpgcompose.c (mksubpkt_callback): Ditto.
7 (encrypted): Ditto.
8 * g10/t-stutter.c (log_hexdump): Ditto.
9 (oracle_test): 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.
13
14 Signed-off-by: Werner Koch <wk@gnupg.org>
15 (cherry picked from commit 6b84ecbf312d98ac8cce9fe5facdc815bc742fa1)
16 ---
17  common/t-iobuf.c  |  6 ++++--
18  g10/gpgcompose.c  | 17 ++++++++++++-----
19  g10/t-stutter.c   | 17 ++++++++++-------
20  g10/tofu.c        |  6 +++---
21  scd/app-openpgp.c | 14 +++++++++++++-
22  5 files changed, 42 insertions(+), 18 deletions(-)
23
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[])
29    {
30      iobuf_t iobuf;
31      int rc;
32 -    char *content = "0123456789";
33 +    char content[] = "0123456789";
34      int n;
35      int c;
36 -    char buffer[strlen (content)];
37 +    char buffer[10];
38 +
39 +    assert (sizeof buffer == sizeof content - 1);
40  
41      iobuf = iobuf_temp_with_content (content, strlen (content));
42      assert (iobuf);
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)
48  
49    if (si->reason_for_revocation)
50      {
51 -      int l = 1 + strlen (si->reason_for_revocation);
52 -      char buf[l];
53 +      int len = 1 + strlen (si->reason_for_revocation);
54 +      char *buf;
55 +
56 +      buf = xmalloc (len);
57  
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);
61 +
62 +      build_sig_subpkt (sig, SIGSUBPKT_REVOC_REASON, buf, len);
63  
64 -      build_sig_subpkt (sig, SIGSUBPKT_REVOC_REASON, buf, l);
65 +      xfree (buf);
66      }
67  
68    if (si->features)
69 @@ -2540,10 +2544,13 @@ encrypted (const char *option, int argc, char *argv[], void *cookie)
70  
71    if (do_debug)
72      {
73 -      char buf[2 * session_key.keylen + 1];
74 +      char *buf;
75 +
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));
80 +      xfree (buf);
81      }
82  
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
86 --- a/g10/t-stutter.c
87 +++ b/g10/t-stutter.c
88 @@ -68,8 +68,8 @@ log_hexdump (byte *buffer, int length)
89      {
90        int have = length > 16 ? 16 : length;
91        int i;
92 -      char formatted[2 * have + 1];
93 -      char text[have + 1];
94 +      char formatted[2 * 16 + 1];
95 +      char text[16 + 1];
96  
97        fprintf (stderr, "%-8d ", written);
98        bin2hex (buffer, have, formatted);
99 @@ -87,10 +87,12 @@ log_hexdump (byte *buffer, int length)
100          }
101  
102        for (i = 0; i < have; i ++)
103 -        if (isprint (buffer[i]))
104 -          text[i] = buffer[i];
105 -        else
106 -          text[i] = '.';
107 +        {
108 +          if (isprint (buffer[i]))
109 +            text[i] = buffer[i];
110 +          else
111 +            text[i] = '.';
112 +        }
113        text[i] = 0;
114  
115        fprintf (stderr, "    ");
116 @@ -347,8 +349,9 @@ oracle (int debug, byte *ciphertext, int len, byte **plaintextp, byte **cfbp)
117  static int
118  oracle_test (unsigned int d, int b, int debug)
119  {
120 -  byte probe[blocksize + 2];
121 +  byte probe[32 + 2];
122  
123 +  log_assert (blocksize + 2 <= sizeof probe);
124    log_assert (d < 256 * 256);
125  
126    if (b == 1)
127 diff --git a/g10/tofu.c b/g10/tofu.c
128 index 2bded9e8d..8d535fa6c 100644
129 --- a/g10/tofu.c
130 +++ b/g10/tofu.c
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.  */
133    {
134      int fingerprint_raw_len = strlen (fingerprint) / 2;
135 -    char fingerprint_raw[fingerprint_raw_len];
136 +    char fingerprint_raw[20];
137      int len = 0;
138  
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)))
144        {
145          if (DBG_TRUST)
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);
149        }
150      else
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 **),
156      {
157        if (app->app_local->extcap.algo_attr_change)
158          {
159 -          unsigned char keyattr[oid_len];
160 +          unsigned char *keyattr;
161  
162 +          if (!oid_len)
163 +            {
164 +              err = gpg_error (GPG_ERR_INTERNAL);
165 +              goto leave;
166 +            }
167 +          keyattr = xtrymalloc (oid_len);
168 +          if (!keyattr)
169 +            {
170 +              err = gpg_error_from_syserror ();
171 +              goto leave;
172 +            }
173            keyattr[0] = algo;
174            memcpy (keyattr+1, oidbuf+1, oid_len-1);
175            err = change_keyattr (app, keyno, keyattr, oid_len, pincb, pincb_arg);
176 +          xfree (keyattr);
177            if (err)
178              goto leave;
179          }