summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2014-04-15 13:35:10 +0200
committerNikos Mavrogiannopoulos <nmav@redhat.com>2014-04-15 14:12:09 +0200
commitf946b20caa4f36727e0f65e9799a871ad5307d18 (patch)
tree5f53fe03683570a9a95c6252b7949910a8ffa691
parent7e9486e064c9ea355e287c9f8160e4e0a21589e9 (diff)
downloadgnutls-f946b20caa4f36727e0f65e9799a871ad5307d18.tar.gz
Corrected bugs reported from coverity in opencdk.
-rw-r--r--lib/opencdk/armor.c52
-rw-r--r--lib/opencdk/kbnode.c11
-rw-r--r--lib/opencdk/keydb.c4
-rw-r--r--lib/opencdk/literal.c22
-rw-r--r--lib/opencdk/main.h1
-rw-r--r--lib/opencdk/misc.c12
-rw-r--r--lib/opencdk/new-packet.c10
-rw-r--r--lib/opencdk/stream.c10
8 files changed, 47 insertions, 75 deletions
diff --git a/lib/opencdk/armor.c b/lib/opencdk/armor.c
index 5d978f5864..be56b871e2 100644
--- a/lib/opencdk/armor.c
+++ b/lib/opencdk/armor.c
@@ -156,7 +156,9 @@ static int compress_get_algo(cdk_stream_t inp, int *r_zipalgo)
int nread, pkttype;
size_t plain_size;
- *r_zipalgo = 0;
+ if (r_zipalgo)
+ *r_zipalgo = 0;
+
cdk_stream_seek(inp, 0);
while (!cdk_stream_eof(inp)) {
nread = _cdk_stream_gets(inp, buf, DIM(buf) - 1);
@@ -186,50 +188,6 @@ static int compress_get_algo(cdk_stream_t inp, int *r_zipalgo)
}
-static int check_armor(cdk_stream_t inp, int *r_zipalgo)
-{
- char buf[4096];
- size_t nread;
- int check;
-
- check = 0;
- nread = cdk_stream_read(inp, buf, DIM(buf) - 1);
- if (nread > 0) {
- buf[nread] = '\0';
- if (strstr(buf, "-----BEGIN PGP")) {
- compress_get_algo(inp, r_zipalgo);
- check = 1;
- }
- cdk_stream_seek(inp, 0);
- }
- return check;
-}
-
-
-static int is_armored(int ctb)
-{
- int pkttype = 0;
-
- if (!(ctb & 0x80)) {
- gnutls_assert();
- return 1; /* invalid packet: assume it is armored */
- }
- pkttype = ctb & 0x40 ? (ctb & 0x3f) : ((ctb >> 2) & 0xf);
- switch (pkttype) {
- case CDK_PKT_MARKER:
- case CDK_PKT_ONEPASS_SIG:
- case CDK_PKT_PUBLIC_KEY:
- case CDK_PKT_SECRET_KEY:
- case CDK_PKT_PUBKEY_ENC:
- case CDK_PKT_SIGNATURE:
- case CDK_PKT_LITERAL:
- case CDK_PKT_COMPRESSED:
- return 0; /* seems to be a regular packet: not armored */
- }
- return 1;
-}
-
-
static u32 update_crc(u32 crc, const byte * buf, size_t buflen)
{
unsigned int j;
@@ -258,8 +216,8 @@ static cdk_error_t armor_encode(void *data, FILE * in, FILE * out)
gnutls_assert();
return CDK_Inv_Value;
}
- if (afx->idx < 0 || afx->idx > (int) DIM(armor_begin) ||
- afx->idx2 < 0 || afx->idx2 > (int) DIM(armor_end)) {
+ if (afx->idx < 0 || afx->idx >= (int) DIM(armor_begin) ||
+ afx->idx2 < 0 || afx->idx2 >= (int) DIM(armor_end)) {
gnutls_assert();
return CDK_Inv_Value;
}
diff --git a/lib/opencdk/kbnode.c b/lib/opencdk/kbnode.c
index b09c64a804..c28cb349bc 100644
--- a/lib/opencdk/kbnode.c
+++ b/lib/opencdk/kbnode.c
@@ -417,7 +417,7 @@ cdk_kbnode_write_to_mem_alloc(cdk_kbnode_t node,
cdk_kbnode_t n;
cdk_stream_t s;
cdk_error_t rc;
- size_t len;
+ ssize_t len;
if (!node || !r_buf || !r_buflen) {
gnutls_assert();
@@ -453,6 +453,9 @@ cdk_kbnode_write_to_mem_alloc(cdk_kbnode_t node,
cdk_stream_seek(s, 0);
len = cdk_stream_get_length(s);
+ if (len == 0)
+ return gnutls_assert_val(CDK_General_Error);
+
*r_buf = cdk_calloc(1, len);
*r_buflen = cdk_stream_read(s, *r_buf, len);
cdk_stream_close(s);
@@ -477,7 +480,7 @@ cdk_kbnode_write_to_mem(cdk_kbnode_t node, byte * buf, size_t * r_nbytes)
cdk_kbnode_t n;
cdk_stream_t s;
cdk_error_t rc;
- size_t len;
+ ssize_t len;
if (!node || !r_nbytes) {
gnutls_assert();
@@ -509,7 +512,11 @@ cdk_kbnode_write_to_mem(cdk_kbnode_t node, byte * buf, size_t * r_nbytes)
}
cdk_stream_seek(s, 0);
+
len = cdk_stream_get_length(s);
+ if (len == 0)
+ return gnutls_assert_val(CDK_General_Error);
+
if (!buf) {
*r_nbytes = len; /* Only return the length of the buffer */
cdk_stream_close(s);
diff --git a/lib/opencdk/keydb.c b/lib/opencdk/keydb.c
index 9724e8ad46..64eebf0344 100644
--- a/lib/opencdk/keydb.c
+++ b/lib/opencdk/keydb.c
@@ -225,7 +225,7 @@ static cdk_error_t
keydb_idx_search(cdk_stream_t inp, u32 * keyid, const byte * fpr,
off_t * r_off)
{
- key_idx_t idx;
+ key_idx_t idx = 0;
if (!inp || !r_off) {
gnutls_assert();
@@ -757,8 +757,6 @@ void cdk_keydb_search_release(cdk_keydb_search_t st)
if (st->idx)
cdk_stream_close(st->idx);
- if (!st)
- return;
if (st->type == CDK_DBSEARCH_EXACT
|| st->type == CDK_DBSEARCH_SUBSTR)
cdk_free(st->u.pattern);
diff --git a/lib/opencdk/literal.c b/lib/opencdk/literal.c
index d7facdf78e..c47f360c5c 100644
--- a/lib/opencdk/literal.c
+++ b/lib/opencdk/literal.c
@@ -187,12 +187,14 @@ static cdk_error_t literal_encode(void *data, FILE * in, FILE * out)
filelen = strlen(pfx->filename);
cdk_pkt_new(&pkt);
pt = pkt->pkt.literal = cdk_calloc(1, sizeof *pt + filelen);
- pt->name = (char *) pt + sizeof(*pt);
- if (!pt) {
+ if (pt == NULL) {
cdk_pkt_release(pkt);
cdk_stream_close(si);
- return CDK_Out_Of_Core;
+ return gnutls_assert_val(CDK_Out_Of_Core);
}
+
+ pt->name = (char *) pt + sizeof(*pt);
+
memcpy(pt->name, pfx->filename, filelen);
pt->namelen = filelen;
pt->name[pt->namelen] = '\0';
@@ -230,6 +232,20 @@ int _cdk_filter_literal(void *data, int ctl, FILE * in, FILE * out)
return CDK_Inv_Mode;
}
+/* Remove all trailing white spaces from the string. */
+static void _cdk_trim_string(char *s)
+{
+ int len = strlen(s);
+ unsigned i;
+
+ for (i=len-1;i>=0;i--) {
+ if ((s[i] == '\t' || s[i] == '\r' || s[i] == '\n' || s[i] == ' ')) {
+ s[i] = 0;
+ } else {
+ break;
+ }
+ }
+}
static int text_encode(void *data, FILE * in, FILE * out)
{
diff --git a/lib/opencdk/main.h b/lib/opencdk/main.h
index eaf7589c1e..d95f277862 100644
--- a/lib/opencdk/main.h
+++ b/lib/opencdk/main.h
@@ -136,7 +136,6 @@ cdk_error_t _cdk_keydb_check_userid(cdk_keydb_hd_t hd, u32 * keyid,
/*-- sign.c --*/
int _cdk_sig_hash_for(cdk_pkt_pubkey_t pk);
-void _cdk_trim_string(char *s);
cdk_error_t _cdk_sig_create(cdk_pkt_pubkey_t pk, cdk_pkt_signature_t sig);
cdk_error_t _cdk_sig_complete(cdk_pkt_signature_t sig, cdk_pkt_seckey_t sk,
digest_hd_st * hd);
diff --git a/lib/opencdk/misc.c b/lib/opencdk/misc.c
index c5c051530d..0d4ee89121 100644
--- a/lib/opencdk/misc.c
+++ b/lib/opencdk/misc.c
@@ -135,18 +135,6 @@ cdk_error_t _cdk_map_gnutls_error(int err)
}
-/* Remove all trailing white spaces from the string. */
-void _cdk_trim_string(char *s)
-{
- int len = strlen(s);
- while (s && *s &&
- (s[len - 1] == '\t' ||
- s[len - 1] == '\r' ||
- s[len - 1] == '\n' || s[len - 1] == ' '))
- s[len - 1] = '\0';
-}
-
-
int _cdk_check_args(int overwrite, const char *in, const char *out)
{
struct stat stbuf;
diff --git a/lib/opencdk/new-packet.c b/lib/opencdk/new-packet.c
index 7d61c2c415..1b35ef657f 100644
--- a/lib/opencdk/new-packet.c
+++ b/lib/opencdk/new-packet.c
@@ -269,7 +269,7 @@ cdk_error_t cdk_pkt_alloc(cdk_packet_t * r_pkt, cdk_packet_type_t pkttype)
switch (pkttype) {
case CDK_PKT_USER_ID:
- pkt->pkt.user_id = cdk_calloc(1, sizeof pkt->pkt.user_id);
+ pkt->pkt.user_id = cdk_calloc(1, sizeof *pkt->pkt.user_id);
if (!pkt->pkt.user_id)
return CDK_Out_Of_Core;
pkt->pkt.user_id->name = NULL;
@@ -287,10 +287,16 @@ cdk_error_t cdk_pkt_alloc(cdk_packet_t * r_pkt, cdk_packet_type_t pkttype)
case CDK_PKT_SECRET_SUBKEY:
pkt->pkt.secret_key =
cdk_calloc(1, sizeof *pkt->pkt.secret_key);
+ if (!pkt->pkt.secret_key)
+ return CDK_Out_Of_Core;
+
pkt->pkt.secret_key->pk =
cdk_calloc(1, sizeof *pkt->pkt.secret_key->pk);
- if (!pkt->pkt.secret_key || !pkt->pkt.secret_key->pk)
+ if (!pkt->pkt.secret_key->pk) {
+ cdk_free(pkt->pkt.secret_key);
+ pkt->pkt.secret_key = NULL;
return CDK_Out_Of_Core;
+ }
break;
case CDK_PKT_SIGNATURE:
diff --git a/lib/opencdk/stream.c b/lib/opencdk/stream.c
index 18da0f35c9..d272886b68 100644
--- a/lib/opencdk/stream.c
+++ b/lib/opencdk/stream.c
@@ -546,7 +546,7 @@ off_t cdk_stream_get_length(cdk_stream_t s)
if (!s) {
gnutls_assert();
- return (off_t) - 1;
+ return (off_t) 0;
}
/* The user callback does not support stat. */
@@ -557,13 +557,13 @@ off_t cdk_stream_get_length(cdk_stream_t s)
if (rc) {
s->error = rc;
gnutls_assert();
- return (off_t) - 1;
+ return (off_t) 0;
}
if (fstat(fileno(s->fp), &statbuf)) {
s->error = CDK_File_Error;
gnutls_assert();
- return (off_t) - 1;
+ return (off_t) 0;
}
return statbuf.st_size;
@@ -922,7 +922,7 @@ int cdk_stream_read(cdk_stream_t s, void *buf, size_t buflen)
s->flags.filtrated = 1;
}
- if (!buf && !buflen)
+ if (!buf || !buflen)
return 0;
nread = fread(buf, 1, buflen, s->fp);
@@ -1052,7 +1052,7 @@ int cdk_stream_putc(cdk_stream_t s, int c)
off_t cdk_stream_tell(cdk_stream_t s)
{
- return s ? ftell(s->fp) : (off_t) - 1;
+ return s ? ftell(s->fp) : (off_t) 0;
}