idx
int64
func_before
string
Vulnerability Classification
string
vul
int64
func_after
string
patch
string
CWE ID
string
lines_before
string
lines_after
string
14,400
kex_send_ext_info(struct ssh *ssh) { int r; char *algs; if ((algs = sshkey_alg_list(0, 1, ',')) == NULL) return SSH_ERR_ALLOC_FAIL; if ((r = sshpkt_start(ssh, SSH2_MSG_EXT_INFO)) != 0 || (r = sshpkt_put_u32(ssh, 1)) != 0 || (r = sshpkt_put_cstring(ssh, "server-sig-algs")) != 0 || (r = sshpkt_put_cstring(ssh, algs)) != 0 || (r = sshpkt_send(ssh)) != 0) goto out; /* success */ r = 0; out: free(algs); return 0; }
DoS
0
kex_send_ext_info(struct ssh *ssh) { int r; char *algs; if ((algs = sshkey_alg_list(0, 1, ',')) == NULL) return SSH_ERR_ALLOC_FAIL; if ((r = sshpkt_start(ssh, SSH2_MSG_EXT_INFO)) != 0 || (r = sshpkt_put_u32(ssh, 1)) != 0 || (r = sshpkt_put_cstring(ssh, "server-sig-algs")) != 0 || (r = sshpkt_put_cstring(ssh, algs)) != 0 || (r = sshpkt_send(ssh)) != 0) goto out; /* success */ r = 0; out: free(algs); return 0; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: kex.c,v 1.121 2016/09/12 23:31:27 djm Exp $ */ +/* $OpenBSD: kex.c,v 1.122 2016/09/19 19:02:19 markus Exp $ */ /* * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. * @@ -425,6 +425,8 @@ kex_input_newkeys(int type, u_int32_t seq, void *ctxt) ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error); if ((r = sshpkt_get_end(ssh)) != 0) return r; + if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0) + return r; kex->done = 1; sshbuf_reset(kex->peer); /* sshbuf_reset(kex->my); */
CWE-476
null
null
14,401
kex_send_kexinit(struct ssh *ssh) { u_char *cookie; struct kex *kex = ssh->kex; int r; if (kex == NULL) return SSH_ERR_INTERNAL_ERROR; if (kex->flags & KEX_INIT_SENT) return 0; kex->done = 0; /* generate a random cookie */ if (sshbuf_len(kex->my) < KEX_COOKIE_LEN) return SSH_ERR_INVALID_FORMAT; if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL) return SSH_ERR_INTERNAL_ERROR; arc4random_buf(cookie, KEX_COOKIE_LEN); if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 || (r = sshpkt_putb(ssh, kex->my)) != 0 || (r = sshpkt_send(ssh)) != 0) return r; debug("SSH2_MSG_KEXINIT sent"); kex->flags |= KEX_INIT_SENT; return 0; }
DoS
0
kex_send_kexinit(struct ssh *ssh) { u_char *cookie; struct kex *kex = ssh->kex; int r; if (kex == NULL) return SSH_ERR_INTERNAL_ERROR; if (kex->flags & KEX_INIT_SENT) return 0; kex->done = 0; /* generate a random cookie */ if (sshbuf_len(kex->my) < KEX_COOKIE_LEN) return SSH_ERR_INVALID_FORMAT; if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL) return SSH_ERR_INTERNAL_ERROR; arc4random_buf(cookie, KEX_COOKIE_LEN); if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 || (r = sshpkt_putb(ssh, kex->my)) != 0 || (r = sshpkt_send(ssh)) != 0) return r; debug("SSH2_MSG_KEXINIT sent"); kex->flags |= KEX_INIT_SENT; return 0; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: kex.c,v 1.121 2016/09/12 23:31:27 djm Exp $ */ +/* $OpenBSD: kex.c,v 1.122 2016/09/19 19:02:19 markus Exp $ */ /* * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. * @@ -425,6 +425,8 @@ kex_input_newkeys(int type, u_int32_t seq, void *ctxt) ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error); if ((r = sshpkt_get_end(ssh)) != 0) return r; + if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0) + return r; kex->done = 1; sshbuf_reset(kex->peer); /* sshbuf_reset(kex->my); */
CWE-476
null
null
14,402
kex_send_newkeys(struct ssh *ssh) { int r; kex_reset_dispatch(ssh); if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 || (r = sshpkt_send(ssh)) != 0) return r; debug("SSH2_MSG_NEWKEYS sent"); debug("expecting SSH2_MSG_NEWKEYS"); ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys); if (ssh->kex->ext_info_c) if ((r = kex_send_ext_info(ssh)) != 0) return r; return 0; }
DoS
0
kex_send_newkeys(struct ssh *ssh) { int r; kex_reset_dispatch(ssh); if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 || (r = sshpkt_send(ssh)) != 0) return r; debug("SSH2_MSG_NEWKEYS sent"); debug("expecting SSH2_MSG_NEWKEYS"); ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys); if (ssh->kex->ext_info_c) if ((r = kex_send_ext_info(ssh)) != 0) return r; return 0; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: kex.c,v 1.121 2016/09/12 23:31:27 djm Exp $ */ +/* $OpenBSD: kex.c,v 1.122 2016/09/19 19:02:19 markus Exp $ */ /* * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. * @@ -425,6 +425,8 @@ kex_input_newkeys(int type, u_int32_t seq, void *ctxt) ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error); if ((r = sshpkt_get_end(ssh)) != 0) return r; + if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0) + return r; kex->done = 1; sshbuf_reset(kex->peer); /* sshbuf_reset(kex->my); */
CWE-476
null
null
14,403
kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX]) { int r; if ((r = kex_new(ssh, proposal, &ssh->kex)) != 0) return r; if ((r = kex_send_kexinit(ssh)) != 0) { /* we start */ kex_free(ssh->kex); ssh->kex = NULL; return r; } return 0; }
DoS
0
kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX]) { int r; if ((r = kex_new(ssh, proposal, &ssh->kex)) != 0) return r; if ((r = kex_send_kexinit(ssh)) != 0) { /* we start */ kex_free(ssh->kex); ssh->kex = NULL; return r; } return 0; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: kex.c,v 1.121 2016/09/12 23:31:27 djm Exp $ */ +/* $OpenBSD: kex.c,v 1.122 2016/09/19 19:02:19 markus Exp $ */ /* * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. * @@ -425,6 +425,8 @@ kex_input_newkeys(int type, u_int32_t seq, void *ctxt) ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error); if ((r = sshpkt_get_end(ssh)) != 0) return r; + if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0) + return r; kex->done = 1; sshbuf_reset(kex->peer); /* sshbuf_reset(kex->my); */
CWE-476
null
null
14,404
proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX]) { static int check[] = { PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1 }; int *idx; char *p; for (idx = &check[0]; *idx != -1; idx++) { if ((p = strchr(my[*idx], ',')) != NULL) *p = '\0'; if ((p = strchr(peer[*idx], ',')) != NULL) *p = '\0'; if (strcmp(my[*idx], peer[*idx]) != 0) { debug2("proposal mismatch: my %s peer %s", my[*idx], peer[*idx]); return (0); } } debug2("proposals match"); return (1); }
DoS
0
proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX]) { static int check[] = { PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1 }; int *idx; char *p; for (idx = &check[0]; *idx != -1; idx++) { if ((p = strchr(my[*idx], ',')) != NULL) *p = '\0'; if ((p = strchr(peer[*idx], ',')) != NULL) *p = '\0'; if (strcmp(my[*idx], peer[*idx]) != 0) { debug2("proposal mismatch: my %s peer %s", my[*idx], peer[*idx]); return (0); } } debug2("proposals match"); return (1); }
@@ -1,4 +1,4 @@ -/* $OpenBSD: kex.c,v 1.121 2016/09/12 23:31:27 djm Exp $ */ +/* $OpenBSD: kex.c,v 1.122 2016/09/19 19:02:19 markus Exp $ */ /* * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. * @@ -425,6 +425,8 @@ kex_input_newkeys(int type, u_int32_t seq, void *ctxt) ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error); if ((r = sshpkt_get_end(ssh)) != 0) return r; + if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0) + return r; kex->done = 1; sshbuf_reset(kex->peer); /* sshbuf_reset(kex->my); */
CWE-476
null
null
14,405
kex_from_blob(struct sshbuf *m, struct kex **kexp) { struct kex *kex; int r; if ((kex = calloc(1, sizeof(struct kex))) == NULL || (kex->my = sshbuf_new()) == NULL || (kex->peer = sshbuf_new()) == NULL) { r = SSH_ERR_ALLOC_FAIL; goto out; } if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 || (r = sshbuf_get_u32(m, &kex->we_need)) != 0 || (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 || (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 || (r = sshbuf_get_stringb(m, kex->my)) != 0 || (r = sshbuf_get_stringb(m, kex->peer)) != 0 || (r = sshbuf_get_u32(m, &kex->flags)) != 0 || (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 || (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0) goto out; kex->server = 1; kex->done = 1; r = 0; out: if (r != 0 || kexp == NULL) { if (kex != NULL) { sshbuf_free(kex->my); sshbuf_free(kex->peer); free(kex); } if (kexp != NULL) *kexp = NULL; } else { *kexp = kex; } return r; }
DoS
0
kex_from_blob(struct sshbuf *m, struct kex **kexp) { struct kex *kex; int r; if ((kex = calloc(1, sizeof(struct kex))) == NULL || (kex->my = sshbuf_new()) == NULL || (kex->peer = sshbuf_new()) == NULL) { r = SSH_ERR_ALLOC_FAIL; goto out; } if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 || (r = sshbuf_get_u32(m, &kex->we_need)) != 0 || (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 || (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 || (r = sshbuf_get_stringb(m, kex->my)) != 0 || (r = sshbuf_get_stringb(m, kex->peer)) != 0 || (r = sshbuf_get_u32(m, &kex->flags)) != 0 || (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 || (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0) goto out; kex->server = 1; kex->done = 1; r = 0; out: if (r != 0 || kexp == NULL) { if (kex != NULL) { sshbuf_free(kex->my); sshbuf_free(kex->peer); free(kex); } if (kexp != NULL) *kexp = NULL; } else { *kexp = kex; } return r; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,406
newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode) { struct sshbuf *b = NULL; struct sshcomp *comp; struct sshenc *enc; struct sshmac *mac; struct newkeys *newkey = NULL; size_t keylen, ivlen, maclen; int r; if ((newkey = calloc(1, sizeof(*newkey))) == NULL) { r = SSH_ERR_ALLOC_FAIL; goto out; } if ((r = sshbuf_froms(m, &b)) != 0) goto out; #ifdef DEBUG_PK sshbuf_dump(b, stderr); #endif enc = &newkey->enc; mac = &newkey->mac; comp = &newkey->comp; if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 || (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 || (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 || (r = sshbuf_get_u32(b, &enc->block_size)) != 0 || (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 || (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0) goto out; if (cipher_authlen(enc->cipher) == 0) { if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0) goto out; if ((r = mac_setup(mac, mac->name)) != 0) goto out; if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 || (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0) goto out; if (maclen > mac->key_len) { r = SSH_ERR_INVALID_FORMAT; goto out; } mac->key_len = maclen; } if ((r = sshbuf_get_u32(b, &comp->type)) != 0 || (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 || (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0) goto out; if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher) { r = SSH_ERR_INVALID_FORMAT; goto out; } if (sshbuf_len(b) != 0) { r = SSH_ERR_INVALID_FORMAT; goto out; } enc->key_len = keylen; enc->iv_len = ivlen; ssh->kex->newkeys[mode] = newkey; newkey = NULL; r = 0; out: free(newkey); sshbuf_free(b); return r; }
DoS
0
newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode) { struct sshbuf *b = NULL; struct sshcomp *comp; struct sshenc *enc; struct sshmac *mac; struct newkeys *newkey = NULL; size_t keylen, ivlen, maclen; int r; if ((newkey = calloc(1, sizeof(*newkey))) == NULL) { r = SSH_ERR_ALLOC_FAIL; goto out; } if ((r = sshbuf_froms(m, &b)) != 0) goto out; #ifdef DEBUG_PK sshbuf_dump(b, stderr); #endif enc = &newkey->enc; mac = &newkey->mac; comp = &newkey->comp; if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 || (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 || (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 || (r = sshbuf_get_u32(b, &enc->block_size)) != 0 || (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 || (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0) goto out; if (cipher_authlen(enc->cipher) == 0) { if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0) goto out; if ((r = mac_setup(mac, mac->name)) != 0) goto out; if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 || (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0) goto out; if (maclen > mac->key_len) { r = SSH_ERR_INVALID_FORMAT; goto out; } mac->key_len = maclen; } if ((r = sshbuf_get_u32(b, &comp->type)) != 0 || (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 || (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0) goto out; if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher) { r = SSH_ERR_INVALID_FORMAT; goto out; } if (sshbuf_len(b) != 0) { r = SSH_ERR_INVALID_FORMAT; goto out; } enc->key_len = keylen; enc->iv_len = ivlen; ssh->kex->newkeys[mode] = newkey; newkey = NULL; r = 0; out: free(newkey); sshbuf_free(b); return r; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,407
ssh_local_ipaddr(struct ssh *ssh) { (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */ return ssh->local_ipaddr; }
DoS
0
ssh_local_ipaddr(struct ssh *ssh) { (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */ return ssh->local_ipaddr; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,408
ssh_local_port(struct ssh *ssh) { (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */ return ssh->local_port; }
DoS
0
ssh_local_port(struct ssh *ssh) { (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */ return ssh->local_port; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,409
ssh_packet_close(struct ssh *ssh) { struct session_state *state = ssh->state; u_int mode; if (!state->initialized) return; state->initialized = 0; if (state->connection_in == state->connection_out) { shutdown(state->connection_out, SHUT_RDWR); close(state->connection_out); } else { close(state->connection_in); close(state->connection_out); } sshbuf_free(state->input); sshbuf_free(state->output); sshbuf_free(state->outgoing_packet); sshbuf_free(state->incoming_packet); for (mode = 0; mode < MODE_MAX; mode++) kex_free_newkeys(state->newkeys[mode]); if (state->compression_buffer) { sshbuf_free(state->compression_buffer); if (state->compression_out_started) { z_streamp stream = &state->compression_out_stream; debug("compress outgoing: " "raw data %llu, compressed %llu, factor %.2f", (unsigned long long)stream->total_in, (unsigned long long)stream->total_out, stream->total_in == 0 ? 0.0 : (double) stream->total_out / stream->total_in); if (state->compression_out_failures == 0) deflateEnd(stream); } if (state->compression_in_started) { z_streamp stream = &state->compression_out_stream; debug("compress incoming: " "raw data %llu, compressed %llu, factor %.2f", (unsigned long long)stream->total_out, (unsigned long long)stream->total_in, stream->total_out == 0 ? 0.0 : (double) stream->total_in / stream->total_out); if (state->compression_in_failures == 0) inflateEnd(stream); } } cipher_free(state->send_context); cipher_free(state->receive_context); state->send_context = state->receive_context = NULL; free(ssh->remote_ipaddr); ssh->remote_ipaddr = NULL; free(ssh->state); ssh->state = NULL; }
DoS
0
ssh_packet_close(struct ssh *ssh) { struct session_state *state = ssh->state; u_int mode; if (!state->initialized) return; state->initialized = 0; if (state->connection_in == state->connection_out) { shutdown(state->connection_out, SHUT_RDWR); close(state->connection_out); } else { close(state->connection_in); close(state->connection_out); } sshbuf_free(state->input); sshbuf_free(state->output); sshbuf_free(state->outgoing_packet); sshbuf_free(state->incoming_packet); for (mode = 0; mode < MODE_MAX; mode++) kex_free_newkeys(state->newkeys[mode]); if (state->compression_buffer) { sshbuf_free(state->compression_buffer); if (state->compression_out_started) { z_streamp stream = &state->compression_out_stream; debug("compress outgoing: " "raw data %llu, compressed %llu, factor %.2f", (unsigned long long)stream->total_in, (unsigned long long)stream->total_out, stream->total_in == 0 ? 0.0 : (double) stream->total_out / stream->total_in); if (state->compression_out_failures == 0) deflateEnd(stream); } if (state->compression_in_started) { z_streamp stream = &state->compression_out_stream; debug("compress incoming: " "raw data %llu, compressed %llu, factor %.2f", (unsigned long long)stream->total_out, (unsigned long long)stream->total_in, stream->total_out == 0 ? 0.0 : (double) stream->total_in / stream->total_out); if (state->compression_in_failures == 0) inflateEnd(stream); } } cipher_free(state->send_context); cipher_free(state->receive_context); state->send_context = state->receive_context = NULL; free(ssh->remote_ipaddr); ssh->remote_ipaddr = NULL; free(ssh->state); ssh->state = NULL; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,410
ssh_packet_connection_is_on_socket(struct ssh *ssh) { struct session_state *state = ssh->state; struct sockaddr_storage from, to; socklen_t fromlen, tolen; if (state->connection_in == -1 || state->connection_out == -1) return 0; /* filedescriptors in and out are the same, so it's a socket */ if (state->connection_in == state->connection_out) return 1; fromlen = sizeof(from); memset(&from, 0, sizeof(from)); if (getpeername(state->connection_in, (struct sockaddr *)&from, &fromlen) < 0) return 0; tolen = sizeof(to); memset(&to, 0, sizeof(to)); if (getpeername(state->connection_out, (struct sockaddr *)&to, &tolen) < 0) return 0; if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0) return 0; if (from.ss_family != AF_INET && from.ss_family != AF_INET6) return 0; return 1; }
DoS
0
ssh_packet_connection_is_on_socket(struct ssh *ssh) { struct session_state *state = ssh->state; struct sockaddr_storage from, to; socklen_t fromlen, tolen; if (state->connection_in == -1 || state->connection_out == -1) return 0; /* filedescriptors in and out are the same, so it's a socket */ if (state->connection_in == state->connection_out) return 1; fromlen = sizeof(from); memset(&from, 0, sizeof(from)); if (getpeername(state->connection_in, (struct sockaddr *)&from, &fromlen) < 0) return 0; tolen = sizeof(to); memset(&to, 0, sizeof(to)); if (getpeername(state->connection_out, (struct sockaddr *)&to, &tolen) < 0) return 0; if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0) return 0; if (from.ss_family != AF_INET && from.ss_family != AF_INET6) return 0; return 1; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,411
ssh_packet_is_rekeying(struct ssh *ssh) { return compat20 && (ssh->state->rekeying || (ssh->kex != NULL && ssh->kex->done == 0)); }
DoS
0
ssh_packet_is_rekeying(struct ssh *ssh) { return compat20 && (ssh->state->rekeying || (ssh->kex != NULL && ssh->kex->done == 0)); }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,412
ssh_packet_need_rekeying(struct ssh *ssh, u_int outbound_packet_len) { struct session_state *state = ssh->state; u_int32_t out_blocks; /* XXX client can't cope with rekeying pre-auth */ if (!state->after_authentication) return 0; /* Haven't keyed yet or KEX in progress. */ if (ssh->kex == NULL || ssh_packet_is_rekeying(ssh)) return 0; /* Peer can't rekey */ if (ssh->compat & SSH_BUG_NOREKEY) return 0; /* * Permit one packet in or out per rekey - this allows us to * make progress when rekey limits are very small. */ if (state->p_send.packets == 0 && state->p_read.packets == 0) return 0; /* Time-based rekeying */ if (state->rekey_interval != 0 && state->rekey_time + state->rekey_interval <= monotime()) return 1; /* Always rekey when MAX_PACKETS sent in either direction */ if (state->p_send.packets > MAX_PACKETS || state->p_read.packets > MAX_PACKETS) return 1; /* Rekey after (cipher-specific) maxiumum blocks */ out_blocks = ROUNDUP(outbound_packet_len, state->newkeys[MODE_OUT]->enc.block_size); return (state->max_blocks_out && (state->p_send.blocks + out_blocks > state->max_blocks_out)) || (state->max_blocks_in && (state->p_read.blocks > state->max_blocks_in)); }
DoS
0
ssh_packet_need_rekeying(struct ssh *ssh, u_int outbound_packet_len) { struct session_state *state = ssh->state; u_int32_t out_blocks; /* XXX client can't cope with rekeying pre-auth */ if (!state->after_authentication) return 0; /* Haven't keyed yet or KEX in progress. */ if (ssh->kex == NULL || ssh_packet_is_rekeying(ssh)) return 0; /* Peer can't rekey */ if (ssh->compat & SSH_BUG_NOREKEY) return 0; /* * Permit one packet in or out per rekey - this allows us to * make progress when rekey limits are very small. */ if (state->p_send.packets == 0 && state->p_read.packets == 0) return 0; /* Time-based rekeying */ if (state->rekey_interval != 0 && state->rekey_time + state->rekey_interval <= monotime()) return 1; /* Always rekey when MAX_PACKETS sent in either direction */ if (state->p_send.packets > MAX_PACKETS || state->p_read.packets > MAX_PACKETS) return 1; /* Rekey after (cipher-specific) maxiumum blocks */ out_blocks = ROUNDUP(outbound_packet_len, state->newkeys[MODE_OUT]->enc.block_size); return (state->max_blocks_out && (state->p_send.blocks + out_blocks > state->max_blocks_out)) || (state->max_blocks_in && (state->p_read.blocks > state->max_blocks_in)); }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,413
ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) { struct session_state *state = ssh->state; int len, r, ms_remain; fd_set *setp; char buf[8192]; struct timeval timeout, start, *timeoutp = NULL; DBG(debug("packet_read()")); setp = calloc(howmany(state->connection_in + 1, NFDBITS), sizeof(fd_mask)); if (setp == NULL) return SSH_ERR_ALLOC_FAIL; /* * Since we are blocking, ensure that all written packets have * been sent. */ if ((r = ssh_packet_write_wait(ssh)) != 0) goto out; /* Stay in the loop until we have received a complete packet. */ for (;;) { /* Try to read a packet from the buffer. */ r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p); if (r != 0) break; if (!compat20 && ( *typep == SSH_SMSG_SUCCESS || *typep == SSH_SMSG_FAILURE || *typep == SSH_CMSG_EOF || *typep == SSH_CMSG_EXIT_CONFIRMATION)) if ((r = sshpkt_get_end(ssh)) != 0) break; /* If we got a packet, return it. */ if (*typep != SSH_MSG_NONE) break; /* * Otherwise, wait for some data to arrive, add it to the * buffer, and try again. */ memset(setp, 0, howmany(state->connection_in + 1, NFDBITS) * sizeof(fd_mask)); FD_SET(state->connection_in, setp); if (state->packet_timeout_ms > 0) { ms_remain = state->packet_timeout_ms; timeoutp = &timeout; } /* Wait for some data to arrive. */ for (;;) { if (state->packet_timeout_ms != -1) { ms_to_timeval(&timeout, ms_remain); gettimeofday(&start, NULL); } if ((r = select(state->connection_in + 1, setp, NULL, NULL, timeoutp)) >= 0) break; if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) break; if (state->packet_timeout_ms == -1) continue; ms_subtract_diff(&start, &ms_remain); if (ms_remain <= 0) { r = 0; break; } } if (r == 0) return SSH_ERR_CONN_TIMEOUT; /* Read data from the socket. */ len = read(state->connection_in, buf, sizeof(buf)); if (len == 0) { r = SSH_ERR_CONN_CLOSED; goto out; } if (len < 0) { r = SSH_ERR_SYSTEM_ERROR; goto out; } /* Append it to the buffer. */ if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0) goto out; } out: free(setp); return r; }
DoS
0
ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) { struct session_state *state = ssh->state; int len, r, ms_remain; fd_set *setp; char buf[8192]; struct timeval timeout, start, *timeoutp = NULL; DBG(debug("packet_read()")); setp = calloc(howmany(state->connection_in + 1, NFDBITS), sizeof(fd_mask)); if (setp == NULL) return SSH_ERR_ALLOC_FAIL; /* * Since we are blocking, ensure that all written packets have * been sent. */ if ((r = ssh_packet_write_wait(ssh)) != 0) goto out; /* Stay in the loop until we have received a complete packet. */ for (;;) { /* Try to read a packet from the buffer. */ r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p); if (r != 0) break; if (!compat20 && ( *typep == SSH_SMSG_SUCCESS || *typep == SSH_SMSG_FAILURE || *typep == SSH_CMSG_EOF || *typep == SSH_CMSG_EXIT_CONFIRMATION)) if ((r = sshpkt_get_end(ssh)) != 0) break; /* If we got a packet, return it. */ if (*typep != SSH_MSG_NONE) break; /* * Otherwise, wait for some data to arrive, add it to the * buffer, and try again. */ memset(setp, 0, howmany(state->connection_in + 1, NFDBITS) * sizeof(fd_mask)); FD_SET(state->connection_in, setp); if (state->packet_timeout_ms > 0) { ms_remain = state->packet_timeout_ms; timeoutp = &timeout; } /* Wait for some data to arrive. */ for (;;) { if (state->packet_timeout_ms != -1) { ms_to_timeval(&timeout, ms_remain); gettimeofday(&start, NULL); } if ((r = select(state->connection_in + 1, setp, NULL, NULL, timeoutp)) >= 0) break; if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) break; if (state->packet_timeout_ms == -1) continue; ms_subtract_diff(&start, &ms_remain); if (ms_remain <= 0) { r = 0; break; } } if (r == 0) return SSH_ERR_CONN_TIMEOUT; /* Read data from the socket. */ len = read(state->connection_in, buf, sizeof(buf)); if (len == 0) { r = SSH_ERR_CONN_CLOSED; goto out; } if (len < 0) { r = SSH_ERR_SYSTEM_ERROR; goto out; } /* Append it to the buffer. */ if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0) goto out; } out: free(setp); return r; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,414
ssh_packet_send1(struct ssh *ssh) { struct session_state *state = ssh->state; u_char buf[8], *cp; int r, padding, len; u_int checksum; /* * If using packet compression, compress the payload of the outgoing * packet. */ if (state->packet_compression) { sshbuf_reset(state->compression_buffer); /* Skip padding. */ if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0) goto out; /* padding */ if ((r = sshbuf_put(state->compression_buffer, "\0\0\0\0\0\0\0\0", 8)) != 0) goto out; if ((r = compress_buffer(ssh, state->outgoing_packet, state->compression_buffer)) != 0) goto out; sshbuf_reset(state->outgoing_packet); if ((r = sshbuf_putb(state->outgoing_packet, state->compression_buffer)) != 0) goto out; } /* Compute packet length without padding (add checksum, remove padding). */ len = sshbuf_len(state->outgoing_packet) + 4 - 8; /* Insert padding. Initialized to zero in packet_start1() */ padding = 8 - len % 8; if (!cipher_ctx_is_plaintext(state->send_context)) { cp = sshbuf_mutable_ptr(state->outgoing_packet); if (cp == NULL) { r = SSH_ERR_INTERNAL_ERROR; goto out; } arc4random_buf(cp + 8 - padding, padding); } if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0) goto out; /* Add check bytes. */ checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet), sshbuf_len(state->outgoing_packet)); POKE_U32(buf, checksum); if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0) goto out; #ifdef PACKET_DEBUG fprintf(stderr, "packet_send plain: "); sshbuf_dump(state->outgoing_packet, stderr); #endif /* Append to output. */ POKE_U32(buf, len); if ((r = sshbuf_put(state->output, buf, 4)) != 0) goto out; if ((r = sshbuf_reserve(state->output, sshbuf_len(state->outgoing_packet), &cp)) != 0) goto out; if ((r = cipher_crypt(state->send_context, 0, cp, sshbuf_ptr(state->outgoing_packet), sshbuf_len(state->outgoing_packet), 0, 0)) != 0) goto out; #ifdef PACKET_DEBUG fprintf(stderr, "encrypted: "); sshbuf_dump(state->output, stderr); #endif state->p_send.packets++; state->p_send.bytes += len + sshbuf_len(state->outgoing_packet); sshbuf_reset(state->outgoing_packet); /* * Note that the packet is now only buffered in output. It won't be * actually sent until ssh_packet_write_wait or ssh_packet_write_poll * is called. */ r = 0; out: return r; }
DoS
0
ssh_packet_send1(struct ssh *ssh) { struct session_state *state = ssh->state; u_char buf[8], *cp; int r, padding, len; u_int checksum; /* * If using packet compression, compress the payload of the outgoing * packet. */ if (state->packet_compression) { sshbuf_reset(state->compression_buffer); /* Skip padding. */ if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0) goto out; /* padding */ if ((r = sshbuf_put(state->compression_buffer, "\0\0\0\0\0\0\0\0", 8)) != 0) goto out; if ((r = compress_buffer(ssh, state->outgoing_packet, state->compression_buffer)) != 0) goto out; sshbuf_reset(state->outgoing_packet); if ((r = sshbuf_putb(state->outgoing_packet, state->compression_buffer)) != 0) goto out; } /* Compute packet length without padding (add checksum, remove padding). */ len = sshbuf_len(state->outgoing_packet) + 4 - 8; /* Insert padding. Initialized to zero in packet_start1() */ padding = 8 - len % 8; if (!cipher_ctx_is_plaintext(state->send_context)) { cp = sshbuf_mutable_ptr(state->outgoing_packet); if (cp == NULL) { r = SSH_ERR_INTERNAL_ERROR; goto out; } arc4random_buf(cp + 8 - padding, padding); } if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0) goto out; /* Add check bytes. */ checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet), sshbuf_len(state->outgoing_packet)); POKE_U32(buf, checksum); if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0) goto out; #ifdef PACKET_DEBUG fprintf(stderr, "packet_send plain: "); sshbuf_dump(state->outgoing_packet, stderr); #endif /* Append to output. */ POKE_U32(buf, len); if ((r = sshbuf_put(state->output, buf, 4)) != 0) goto out; if ((r = sshbuf_reserve(state->output, sshbuf_len(state->outgoing_packet), &cp)) != 0) goto out; if ((r = cipher_crypt(state->send_context, 0, cp, sshbuf_ptr(state->outgoing_packet), sshbuf_len(state->outgoing_packet), 0, 0)) != 0) goto out; #ifdef PACKET_DEBUG fprintf(stderr, "encrypted: "); sshbuf_dump(state->output, stderr); #endif state->p_send.packets++; state->p_send.bytes += len + sshbuf_len(state->outgoing_packet); sshbuf_reset(state->outgoing_packet); /* * Note that the packet is now only buffered in output. It won't be * actually sent until ssh_packet_write_wait or ssh_packet_write_poll * is called. */ r = 0; out: return r; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,415
ssh_packet_send2_wrapped(struct ssh *ssh) { struct session_state *state = ssh->state; u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH]; u_char tmp, padlen, pad = 0; u_int authlen = 0, aadlen = 0; u_int len; struct sshenc *enc = NULL; struct sshmac *mac = NULL; struct sshcomp *comp = NULL; int r, block_size; if (state->newkeys[MODE_OUT] != NULL) { enc = &state->newkeys[MODE_OUT]->enc; mac = &state->newkeys[MODE_OUT]->mac; comp = &state->newkeys[MODE_OUT]->comp; /* disable mac for authenticated encryption */ if ((authlen = cipher_authlen(enc->cipher)) != 0) mac = NULL; } block_size = enc ? enc->block_size : 8; aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0; type = (sshbuf_ptr(state->outgoing_packet))[5]; if (ssh_packet_log_type(type)) debug3("send packet: type %u", type); #ifdef PACKET_DEBUG fprintf(stderr, "plain: "); sshbuf_dump(state->outgoing_packet, stderr); #endif if (comp && comp->enabled) { len = sshbuf_len(state->outgoing_packet); /* skip header, compress only payload */ if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0) goto out; sshbuf_reset(state->compression_buffer); if ((r = compress_buffer(ssh, state->outgoing_packet, state->compression_buffer)) != 0) goto out; sshbuf_reset(state->outgoing_packet); if ((r = sshbuf_put(state->outgoing_packet, "\0\0\0\0\0", 5)) != 0 || (r = sshbuf_putb(state->outgoing_packet, state->compression_buffer)) != 0) goto out; DBG(debug("compression: raw %d compressed %zd", len, sshbuf_len(state->outgoing_packet))); } /* sizeof (packet_len + pad_len + payload) */ len = sshbuf_len(state->outgoing_packet); /* * calc size of padding, alloc space, get random data, * minimum padding is 4 bytes */ len -= aadlen; /* packet length is not encrypted for EtM modes */ padlen = block_size - (len % block_size); if (padlen < 4) padlen += block_size; if (state->extra_pad) { tmp = state->extra_pad; state->extra_pad = ROUNDUP(state->extra_pad, block_size); /* check if roundup overflowed */ if (state->extra_pad < tmp) return SSH_ERR_INVALID_ARGUMENT; tmp = (len + padlen) % state->extra_pad; /* Check whether pad calculation below will underflow */ if (tmp > state->extra_pad) return SSH_ERR_INVALID_ARGUMENT; pad = state->extra_pad - tmp; DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)", __func__, pad, len, padlen, state->extra_pad)); tmp = padlen; padlen += pad; /* Check whether padlen calculation overflowed */ if (padlen < tmp) return SSH_ERR_INVALID_ARGUMENT; /* overflow */ state->extra_pad = 0; } if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0) goto out; if (enc && !cipher_ctx_is_plaintext(state->send_context)) { /* random padding */ arc4random_buf(cp, padlen); } else { /* clear padding */ explicit_bzero(cp, padlen); } /* sizeof (packet_len + pad_len + payload + padding) */ len = sshbuf_len(state->outgoing_packet); cp = sshbuf_mutable_ptr(state->outgoing_packet); if (cp == NULL) { r = SSH_ERR_INTERNAL_ERROR; goto out; } /* packet_length includes payload, padding and padding length field */ POKE_U32(cp, len - 4); cp[4] = padlen; DBG(debug("send: len %d (includes padlen %d, aadlen %d)", len, padlen, aadlen)); /* compute MAC over seqnr and packet(length fields, payload, padding) */ if (mac && mac->enabled && !mac->etm) { if ((r = mac_compute(mac, state->p_send.seqnr, sshbuf_ptr(state->outgoing_packet), len, macbuf, sizeof(macbuf))) != 0) goto out; DBG(debug("done calc MAC out #%d", state->p_send.seqnr)); } /* encrypt packet and append to output buffer. */ if ((r = sshbuf_reserve(state->output, sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0) goto out; if ((r = cipher_crypt(state->send_context, state->p_send.seqnr, cp, sshbuf_ptr(state->outgoing_packet), len - aadlen, aadlen, authlen)) != 0) goto out; /* append unencrypted MAC */ if (mac && mac->enabled) { if (mac->etm) { /* EtM: compute mac over aadlen + cipher text */ if ((r = mac_compute(mac, state->p_send.seqnr, cp, len, macbuf, sizeof(macbuf))) != 0) goto out; DBG(debug("done calc MAC(EtM) out #%d", state->p_send.seqnr)); } if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0) goto out; } #ifdef PACKET_DEBUG fprintf(stderr, "encrypted: "); sshbuf_dump(state->output, stderr); #endif /* increment sequence number for outgoing packets */ if (++state->p_send.seqnr == 0) logit("outgoing seqnr wraps around"); if (++state->p_send.packets == 0) if (!(ssh->compat & SSH_BUG_NOREKEY)) return SSH_ERR_NEED_REKEY; state->p_send.blocks += len / block_size; state->p_send.bytes += len; sshbuf_reset(state->outgoing_packet); if (type == SSH2_MSG_NEWKEYS) r = ssh_set_newkeys(ssh, MODE_OUT); else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0; out: return r; }
DoS
0
ssh_packet_send2_wrapped(struct ssh *ssh) { struct session_state *state = ssh->state; u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH]; u_char tmp, padlen, pad = 0; u_int authlen = 0, aadlen = 0; u_int len; struct sshenc *enc = NULL; struct sshmac *mac = NULL; struct sshcomp *comp = NULL; int r, block_size; if (state->newkeys[MODE_OUT] != NULL) { enc = &state->newkeys[MODE_OUT]->enc; mac = &state->newkeys[MODE_OUT]->mac; comp = &state->newkeys[MODE_OUT]->comp; /* disable mac for authenticated encryption */ if ((authlen = cipher_authlen(enc->cipher)) != 0) mac = NULL; } block_size = enc ? enc->block_size : 8; aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0; type = (sshbuf_ptr(state->outgoing_packet))[5]; if (ssh_packet_log_type(type)) debug3("send packet: type %u", type); #ifdef PACKET_DEBUG fprintf(stderr, "plain: "); sshbuf_dump(state->outgoing_packet, stderr); #endif if (comp && comp->enabled) { len = sshbuf_len(state->outgoing_packet); /* skip header, compress only payload */ if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0) goto out; sshbuf_reset(state->compression_buffer); if ((r = compress_buffer(ssh, state->outgoing_packet, state->compression_buffer)) != 0) goto out; sshbuf_reset(state->outgoing_packet); if ((r = sshbuf_put(state->outgoing_packet, "\0\0\0\0\0", 5)) != 0 || (r = sshbuf_putb(state->outgoing_packet, state->compression_buffer)) != 0) goto out; DBG(debug("compression: raw %d compressed %zd", len, sshbuf_len(state->outgoing_packet))); } /* sizeof (packet_len + pad_len + payload) */ len = sshbuf_len(state->outgoing_packet); /* * calc size of padding, alloc space, get random data, * minimum padding is 4 bytes */ len -= aadlen; /* packet length is not encrypted for EtM modes */ padlen = block_size - (len % block_size); if (padlen < 4) padlen += block_size; if (state->extra_pad) { tmp = state->extra_pad; state->extra_pad = ROUNDUP(state->extra_pad, block_size); /* check if roundup overflowed */ if (state->extra_pad < tmp) return SSH_ERR_INVALID_ARGUMENT; tmp = (len + padlen) % state->extra_pad; /* Check whether pad calculation below will underflow */ if (tmp > state->extra_pad) return SSH_ERR_INVALID_ARGUMENT; pad = state->extra_pad - tmp; DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)", __func__, pad, len, padlen, state->extra_pad)); tmp = padlen; padlen += pad; /* Check whether padlen calculation overflowed */ if (padlen < tmp) return SSH_ERR_INVALID_ARGUMENT; /* overflow */ state->extra_pad = 0; } if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0) goto out; if (enc && !cipher_ctx_is_plaintext(state->send_context)) { /* random padding */ arc4random_buf(cp, padlen); } else { /* clear padding */ explicit_bzero(cp, padlen); } /* sizeof (packet_len + pad_len + payload + padding) */ len = sshbuf_len(state->outgoing_packet); cp = sshbuf_mutable_ptr(state->outgoing_packet); if (cp == NULL) { r = SSH_ERR_INTERNAL_ERROR; goto out; } /* packet_length includes payload, padding and padding length field */ POKE_U32(cp, len - 4); cp[4] = padlen; DBG(debug("send: len %d (includes padlen %d, aadlen %d)", len, padlen, aadlen)); /* compute MAC over seqnr and packet(length fields, payload, padding) */ if (mac && mac->enabled && !mac->etm) { if ((r = mac_compute(mac, state->p_send.seqnr, sshbuf_ptr(state->outgoing_packet), len, macbuf, sizeof(macbuf))) != 0) goto out; DBG(debug("done calc MAC out #%d", state->p_send.seqnr)); } /* encrypt packet and append to output buffer. */ if ((r = sshbuf_reserve(state->output, sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0) goto out; if ((r = cipher_crypt(state->send_context, state->p_send.seqnr, cp, sshbuf_ptr(state->outgoing_packet), len - aadlen, aadlen, authlen)) != 0) goto out; /* append unencrypted MAC */ if (mac && mac->enabled) { if (mac->etm) { /* EtM: compute mac over aadlen + cipher text */ if ((r = mac_compute(mac, state->p_send.seqnr, cp, len, macbuf, sizeof(macbuf))) != 0) goto out; DBG(debug("done calc MAC(EtM) out #%d", state->p_send.seqnr)); } if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0) goto out; } #ifdef PACKET_DEBUG fprintf(stderr, "encrypted: "); sshbuf_dump(state->output, stderr); #endif /* increment sequence number for outgoing packets */ if (++state->p_send.seqnr == 0) logit("outgoing seqnr wraps around"); if (++state->p_send.packets == 0) if (!(ssh->compat & SSH_BUG_NOREKEY)) return SSH_ERR_NEED_REKEY; state->p_send.blocks += len / block_size; state->p_send.bytes += len; sshbuf_reset(state->outgoing_packet); if (type == SSH2_MSG_NEWKEYS) r = ssh_set_newkeys(ssh, MODE_OUT); else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0; out: return r; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,416
ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out) { struct session_state *state; const struct sshcipher *none = cipher_by_name("none"); int r; if (none == NULL) { error("%s: cannot load cipher 'none'", __func__); return NULL; } if (ssh == NULL) ssh = ssh_alloc_session_state(); if (ssh == NULL) { error("%s: cound not allocate state", __func__); return NULL; } state = ssh->state; state->connection_in = fd_in; state->connection_out = fd_out; if ((r = cipher_init(&state->send_context, none, (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 || (r = cipher_init(&state->receive_context, none, (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) { error("%s: cipher_init failed: %s", __func__, ssh_err(r)); free(ssh); /* XXX need ssh_free_session_state? */ return NULL; } state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL; deattack_init(&state->deattack); /* * Cache the IP address of the remote connection for use in error * messages that might be generated after the connection has closed. */ (void)ssh_remote_ipaddr(ssh); return ssh; }
DoS
0
ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out) { struct session_state *state; const struct sshcipher *none = cipher_by_name("none"); int r; if (none == NULL) { error("%s: cannot load cipher 'none'", __func__); return NULL; } if (ssh == NULL) ssh = ssh_alloc_session_state(); if (ssh == NULL) { error("%s: cound not allocate state", __func__); return NULL; } state = ssh->state; state->connection_in = fd_in; state->connection_out = fd_out; if ((r = cipher_init(&state->send_context, none, (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 || (r = cipher_init(&state->receive_context, none, (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) { error("%s: cipher_init failed: %s", __func__, ssh_err(r)); free(ssh); /* XXX need ssh_free_session_state? */ return NULL; } state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL; deattack_init(&state->deattack); /* * Cache the IP address of the remote connection for use in error * messages that might be generated after the connection has closed. */ (void)ssh_remote_ipaddr(ssh); return ssh; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,417
ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number) { #ifndef WITH_SSH1 fatal("no SSH protocol 1 support"); #else /* WITH_SSH1 */ struct session_state *state = ssh->state; const struct sshcipher *cipher = cipher_by_number(number); int r; const char *wmsg; if (cipher == NULL) fatal("%s: unknown cipher number %d", __func__, number); if (keylen < 20) fatal("%s: keylen too small: %d", __func__, keylen); if (keylen > SSH_SESSION_KEY_LENGTH) fatal("%s: keylen too big: %d", __func__, keylen); memcpy(state->ssh1_key, key, keylen); state->ssh1_keylen = keylen; if ((r = cipher_init(&state->send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT)) != 0 || (r = cipher_init(&state->receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT) != 0)) fatal("%s: cipher_init failed: %s", __func__, ssh_err(r)); if (!state->cipher_warning_done && ((wmsg = cipher_warning_message(state->send_context)) != NULL || (wmsg = cipher_warning_message(state->send_context)) != NULL)) { error("Warning: %s", wmsg); state->cipher_warning_done = 1; } #endif /* WITH_SSH1 */ }
DoS
0
ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number) { #ifndef WITH_SSH1 fatal("no SSH protocol 1 support"); #else /* WITH_SSH1 */ struct session_state *state = ssh->state; const struct sshcipher *cipher = cipher_by_number(number); int r; const char *wmsg; if (cipher == NULL) fatal("%s: unknown cipher number %d", __func__, number); if (keylen < 20) fatal("%s: keylen too small: %d", __func__, keylen); if (keylen > SSH_SESSION_KEY_LENGTH) fatal("%s: keylen too big: %d", __func__, keylen); memcpy(state->ssh1_key, key, keylen); state->ssh1_keylen = keylen; if ((r = cipher_init(&state->send_context, cipher, key, keylen, NULL, 0, CIPHER_ENCRYPT)) != 0 || (r = cipher_init(&state->receive_context, cipher, key, keylen, NULL, 0, CIPHER_DECRYPT) != 0)) fatal("%s: cipher_init failed: %s", __func__, ssh_err(r)); if (!state->cipher_warning_done && ((wmsg = cipher_warning_message(state->send_context)) != NULL || (wmsg = cipher_warning_message(state->send_context)) != NULL)) { error("Warning: %s", wmsg); state->cipher_warning_done = 1; } #endif /* WITH_SSH1 */ }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,418
ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds) { debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes, (int)seconds); ssh->state->rekey_limit = bytes; ssh->state->rekey_interval = seconds; }
DoS
0
ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds) { debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes, (int)seconds); ssh->state->rekey_limit = bytes; ssh->state->rekey_interval = seconds; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,419
ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m) { struct session_state *state = ssh->state; const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output; size_t ssh1keylen, rlen, slen, ilen, olen; int r; u_int ssh1cipher = 0; if (!compat20) { if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 || (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 || (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 || (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 || (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0) return r; if (ssh1cipher > INT_MAX) return SSH_ERR_KEY_UNKNOWN_CIPHER; ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen, (int)ssh1cipher); if (cipher_get_keyiv_len(state->send_context) != (int)slen || cipher_get_keyiv_len(state->receive_context) != (int)rlen) return SSH_ERR_INVALID_FORMAT; if ((r = cipher_set_keyiv(state->send_context, ivout)) != 0 || (r = cipher_set_keyiv(state->receive_context, ivin)) != 0) return r; } else { if ((r = kex_from_blob(m, &ssh->kex)) != 0 || (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 || (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 || (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 || (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 || (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 || (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 || (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 || (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 || (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 || (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 || (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 || (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0) return r; /* * We set the time here so that in post-auth privsep slave we * count from the completion of the authentication. */ state->rekey_time = monotime(); /* XXX ssh_set_newkeys overrides p_read.packets? XXX */ if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 || (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0) return r; } if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 || (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0) return r; if (cipher_get_keycontext(state->send_context, NULL) != (int)slen || cipher_get_keycontext(state->receive_context, NULL) != (int)rlen) return SSH_ERR_INVALID_FORMAT; cipher_set_keycontext(state->send_context, keyout); cipher_set_keycontext(state->receive_context, keyin); if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 || (r = ssh_packet_set_postauth(ssh)) != 0) return r; sshbuf_reset(state->input); sshbuf_reset(state->output); if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 || (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 || (r = sshbuf_put(state->input, input, ilen)) != 0 || (r = sshbuf_put(state->output, output, olen)) != 0) return r; if (sshbuf_len(m)) return SSH_ERR_INVALID_FORMAT; debug3("%s: done", __func__); return 0; }
DoS
0
ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m) { struct session_state *state = ssh->state; const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output; size_t ssh1keylen, rlen, slen, ilen, olen; int r; u_int ssh1cipher = 0; if (!compat20) { if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 || (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 || (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 || (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 || (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0) return r; if (ssh1cipher > INT_MAX) return SSH_ERR_KEY_UNKNOWN_CIPHER; ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen, (int)ssh1cipher); if (cipher_get_keyiv_len(state->send_context) != (int)slen || cipher_get_keyiv_len(state->receive_context) != (int)rlen) return SSH_ERR_INVALID_FORMAT; if ((r = cipher_set_keyiv(state->send_context, ivout)) != 0 || (r = cipher_set_keyiv(state->receive_context, ivin)) != 0) return r; } else { if ((r = kex_from_blob(m, &ssh->kex)) != 0 || (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 || (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 || (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 || (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 || (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 || (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 || (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 || (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 || (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 || (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 || (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 || (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0) return r; /* * We set the time here so that in post-auth privsep slave we * count from the completion of the authentication. */ state->rekey_time = monotime(); /* XXX ssh_set_newkeys overrides p_read.packets? XXX */ if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 || (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0) return r; } if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 || (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0) return r; if (cipher_get_keycontext(state->send_context, NULL) != (int)slen || cipher_get_keycontext(state->receive_context, NULL) != (int)rlen) return SSH_ERR_INVALID_FORMAT; cipher_set_keycontext(state->send_context, keyout); cipher_set_keycontext(state->receive_context, keyin); if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 || (r = ssh_packet_set_postauth(ssh)) != 0) return r; sshbuf_reset(state->input); sshbuf_reset(state->output); if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 || (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 || (r = sshbuf_put(state->input, input, ilen)) != 0 || (r = sshbuf_put(state->output, output, olen)) != 0) return r; if (sshbuf_len(m)) return SSH_ERR_INVALID_FORMAT; debug3("%s: done", __func__); return 0; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,420
ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc, struct sshmac *mac, size_t mac_already, u_int discard) { struct session_state *state = ssh->state; int r; if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) { if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0) return r; return SSH_ERR_MAC_INVALID; } /* * Record number of bytes over which the mac has already * been computed in order to minimize timing attacks. */ if (mac && mac->enabled) { state->packet_discard_mac = mac; state->packet_discard_mac_already = mac_already; } if (sshbuf_len(state->input) >= discard) return ssh_packet_stop_discard(ssh); state->packet_discard = discard - sshbuf_len(state->input); return 0; }
DoS
0
ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc, struct sshmac *mac, size_t mac_already, u_int discard) { struct session_state *state = ssh->state; int r; if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) { if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0) return r; return SSH_ERR_MAC_INVALID; } /* * Record number of bytes over which the mac has already * been computed in order to minimize timing attacks. */ if (mac && mac->enabled) { state->packet_discard_mac = mac; state->packet_discard_mac_already = mac_already; } if (sshbuf_len(state->input) >= discard) return ssh_packet_stop_discard(ssh); state->packet_discard = discard - sshbuf_len(state->input); return 0; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,421
ssh_packet_stop_discard(struct ssh *ssh) { struct session_state *state = ssh->state; int r; if (state->packet_discard_mac) { char buf[1024]; size_t dlen = PACKET_MAX_SIZE; if (dlen > state->packet_discard_mac_already) dlen -= state->packet_discard_mac_already; memset(buf, 'a', sizeof(buf)); while (sshbuf_len(state->incoming_packet) < dlen) if ((r = sshbuf_put(state->incoming_packet, buf, sizeof(buf))) != 0) return r; (void) mac_compute(state->packet_discard_mac, state->p_read.seqnr, sshbuf_ptr(state->incoming_packet), dlen, NULL, 0); } logit("Finished discarding for %.200s port %d", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); return SSH_ERR_MAC_INVALID; }
DoS
0
ssh_packet_stop_discard(struct ssh *ssh) { struct session_state *state = ssh->state; int r; if (state->packet_discard_mac) { char buf[1024]; size_t dlen = PACKET_MAX_SIZE; if (dlen > state->packet_discard_mac_already) dlen -= state->packet_discard_mac_already; memset(buf, 'a', sizeof(buf)); while (sshbuf_len(state->incoming_packet) < dlen) if ((r = sshbuf_put(state->incoming_packet, buf, sizeof(buf))) != 0) return r; (void) mac_compute(state->packet_discard_mac, state->p_read.seqnr, sshbuf_ptr(state->incoming_packet), dlen, NULL, 0); } logit("Finished discarding for %.200s port %d", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); return SSH_ERR_MAC_INVALID; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,422
ssh_packet_type_is_kex(u_char type) { return type >= SSH2_MSG_TRANSPORT_MIN && type <= SSH2_MSG_TRANSPORT_MAX && type != SSH2_MSG_SERVICE_REQUEST && type != SSH2_MSG_SERVICE_ACCEPT && type != SSH2_MSG_EXT_INFO; }
DoS
0
ssh_packet_type_is_kex(u_char type) { return type >= SSH2_MSG_TRANSPORT_MIN && type <= SSH2_MSG_TRANSPORT_MAX && type != SSH2_MSG_SERVICE_REQUEST && type != SSH2_MSG_SERVICE_ACCEPT && type != SSH2_MSG_EXT_INFO; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,423
ssh_packet_write_poll(struct ssh *ssh) { struct session_state *state = ssh->state; int len = sshbuf_len(state->output); int r; if (len > 0) { len = write(state->connection_out, sshbuf_ptr(state->output), len); if (len == -1) { if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) return 0; return SSH_ERR_SYSTEM_ERROR; } if (len == 0) return SSH_ERR_CONN_CLOSED; if ((r = sshbuf_consume(state->output, len)) != 0) return r; } return 0; }
DoS
0
ssh_packet_write_poll(struct ssh *ssh) { struct session_state *state = ssh->state; int len = sshbuf_len(state->output); int r; if (len > 0) { len = write(state->connection_out, sshbuf_ptr(state->output), len); if (len == -1) { if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) return 0; return SSH_ERR_SYSTEM_ERROR; } if (len == 0) return SSH_ERR_CONN_CLOSED; if ((r = sshbuf_consume(state->output, len)) != 0) return r; } return 0; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,424
ssh_remote_port(struct ssh *ssh) { (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */ return ssh->remote_port; }
DoS
0
ssh_remote_port(struct ssh *ssh) { (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */ return ssh->remote_port; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,425
ssh_set_newkeys(struct ssh *ssh, int mode) { struct session_state *state = ssh->state; struct sshenc *enc; struct sshmac *mac; struct sshcomp *comp; struct sshcipher_ctx **ccp; struct packet_state *ps; u_int64_t *max_blocks; const char *wmsg, *dir; int r, crypt_type; debug2("set_newkeys: mode %d", mode); if (mode == MODE_OUT) { dir = "output"; ccp = &state->send_context; crypt_type = CIPHER_ENCRYPT; ps = &state->p_send; max_blocks = &state->max_blocks_out; } else { dir = "input"; ccp = &state->receive_context; crypt_type = CIPHER_DECRYPT; ps = &state->p_read; max_blocks = &state->max_blocks_in; } if (state->newkeys[mode] != NULL) { debug("%s: rekeying after %llu %s blocks" " (%llu bytes total)", __func__, (unsigned long long)ps->blocks, dir, (unsigned long long)ps->bytes); cipher_free(*ccp); *ccp = NULL; enc = &state->newkeys[mode]->enc; mac = &state->newkeys[mode]->mac; comp = &state->newkeys[mode]->comp; mac_clear(mac); explicit_bzero(enc->iv, enc->iv_len); explicit_bzero(enc->key, enc->key_len); explicit_bzero(mac->key, mac->key_len); free(enc->name); free(enc->iv); free(enc->key); free(mac->name); free(mac->key); free(comp->name); free(state->newkeys[mode]); } /* note that both bytes and the seqnr are not reset */ ps->packets = ps->blocks = 0; /* move newkeys from kex to state */ if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL) return SSH_ERR_INTERNAL_ERROR; ssh->kex->newkeys[mode] = NULL; enc = &state->newkeys[mode]->enc; mac = &state->newkeys[mode]->mac; comp = &state->newkeys[mode]->comp; if (cipher_authlen(enc->cipher) == 0) { if ((r = mac_init(mac)) != 0) return r; } mac->enabled = 1; DBG(debug("cipher_init_context: %d", mode)); if ((r = cipher_init(ccp, enc->cipher, enc->key, enc->key_len, enc->iv, enc->iv_len, crypt_type)) != 0) return r; if (!state->cipher_warning_done && (wmsg = cipher_warning_message(*ccp)) != NULL) { error("Warning: %s", wmsg); state->cipher_warning_done = 1; } /* Deleting the keys does not gain extra security */ /* explicit_bzero(enc->iv, enc->block_size); explicit_bzero(enc->key, enc->key_len); explicit_bzero(mac->key, mac->key_len); */ if ((comp->type == COMP_ZLIB || (comp->type == COMP_DELAYED && state->after_authentication)) && comp->enabled == 0) { if ((r = ssh_packet_init_compression(ssh)) < 0) return r; if (mode == MODE_OUT) { if ((r = start_compression_out(ssh, 6)) != 0) return r; } else { if ((r = start_compression_in(ssh)) != 0) return r; } comp->enabled = 1; } /* * The 2^(blocksize*2) limit is too expensive for 3DES, * blowfish, etc, so enforce a 1GB limit for small blocksizes. */ if (enc->block_size >= 16) *max_blocks = (u_int64_t)1 << (enc->block_size*2); else *max_blocks = ((u_int64_t)1 << 30) / enc->block_size; if (state->rekey_limit) *max_blocks = MINIMUM(*max_blocks, state->rekey_limit / enc->block_size); debug("rekey after %llu blocks", (unsigned long long)*max_blocks); return 0; }
DoS
0
ssh_set_newkeys(struct ssh *ssh, int mode) { struct session_state *state = ssh->state; struct sshenc *enc; struct sshmac *mac; struct sshcomp *comp; struct sshcipher_ctx **ccp; struct packet_state *ps; u_int64_t *max_blocks; const char *wmsg, *dir; int r, crypt_type; debug2("set_newkeys: mode %d", mode); if (mode == MODE_OUT) { dir = "output"; ccp = &state->send_context; crypt_type = CIPHER_ENCRYPT; ps = &state->p_send; max_blocks = &state->max_blocks_out; } else { dir = "input"; ccp = &state->receive_context; crypt_type = CIPHER_DECRYPT; ps = &state->p_read; max_blocks = &state->max_blocks_in; } if (state->newkeys[mode] != NULL) { debug("%s: rekeying after %llu %s blocks" " (%llu bytes total)", __func__, (unsigned long long)ps->blocks, dir, (unsigned long long)ps->bytes); cipher_free(*ccp); *ccp = NULL; enc = &state->newkeys[mode]->enc; mac = &state->newkeys[mode]->mac; comp = &state->newkeys[mode]->comp; mac_clear(mac); explicit_bzero(enc->iv, enc->iv_len); explicit_bzero(enc->key, enc->key_len); explicit_bzero(mac->key, mac->key_len); free(enc->name); free(enc->iv); free(enc->key); free(mac->name); free(mac->key); free(comp->name); free(state->newkeys[mode]); } /* note that both bytes and the seqnr are not reset */ ps->packets = ps->blocks = 0; /* move newkeys from kex to state */ if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL) return SSH_ERR_INTERNAL_ERROR; ssh->kex->newkeys[mode] = NULL; enc = &state->newkeys[mode]->enc; mac = &state->newkeys[mode]->mac; comp = &state->newkeys[mode]->comp; if (cipher_authlen(enc->cipher) == 0) { if ((r = mac_init(mac)) != 0) return r; } mac->enabled = 1; DBG(debug("cipher_init_context: %d", mode)); if ((r = cipher_init(ccp, enc->cipher, enc->key, enc->key_len, enc->iv, enc->iv_len, crypt_type)) != 0) return r; if (!state->cipher_warning_done && (wmsg = cipher_warning_message(*ccp)) != NULL) { error("Warning: %s", wmsg); state->cipher_warning_done = 1; } /* Deleting the keys does not gain extra security */ /* explicit_bzero(enc->iv, enc->block_size); explicit_bzero(enc->key, enc->key_len); explicit_bzero(mac->key, mac->key_len); */ if ((comp->type == COMP_ZLIB || (comp->type == COMP_DELAYED && state->after_authentication)) && comp->enabled == 0) { if ((r = ssh_packet_init_compression(ssh)) < 0) return r; if (mode == MODE_OUT) { if ((r = start_compression_out(ssh, 6)) != 0) return r; } else { if ((r = start_compression_in(ssh)) != 0) return r; } comp->enabled = 1; } /* * The 2^(blocksize*2) limit is too expensive for 3DES, * blowfish, etc, so enforce a 1GB limit for small blocksizes. */ if (enc->block_size >= 16) *max_blocks = (u_int64_t)1 << (enc->block_size*2); else *max_blocks = ((u_int64_t)1 << 30) / enc->block_size; if (state->rekey_limit) *max_blocks = MINIMUM(*max_blocks, state->rekey_limit / enc->block_size); debug("rekey after %llu blocks", (unsigned long long)*max_blocks); return 0; }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,426
sshpkt_fatal(struct ssh *ssh, const char *tag, int r) { switch (r) { case SSH_ERR_CONN_CLOSED: logdie("Connection closed by %.200s port %d", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); case SSH_ERR_CONN_TIMEOUT: logdie("Connection %s %.200s port %d timed out", ssh->state->server_side ? "from" : "to", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); case SSH_ERR_DISCONNECTED: logdie("Disconnected from %.200s port %d", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); case SSH_ERR_SYSTEM_ERROR: if (errno == ECONNRESET) logdie("Connection reset by %.200s port %d", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); /* FALLTHROUGH */ case SSH_ERR_NO_CIPHER_ALG_MATCH: case SSH_ERR_NO_MAC_ALG_MATCH: case SSH_ERR_NO_COMPRESS_ALG_MATCH: case SSH_ERR_NO_KEX_ALG_MATCH: case SSH_ERR_NO_HOSTKEY_ALG_MATCH: if (ssh && ssh->kex && ssh->kex->failed_choice) { logdie("Unable to negotiate with %.200s port %d: %s. " "Their offer: %s", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r), ssh->kex->failed_choice); } /* FALLTHROUGH */ default: logdie("%s%sConnection %s %.200s port %d: %s", tag != NULL ? tag : "", tag != NULL ? ": " : "", ssh->state->server_side ? "from" : "to", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r)); } }
DoS
0
sshpkt_fatal(struct ssh *ssh, const char *tag, int r) { switch (r) { case SSH_ERR_CONN_CLOSED: logdie("Connection closed by %.200s port %d", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); case SSH_ERR_CONN_TIMEOUT: logdie("Connection %s %.200s port %d timed out", ssh->state->server_side ? "from" : "to", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); case SSH_ERR_DISCONNECTED: logdie("Disconnected from %.200s port %d", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); case SSH_ERR_SYSTEM_ERROR: if (errno == ECONNRESET) logdie("Connection reset by %.200s port %d", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); /* FALLTHROUGH */ case SSH_ERR_NO_CIPHER_ALG_MATCH: case SSH_ERR_NO_MAC_ALG_MATCH: case SSH_ERR_NO_COMPRESS_ALG_MATCH: case SSH_ERR_NO_KEX_ALG_MATCH: case SSH_ERR_NO_HOSTKEY_ALG_MATCH: if (ssh && ssh->kex && ssh->kex->failed_choice) { logdie("Unable to negotiate with %.200s port %d: %s. " "Their offer: %s", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r), ssh->kex->failed_choice); } /* FALLTHROUGH */ default: logdie("%s%sConnection %s %.200s port %d: %s", tag != NULL ? tag : "", tag != NULL ? ": " : "", ssh->state->server_side ? "from" : "to", ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r)); } }
@@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.237 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: packet.c,v 1.238 2016/09/19 19:02:19 markus Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1907,9 +1907,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p) return r; return SSH_ERR_PROTOCOL_ERROR; } - if (*typep == SSH2_MSG_NEWKEYS) - r = ssh_set_newkeys(ssh, MODE_IN); - else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) + if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side) r = ssh_packet_enable_delayed_compress(ssh); else r = 0;
CWE-476
null
null
14,427
jbig2_alloc(Jbig2Allocator *allocator, size_t size, size_t num) { /* check for integer multiplication overflow */ if (num > 0 && size >= (size_t) - 0x100 / num) return NULL; return allocator->alloc(allocator, size * num); }
Overflow
0
jbig2_alloc(Jbig2Allocator *allocator, size_t size, size_t num) { /* check for integer multiplication overflow */ if (num > 0 && size >= (size_t) - 0x100 / num) return NULL; return allocator->alloc(allocator, size * num); }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,428
jbig2_ctx_free(Jbig2Ctx *ctx) { Jbig2Allocator *ca = ctx->allocator; int i; jbig2_free(ca, ctx->buf); if (ctx->segments != NULL) { for (i = 0; i < ctx->n_segments; i++) jbig2_free_segment(ctx, ctx->segments[i]); jbig2_free(ca, ctx->segments); } if (ctx->pages != NULL) { for (i = 0; i <= ctx->current_page; i++) if (ctx->pages[i].image != NULL) jbig2_image_release(ctx, ctx->pages[i].image); jbig2_free(ca, ctx->pages); } jbig2_free(ca, ctx); }
Overflow
0
jbig2_ctx_free(Jbig2Ctx *ctx) { Jbig2Allocator *ca = ctx->allocator; int i; jbig2_free(ca, ctx->buf); if (ctx->segments != NULL) { for (i = 0; i < ctx->n_segments; i++) jbig2_free_segment(ctx, ctx->segments[i]); jbig2_free(ca, ctx->segments); } if (ctx->pages != NULL) { for (i = 0; i <= ctx->current_page; i++) if (ctx->pages[i].image != NULL) jbig2_image_release(ctx, ctx->pages[i].image); jbig2_free(ca, ctx->pages); } jbig2_free(ca, ctx); }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,429
jbig2_ctx_new(Jbig2Allocator *allocator, Jbig2Options options, Jbig2GlobalCtx *global_ctx, Jbig2ErrorCallback error_callback, void *error_callback_data) { Jbig2Ctx *result; if (allocator == NULL) allocator = &jbig2_default_allocator; if (error_callback == NULL) error_callback = &jbig2_default_error; result = (Jbig2Ctx *) jbig2_alloc(allocator, sizeof(Jbig2Ctx), 1); if (result == NULL) { error_callback(error_callback_data, "initial context allocation failed!", JBIG2_SEVERITY_FATAL, -1); return result; } result->allocator = allocator; result->options = options; result->global_ctx = (const Jbig2Ctx *)global_ctx; result->error_callback = error_callback; result->error_callback_data = error_callback_data; result->state = (options & JBIG2_OPTIONS_EMBEDDED) ? JBIG2_FILE_SEQUENTIAL_HEADER : JBIG2_FILE_HEADER; result->buf = NULL; result->n_segments = 0; result->n_segments_max = 16; result->segments = jbig2_new(result, Jbig2Segment *, result->n_segments_max); if (result->segments == NULL) { error_callback(error_callback_data, "initial segments allocation failed!", JBIG2_SEVERITY_FATAL, -1); jbig2_free(allocator, result); return result; } result->segment_index = 0; result->current_page = 0; result->max_page_index = 4; result->pages = jbig2_new(result, Jbig2Page, result->max_page_index); if (result->pages == NULL) { error_callback(error_callback_data, "initial pages allocation failed!", JBIG2_SEVERITY_FATAL, -1); jbig2_free(allocator, result->segments); jbig2_free(allocator, result); return result; } { int index; for (index = 0; index < result->max_page_index; index++) { result->pages[index].state = JBIG2_PAGE_FREE; result->pages[index].number = 0; result->pages[index].image = NULL; } } return result; }
Overflow
0
jbig2_ctx_new(Jbig2Allocator *allocator, Jbig2Options options, Jbig2GlobalCtx *global_ctx, Jbig2ErrorCallback error_callback, void *error_callback_data) { Jbig2Ctx *result; if (allocator == NULL) allocator = &jbig2_default_allocator; if (error_callback == NULL) error_callback = &jbig2_default_error; result = (Jbig2Ctx *) jbig2_alloc(allocator, sizeof(Jbig2Ctx), 1); if (result == NULL) { error_callback(error_callback_data, "initial context allocation failed!", JBIG2_SEVERITY_FATAL, -1); return result; } result->allocator = allocator; result->options = options; result->global_ctx = (const Jbig2Ctx *)global_ctx; result->error_callback = error_callback; result->error_callback_data = error_callback_data; result->state = (options & JBIG2_OPTIONS_EMBEDDED) ? JBIG2_FILE_SEQUENTIAL_HEADER : JBIG2_FILE_HEADER; result->buf = NULL; result->n_segments = 0; result->n_segments_max = 16; result->segments = jbig2_new(result, Jbig2Segment *, result->n_segments_max); if (result->segments == NULL) { error_callback(error_callback_data, "initial segments allocation failed!", JBIG2_SEVERITY_FATAL, -1); jbig2_free(allocator, result); return result; } result->segment_index = 0; result->current_page = 0; result->max_page_index = 4; result->pages = jbig2_new(result, Jbig2Page, result->max_page_index); if (result->pages == NULL) { error_callback(error_callback_data, "initial pages allocation failed!", JBIG2_SEVERITY_FATAL, -1); jbig2_free(allocator, result->segments); jbig2_free(allocator, result); return result; } { int index; for (index = 0; index < result->max_page_index; index++) { result->pages[index].state = JBIG2_PAGE_FREE; result->pages[index].number = 0; result->pages[index].image = NULL; } } return result; }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,430
jbig2_data_in(Jbig2Ctx *ctx, const unsigned char *data, size_t size) { const size_t initial_buf_size = 1024; if (ctx->buf == NULL) { size_t buf_size = initial_buf_size; do buf_size <<= 1; while (buf_size < size); ctx->buf = jbig2_new(ctx, byte, buf_size); if (ctx->buf == NULL) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate ctx->buf in jbig2_data_in"); } ctx->buf_size = buf_size; ctx->buf_rd_ix = 0; ctx->buf_wr_ix = 0; } else if (ctx->buf_wr_ix + size > ctx->buf_size) { if (ctx->buf_rd_ix <= (ctx->buf_size >> 1) && ctx->buf_wr_ix - ctx->buf_rd_ix + size <= ctx->buf_size) { memmove(ctx->buf, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix); } else { byte *buf; size_t buf_size = initial_buf_size; do buf_size <<= 1; while (buf_size < ctx->buf_wr_ix - ctx->buf_rd_ix + size); buf = jbig2_new(ctx, byte, buf_size); if (buf == NULL) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate buf in jbig2_data_in"); } memcpy(buf, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix); jbig2_free(ctx->allocator, ctx->buf); ctx->buf = buf; ctx->buf_size = buf_size; } ctx->buf_wr_ix -= ctx->buf_rd_ix; ctx->buf_rd_ix = 0; } memcpy(ctx->buf + ctx->buf_wr_ix, data, size); ctx->buf_wr_ix += size; /* data has now been added to buffer */ for (;;) { const byte jbig2_id_string[8] = { 0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a }; Jbig2Segment *segment; size_t header_size; int code; switch (ctx->state) { case JBIG2_FILE_HEADER: /* D.4.1 */ if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9) return 0; if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8)) return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "Not a JBIG2 file header"); /* D.4.2 */ ctx->file_header_flags = ctx->buf[ctx->buf_rd_ix + 8]; if (ctx->file_header_flags & 0xFC) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "reserved bits (2-7) of file header flags are not zero (0x%02x)", ctx->file_header_flags); } /* D.4.3 */ if (!(ctx->file_header_flags & 2)) { /* number of pages is known */ if (ctx->buf_wr_ix - ctx->buf_rd_ix < 13) return 0; ctx->n_pages = jbig2_get_uint32(ctx->buf + ctx->buf_rd_ix + 9); ctx->buf_rd_ix += 13; if (ctx->n_pages == 1) jbig2_error(ctx, JBIG2_SEVERITY_INFO, -1, "file header indicates a single page document"); else jbig2_error(ctx, JBIG2_SEVERITY_INFO, -1, "file header indicates a %d page document", ctx->n_pages); } else { /* number of pages not known */ ctx->n_pages = 0; ctx->buf_rd_ix += 9; } /* determine the file organization based on the flags - D.4.2 again */ if (ctx->file_header_flags & 1) { ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER; jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "file header indicates sequential organization"); } else { ctx->state = JBIG2_FILE_RANDOM_HEADERS; jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "file header indicates random-access organization"); } break; case JBIG2_FILE_SEQUENTIAL_HEADER: case JBIG2_FILE_RANDOM_HEADERS: segment = jbig2_parse_segment_header(ctx, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix, &header_size); if (segment == NULL) return 0; /* need more data */ ctx->buf_rd_ix += header_size; if (ctx->n_segments == ctx->n_segments_max) ctx->segments = jbig2_renew(ctx, ctx->segments, Jbig2Segment *, (ctx->n_segments_max <<= 2)); ctx->segments[ctx->n_segments++] = segment; if (ctx->state == JBIG2_FILE_RANDOM_HEADERS) { if ((segment->flags & 63) == 51) /* end of file */ ctx->state = JBIG2_FILE_RANDOM_BODIES; } else /* JBIG2_FILE_SEQUENTIAL_HEADER */ ctx->state = JBIG2_FILE_SEQUENTIAL_BODY; break; case JBIG2_FILE_SEQUENTIAL_BODY: case JBIG2_FILE_RANDOM_BODIES: segment = ctx->segments[ctx->segment_index]; if (segment->data_length > ctx->buf_wr_ix - ctx->buf_rd_ix) return 0; /* need more data */ code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix); ctx->buf_rd_ix += segment->data_length; ctx->segment_index++; if (ctx->state == JBIG2_FILE_RANDOM_BODIES) { if (ctx->segment_index == ctx->n_segments) ctx->state = JBIG2_FILE_EOF; } else { /* JBIG2_FILE_SEQUENCIAL_BODY */ ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER; } if (code < 0) { ctx->state = JBIG2_FILE_EOF; return code; } break; case JBIG2_FILE_EOF: if (ctx->buf_rd_ix == ctx->buf_wr_ix) return 0; return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "Garbage beyond end of file"); } } }
Overflow
0
jbig2_data_in(Jbig2Ctx *ctx, const unsigned char *data, size_t size) { const size_t initial_buf_size = 1024; if (ctx->buf == NULL) { size_t buf_size = initial_buf_size; do buf_size <<= 1; while (buf_size < size); ctx->buf = jbig2_new(ctx, byte, buf_size); if (ctx->buf == NULL) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate ctx->buf in jbig2_data_in"); } ctx->buf_size = buf_size; ctx->buf_rd_ix = 0; ctx->buf_wr_ix = 0; } else if (ctx->buf_wr_ix + size > ctx->buf_size) { if (ctx->buf_rd_ix <= (ctx->buf_size >> 1) && ctx->buf_wr_ix - ctx->buf_rd_ix + size <= ctx->buf_size) { memmove(ctx->buf, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix); } else { byte *buf; size_t buf_size = initial_buf_size; do buf_size <<= 1; while (buf_size < ctx->buf_wr_ix - ctx->buf_rd_ix + size); buf = jbig2_new(ctx, byte, buf_size); if (buf == NULL) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate buf in jbig2_data_in"); } memcpy(buf, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix); jbig2_free(ctx->allocator, ctx->buf); ctx->buf = buf; ctx->buf_size = buf_size; } ctx->buf_wr_ix -= ctx->buf_rd_ix; ctx->buf_rd_ix = 0; } memcpy(ctx->buf + ctx->buf_wr_ix, data, size); ctx->buf_wr_ix += size; /* data has now been added to buffer */ for (;;) { const byte jbig2_id_string[8] = { 0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a }; Jbig2Segment *segment; size_t header_size; int code; switch (ctx->state) { case JBIG2_FILE_HEADER: /* D.4.1 */ if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9) return 0; if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8)) return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "Not a JBIG2 file header"); /* D.4.2 */ ctx->file_header_flags = ctx->buf[ctx->buf_rd_ix + 8]; if (ctx->file_header_flags & 0xFC) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "reserved bits (2-7) of file header flags are not zero (0x%02x)", ctx->file_header_flags); } /* D.4.3 */ if (!(ctx->file_header_flags & 2)) { /* number of pages is known */ if (ctx->buf_wr_ix - ctx->buf_rd_ix < 13) return 0; ctx->n_pages = jbig2_get_uint32(ctx->buf + ctx->buf_rd_ix + 9); ctx->buf_rd_ix += 13; if (ctx->n_pages == 1) jbig2_error(ctx, JBIG2_SEVERITY_INFO, -1, "file header indicates a single page document"); else jbig2_error(ctx, JBIG2_SEVERITY_INFO, -1, "file header indicates a %d page document", ctx->n_pages); } else { /* number of pages not known */ ctx->n_pages = 0; ctx->buf_rd_ix += 9; } /* determine the file organization based on the flags - D.4.2 again */ if (ctx->file_header_flags & 1) { ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER; jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "file header indicates sequential organization"); } else { ctx->state = JBIG2_FILE_RANDOM_HEADERS; jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "file header indicates random-access organization"); } break; case JBIG2_FILE_SEQUENTIAL_HEADER: case JBIG2_FILE_RANDOM_HEADERS: segment = jbig2_parse_segment_header(ctx, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix, &header_size); if (segment == NULL) return 0; /* need more data */ ctx->buf_rd_ix += header_size; if (ctx->n_segments == ctx->n_segments_max) ctx->segments = jbig2_renew(ctx, ctx->segments, Jbig2Segment *, (ctx->n_segments_max <<= 2)); ctx->segments[ctx->n_segments++] = segment; if (ctx->state == JBIG2_FILE_RANDOM_HEADERS) { if ((segment->flags & 63) == 51) /* end of file */ ctx->state = JBIG2_FILE_RANDOM_BODIES; } else /* JBIG2_FILE_SEQUENTIAL_HEADER */ ctx->state = JBIG2_FILE_SEQUENTIAL_BODY; break; case JBIG2_FILE_SEQUENTIAL_BODY: case JBIG2_FILE_RANDOM_BODIES: segment = ctx->segments[ctx->segment_index]; if (segment->data_length > ctx->buf_wr_ix - ctx->buf_rd_ix) return 0; /* need more data */ code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix); ctx->buf_rd_ix += segment->data_length; ctx->segment_index++; if (ctx->state == JBIG2_FILE_RANDOM_BODIES) { if (ctx->segment_index == ctx->n_segments) ctx->state = JBIG2_FILE_EOF; } else { /* JBIG2_FILE_SEQUENCIAL_BODY */ ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER; } if (code < 0) { ctx->state = JBIG2_FILE_EOF; return code; } break; case JBIG2_FILE_EOF: if (ctx->buf_rd_ix == ctx->buf_wr_ix) return 0; return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "Garbage beyond end of file"); } } }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,431
jbig2_default_alloc(Jbig2Allocator *allocator, size_t size) { return malloc(size); }
Overflow
0
jbig2_default_alloc(Jbig2Allocator *allocator, size_t size) { return malloc(size); }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,432
jbig2_default_error(void *data, const char *msg, Jbig2Severity severity, int32_t seg_idx) { /* report only fatal errors by default */ if (severity == JBIG2_SEVERITY_FATAL) { fprintf(stderr, "jbig2 decoder FATAL ERROR: %s", msg); if (seg_idx != -1) fprintf(stderr, " (segment 0x%02x)", seg_idx); fprintf(stderr, "\n"); fflush(stderr); } return 0; }
Overflow
0
jbig2_default_error(void *data, const char *msg, Jbig2Severity severity, int32_t seg_idx) { /* report only fatal errors by default */ if (severity == JBIG2_SEVERITY_FATAL) { fprintf(stderr, "jbig2 decoder FATAL ERROR: %s", msg); if (seg_idx != -1) fprintf(stderr, " (segment 0x%02x)", seg_idx); fprintf(stderr, "\n"); fflush(stderr); } return 0; }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,433
jbig2_default_free(Jbig2Allocator *allocator, void *p) { free(p); }
Overflow
0
jbig2_default_free(Jbig2Allocator *allocator, void *p) { free(p); }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,434
jbig2_default_realloc(Jbig2Allocator *allocator, void *p, size_t size) { return realloc(p, size); }
Overflow
0
jbig2_default_realloc(Jbig2Allocator *allocator, void *p, size_t size) { return realloc(p, size); }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,435
jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, int32_t segment_number, const char *fmt, ...) { char buf[1024]; va_list ap; int n; int code; va_start(ap, fmt); n = vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); if (n < 0 || n == sizeof(buf)) strncpy(buf, "jbig2_error: error in generating error string", sizeof(buf)); code = ctx->error_callback(ctx->error_callback_data, buf, severity, segment_number); if (severity == JBIG2_SEVERITY_FATAL) code = -1; return code; }
Overflow
0
jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, int32_t segment_number, const char *fmt, ...) { char buf[1024]; va_list ap; int n; int code; va_start(ap, fmt); n = vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); if (n < 0 || n == sizeof(buf)) strncpy(buf, "jbig2_error: error in generating error string", sizeof(buf)); code = ctx->error_callback(ctx->error_callback_data, buf, severity, segment_number); if (severity == JBIG2_SEVERITY_FATAL) code = -1; return code; }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,436
jbig2_free(Jbig2Allocator *allocator, void *p) { allocator->free(allocator, p); }
Overflow
0
jbig2_free(Jbig2Allocator *allocator, void *p) { allocator->free(allocator, p); }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,437
jbig2_get_int16(const byte *bptr) { return get_int16(bptr); }
Overflow
0
jbig2_get_int16(const byte *bptr) { return get_int16(bptr); }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,438
jbig2_get_int32(const byte *bptr) { return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2); }
Overflow
0
jbig2_get_int32(const byte *bptr) { return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2); }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,439
jbig2_get_uint32(const byte *bptr) { return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2); }
Overflow
0
jbig2_get_uint32(const byte *bptr) { return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2); }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,440
jbig2_global_ctx_free(Jbig2GlobalCtx *global_ctx) { jbig2_ctx_free((Jbig2Ctx *) global_ctx); }
Overflow
0
jbig2_global_ctx_free(Jbig2GlobalCtx *global_ctx) { jbig2_ctx_free((Jbig2Ctx *) global_ctx); }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,441
jbig2_make_global_ctx(Jbig2Ctx *ctx) { return (Jbig2GlobalCtx *) ctx; }
Overflow
0
jbig2_make_global_ctx(Jbig2Ctx *ctx) { return (Jbig2GlobalCtx *) ctx; }
@@ -379,7 +379,7 @@ typedef struct { } Jbig2WordStreamBuf; static int -jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word) +jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, size_t offset, uint32_t *word) { Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self; const byte *data = z->data; @@ -390,7 +390,7 @@ jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t else if (offset > z->size) return -1; else { - int i; + size_t i; result = 0; for (i = 0; i < z->size - offset; i++)
CWE-119
null
null
14,442
jbig2_decode_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int8_t *gbat = params->gbat; if (image->stride * image->height > (1 << 24) && segment->data_length < image->stride * image->height / 256) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "region is far larger than data provided (%d << %d), aborting to prevent DOS", segment->data_length, image->stride * image->height); } if (!params->MMR && params->TPGDON) return jbig2_decode_generic_region_TPGDON(ctx, segment, params, as, image, GB_stats); if (!params->MMR && params->GBTEMPLATE == 0) { if (gbat[0] == +3 && gbat[1] == -1 && gbat[2] == -3 && gbat[3] == -1 && gbat[4] == +2 && gbat[5] == -2 && gbat[6] == -2 && gbat[7] == -2) return jbig2_decode_generic_template0(ctx, segment, params, as, image, GB_stats); else return jbig2_decode_generic_template0_unopt(ctx, segment, params, as, image, GB_stats); } else if (!params->MMR && params->GBTEMPLATE == 1) return jbig2_decode_generic_template1(ctx, segment, params, as, image, GB_stats); else if (!params->MMR && params->GBTEMPLATE == 2) { if (gbat[0] == 3 && gbat[1] == -1) return jbig2_decode_generic_template2a(ctx, segment, params, as, image, GB_stats); else return jbig2_decode_generic_template2(ctx, segment, params, as, image, GB_stats); } else if (!params->MMR && params->GBTEMPLATE == 3) { if (gbat[0] == 2 && gbat[1] == -1) return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats); else return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats); } { int i; for (i = 0; i < 8; i++) jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "gbat[%d] = %d", i, params->gbat[i]); } jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "decode_generic_region: MMR=%d, GBTEMPLATE=%d NYI", params->MMR, params->GBTEMPLATE); return -1; }
Overflow
0
jbig2_decode_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int8_t *gbat = params->gbat; if (image->stride * image->height > (1 << 24) && segment->data_length < image->stride * image->height / 256) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "region is far larger than data provided (%d << %d), aborting to prevent DOS", segment->data_length, image->stride * image->height); } if (!params->MMR && params->TPGDON) return jbig2_decode_generic_region_TPGDON(ctx, segment, params, as, image, GB_stats); if (!params->MMR && params->GBTEMPLATE == 0) { if (gbat[0] == +3 && gbat[1] == -1 && gbat[2] == -3 && gbat[3] == -1 && gbat[4] == +2 && gbat[5] == -2 && gbat[6] == -2 && gbat[7] == -2) return jbig2_decode_generic_template0(ctx, segment, params, as, image, GB_stats); else return jbig2_decode_generic_template0_unopt(ctx, segment, params, as, image, GB_stats); } else if (!params->MMR && params->GBTEMPLATE == 1) return jbig2_decode_generic_template1(ctx, segment, params, as, image, GB_stats); else if (!params->MMR && params->GBTEMPLATE == 2) { if (gbat[0] == 3 && gbat[1] == -1) return jbig2_decode_generic_template2a(ctx, segment, params, as, image, GB_stats); else return jbig2_decode_generic_template2(ctx, segment, params, as, image, GB_stats); } else if (!params->MMR && params->GBTEMPLATE == 3) { if (gbat[0] == 2 && gbat[1] == -1) return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats); else return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats); } { int i; for (i = 0; i < 8; i++) jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "gbat[%d] = %d", i, params->gbat[i]); } jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "decode_generic_region: MMR=%d, GBTEMPLATE=%d NYI", params->MMR, params->GBTEMPLATE); return -1; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,443
jbig2_decode_generic_region_TPGDON(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { switch (params->GBTEMPLATE) { case 0: return jbig2_decode_generic_template0_TPGDON(ctx, segment, params, as, image, GB_stats); case 1: return jbig2_decode_generic_template1_TPGDON(ctx, segment, params, as, image, GB_stats); case 2: return jbig2_decode_generic_template2_TPGDON(ctx, segment, params, as, image, GB_stats); case 3: return jbig2_decode_generic_template3_TPGDON(ctx, segment, params, as, image, GB_stats); } return -1; }
Overflow
0
jbig2_decode_generic_region_TPGDON(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { switch (params->GBTEMPLATE) { case 0: return jbig2_decode_generic_template0_TPGDON(ctx, segment, params, as, image, GB_stats); case 1: return jbig2_decode_generic_template1_TPGDON(ctx, segment, params, as, image, GB_stats); case 2: return jbig2_decode_generic_template2_TPGDON(ctx, segment, params, as, image, GB_stats); case 3: return jbig2_decode_generic_template3_TPGDON(ctx, segment, params, as, image, GB_stats); } return -1; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,444
jbig2_decode_generic_template0(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; const int rowstride = image->stride; int x, y; byte *gbreg_line = (byte *) image->data; /* todo: currently we only handle the nominal gbat location */ #ifdef OUTPUT_PBM printf("P4\n%d %d\n", GBW, GBH); #endif if (GBW <= 0) return 0; for (y = 0; y < GBH; y++) { uint32_t CONTEXT; uint32_t line_m1; uint32_t line_m2; int padded_width = (GBW + 7) & -8; line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0; line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 6 : 0; CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800); /* 6.2.5.7 3d */ for (x = 0; x < padded_width; x += 8) { byte result = 0; int x_minor; int minor_width = GBW - x > 8 ? 8 : GBW - x; if (y >= 1) line_m1 = (line_m1 << 8) | (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0); if (y >= 2) line_m2 = (line_m2 << 8) | (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 6 : 0); /* This is the speed-critical inner loop. */ for (x_minor = 0; x_minor < minor_width; x_minor++) { bool bit; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; result |= bit << (7 - x_minor); CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800); } gbreg_line[x >> 3] = result; } #ifdef OUTPUT_PBM fwrite(gbreg_line, 1, rowstride, stdout); #endif gbreg_line += rowstride; } return 0; }
Overflow
0
jbig2_decode_generic_template0(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; const int rowstride = image->stride; int x, y; byte *gbreg_line = (byte *) image->data; /* todo: currently we only handle the nominal gbat location */ #ifdef OUTPUT_PBM printf("P4\n%d %d\n", GBW, GBH); #endif if (GBW <= 0) return 0; for (y = 0; y < GBH; y++) { uint32_t CONTEXT; uint32_t line_m1; uint32_t line_m2; int padded_width = (GBW + 7) & -8; line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0; line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 6 : 0; CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800); /* 6.2.5.7 3d */ for (x = 0; x < padded_width; x += 8) { byte result = 0; int x_minor; int minor_width = GBW - x > 8 ? 8 : GBW - x; if (y >= 1) line_m1 = (line_m1 << 8) | (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0); if (y >= 2) line_m2 = (line_m2 << 8) | (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 6 : 0); /* This is the speed-critical inner loop. */ for (x_minor = 0; x_minor < minor_width; x_minor++) { bool bit; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; result |= bit << (7 - x_minor); CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800); } gbreg_line[x >> 3] = result; } #ifdef OUTPUT_PBM fwrite(gbreg_line, 1, rowstride, stdout); #endif gbreg_line += rowstride; } return 0; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,445
jbig2_decode_generic_template0_TPGDON(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; int LTP = 0; for (y = 0; y < GBH; y++) { bit = jbig2_arith_decode(as, &GB_stats[0x9B25]); if (bit < 0) return -1; LTP ^= bit; if (!LTP) { for (x = 0; x < GBW; x++) { CONTEXT = jbig2_image_get_pixel(image, x - 1, y); CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2; CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4; CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 7; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 8; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 9; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 12; CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 13; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 14; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } else { copy_prev_row(image, y); } } return 0; }
Overflow
0
jbig2_decode_generic_template0_TPGDON(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; int LTP = 0; for (y = 0; y < GBH; y++) { bit = jbig2_arith_decode(as, &GB_stats[0x9B25]); if (bit < 0) return -1; LTP ^= bit; if (!LTP) { for (x = 0; x < GBW; x++) { CONTEXT = jbig2_image_get_pixel(image, x - 1, y); CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2; CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4; CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 7; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 8; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 9; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 12; CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 13; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 14; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } else { copy_prev_row(image, y); } } return 0; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,446
jbig2_decode_generic_template0_unopt(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; /* this version is generic and easy to understand, but very slow */ for (y = 0; y < GBH; y++) { for (x = 0; x < GBW; x++) { CONTEXT = 0; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2; CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4; CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 7; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 8; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 9; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 12; CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 2) << 13; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 14; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } return 0; }
Overflow
0
jbig2_decode_generic_template0_unopt(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; /* this version is generic and easy to understand, but very slow */ for (y = 0; y < GBH; y++) { for (x = 0; x < GBW; x++) { CONTEXT = 0; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2; CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4; CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 7; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 8; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 9; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 12; CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 2) << 13; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 14; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } return 0; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,447
jbig2_decode_generic_template1_TPGDON(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; int LTP = 0; for (y = 0; y < GBH; y++) { bit = jbig2_arith_decode(as, &GB_stats[0x0795]); if (bit < 0) return -1; LTP ^= bit; if (!LTP) { for (x = 0; x < GBW; x++) { CONTEXT = jbig2_image_get_pixel(image, x - 1, y); CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3; CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 4; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8; CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 2) << 9; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 10; CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 11; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 12; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } else { copy_prev_row(image, y); } } return 0; }
Overflow
0
jbig2_decode_generic_template1_TPGDON(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; int LTP = 0; for (y = 0; y < GBH; y++) { bit = jbig2_arith_decode(as, &GB_stats[0x0795]); if (bit < 0) return -1; LTP ^= bit; if (!LTP) { for (x = 0; x < GBW; x++) { CONTEXT = jbig2_image_get_pixel(image, x - 1, y); CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3; CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 4; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8; CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 2) << 9; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 10; CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 11; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 12; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } else { copy_prev_row(image, y); } } return 0; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,448
jbig2_decode_generic_template2(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; const int rowstride = image->stride; int x, y; byte *gbreg_line = (byte *) image->data; /* todo: currently we only handle the nominal gbat location */ #ifdef OUTPUT_PBM printf("P4\n%d %d\n", GBW, GBH); #endif if (GBW <= 0) return 0; for (y = 0; y < GBH; y++) { uint32_t CONTEXT; uint32_t line_m1; uint32_t line_m2; int padded_width = (GBW + 7) & -8; line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0; line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 4 : 0; CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380); /* 6.2.5.7 3d */ for (x = 0; x < padded_width; x += 8) { byte result = 0; int x_minor; int minor_width = GBW - x > 8 ? 8 : GBW - x; if (y >= 1) line_m1 = (line_m1 << 8) | (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0); if (y >= 2) line_m2 = (line_m2 << 8) | (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 4 : 0); /* This is the speed-critical inner loop. */ for (x_minor = 0; x_minor < minor_width; x_minor++) { bool bit; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; result |= bit << (7 - x_minor); CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80); } gbreg_line[x >> 3] = result; } #ifdef OUTPUT_PBM fwrite(gbreg_line, 1, rowstride, stdout); #endif gbreg_line += rowstride; } return 0; }
Overflow
0
jbig2_decode_generic_template2(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; const int rowstride = image->stride; int x, y; byte *gbreg_line = (byte *) image->data; /* todo: currently we only handle the nominal gbat location */ #ifdef OUTPUT_PBM printf("P4\n%d %d\n", GBW, GBH); #endif if (GBW <= 0) return 0; for (y = 0; y < GBH; y++) { uint32_t CONTEXT; uint32_t line_m1; uint32_t line_m2; int padded_width = (GBW + 7) & -8; line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0; line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 4 : 0; CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380); /* 6.2.5.7 3d */ for (x = 0; x < padded_width; x += 8) { byte result = 0; int x_minor; int minor_width = GBW - x > 8 ? 8 : GBW - x; if (y >= 1) line_m1 = (line_m1 << 8) | (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0); if (y >= 2) line_m2 = (line_m2 << 8) | (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 4 : 0); /* This is the speed-critical inner loop. */ for (x_minor = 0; x_minor < minor_width; x_minor++) { bool bit; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; result |= bit << (7 - x_minor); CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80); } gbreg_line[x >> 3] = result; } #ifdef OUTPUT_PBM fwrite(gbreg_line, 1, rowstride, stdout); #endif gbreg_line += rowstride; } return 0; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,449
jbig2_decode_generic_template2_TPGDON(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; int LTP = 0; for (y = 0; y < GBH; y++) { bit = jbig2_arith_decode(as, &GB_stats[0xE5]); if (bit < 0) return -1; LTP ^= bit; if (!LTP) { for (x = 0; x < GBW; x++) { CONTEXT = jbig2_image_get_pixel(image, x - 1, y); CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 3; CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 4; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 7; CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 8; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 9; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } else { copy_prev_row(image, y); } } return 0; }
Overflow
0
jbig2_decode_generic_template2_TPGDON(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; int LTP = 0; for (y = 0; y < GBH; y++) { bit = jbig2_arith_decode(as, &GB_stats[0xE5]); if (bit < 0) return -1; LTP ^= bit; if (!LTP) { for (x = 0; x < GBW; x++) { CONTEXT = jbig2_image_get_pixel(image, x - 1, y); CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 3; CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 4; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 7; CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 8; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 9; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } else { copy_prev_row(image, y); } } return 0; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,450
jbig2_decode_generic_template3_TPGDON(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; int LTP = 0; for (y = 0; y < GBH; y++) { bit = jbig2_arith_decode(as, &GB_stats[0x0195]); if (bit < 0) return -1; LTP ^= bit; if (!LTP) { for (x = 0; x < GBW; x++) { CONTEXT = jbig2_image_get_pixel(image, x - 1, y); CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2; CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } else { copy_prev_row(image, y); } } return 0; }
Overflow
0
jbig2_decode_generic_template3_TPGDON(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; int LTP = 0; for (y = 0; y < GBH; y++) { bit = jbig2_arith_decode(as, &GB_stats[0x0195]); if (bit < 0) return -1; LTP ^= bit; if (!LTP) { for (x = 0; x < GBW; x++) { CONTEXT = jbig2_image_get_pixel(image, x - 1, y); CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2; CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } else { copy_prev_row(image, y); } } return 0; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,451
jbig2_decode_generic_template3_unopt(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; /* this version is generic and easy to understand, but very slow */ for (y = 0; y < GBH; y++) { for (x = 0; x < GBW; x++) { CONTEXT = 0; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2; CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } return 0; }
Overflow
0
jbig2_decode_generic_template3_unopt(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats) { const int GBW = image->width; const int GBH = image->height; uint32_t CONTEXT; int x, y; bool bit; /* this version is generic and easy to understand, but very slow */ for (y = 0; y < GBH; y++) { for (x = 0; x < GBW; x++) { CONTEXT = 0; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2; CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3; CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4; CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5; CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 6; CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7; CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8; CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9; bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]); if (bit < 0) return -1; jbig2_image_set_pixel(image, x, y, bit); } } return 0; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,452
jbig2_generic_stats_size(Jbig2Ctx *ctx, int template) { int stats_size = template == 0 ? 1 << 16 : template == 1 ? 1 << 1 << 13 : 1 << 10; return stats_size; }
Overflow
0
jbig2_generic_stats_size(Jbig2Ctx *ctx, int template) { int stats_size = template == 0 ? 1 << 16 : template == 1 ? 1 << 1 << 13 : 1 << 10; return stats_size; }
@@ -718,7 +718,7 @@ jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte byte seg_flags; int8_t gbat[8]; int offset; - int gbat_bytes = 0; + uint32_t gbat_bytes = 0; Jbig2GenericRegionParams params; int code = 0; Jbig2Image *image = NULL;
CWE-119
null
null
14,453
jbig2_decode_pattern_dict(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2PatternDictParams *params, const byte *data, const size_t size, Jbig2ArithCx *GB_stats) { Jbig2PatternDict *hd = NULL; Jbig2Image *image = NULL; Jbig2GenericRegionParams rparams; int code = 0; /* allocate the collective image */ image = jbig2_image_new(ctx, params->HDPW * (params->GRAYMAX + 1), params->HDPH); if (image == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate collective bitmap for halftone dict!"); return NULL; } /* fill out the generic region decoder parameters */ rparams.MMR = params->HDMMR; rparams.GBTEMPLATE = params->HDTEMPLATE; rparams.TPGDON = 0; /* not used if HDMMR = 1 */ rparams.USESKIP = 0; rparams.gbat[0] = -(int8_t) params->HDPW; rparams.gbat[1] = 0; rparams.gbat[2] = -3; rparams.gbat[3] = -1; rparams.gbat[4] = 2; rparams.gbat[5] = -2; rparams.gbat[6] = -2; rparams.gbat[7] = -2; if (params->HDMMR) { code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data, size, image); } else { Jbig2WordStream *ws = jbig2_word_stream_buf_new(ctx, data, size); if (ws != NULL) { Jbig2ArithState *as = jbig2_arith_new(ctx, ws); if (as != NULL) { code = jbig2_decode_generic_region(ctx, segment, &rparams, as, image, GB_stats); } else { code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate storage for as in halftone dict!"); } jbig2_free(ctx->allocator, as); jbig2_word_stream_buf_free(ctx, ws); } else { code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate storage for ws in halftone dict!"); } } if (code == 0) hd = jbig2_hd_new(ctx, params, image); jbig2_image_release(ctx, image); return hd; }
Overflow
0
jbig2_decode_pattern_dict(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2PatternDictParams *params, const byte *data, const size_t size, Jbig2ArithCx *GB_stats) { Jbig2PatternDict *hd = NULL; Jbig2Image *image = NULL; Jbig2GenericRegionParams rparams; int code = 0; /* allocate the collective image */ image = jbig2_image_new(ctx, params->HDPW * (params->GRAYMAX + 1), params->HDPH); if (image == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate collective bitmap for halftone dict!"); return NULL; } /* fill out the generic region decoder parameters */ rparams.MMR = params->HDMMR; rparams.GBTEMPLATE = params->HDTEMPLATE; rparams.TPGDON = 0; /* not used if HDMMR = 1 */ rparams.USESKIP = 0; rparams.gbat[0] = -(int8_t) params->HDPW; rparams.gbat[1] = 0; rparams.gbat[2] = -3; rparams.gbat[3] = -1; rparams.gbat[4] = 2; rparams.gbat[5] = -2; rparams.gbat[6] = -2; rparams.gbat[7] = -2; if (params->HDMMR) { code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data, size, image); } else { Jbig2WordStream *ws = jbig2_word_stream_buf_new(ctx, data, size); if (ws != NULL) { Jbig2ArithState *as = jbig2_arith_new(ctx, ws); if (as != NULL) { code = jbig2_decode_generic_region(ctx, segment, &rparams, as, image, GB_stats); } else { code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate storage for as in halftone dict!"); } jbig2_free(ctx->allocator, as); jbig2_word_stream_buf_free(ctx, ws); } else { code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate storage for ws in halftone dict!"); } } if (code == 0) hd = jbig2_hd_new(ctx, params, image); jbig2_image_release(ctx, image); return hd; }
@@ -257,8 +257,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, { uint8_t **GSVALS = NULL; size_t consumed_bytes = 0; - int i, j, code, stride; - int x, y; + uint32_t i, j, stride, x, y; + int code; Jbig2Image **GSPLANES; Jbig2GenericRegionParams rparams; Jbig2WordStream *ws = NULL; @@ -276,9 +276,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, if (GSPLANES[i] == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate %dx%d image for GSPLANES", GSW, GSH); /* free already allocated */ - for (j = i - 1; j >= 0; --j) { - jbig2_image_release(ctx, GSPLANES[j]); - } + for (j = i; j > 0;) + jbig2_image_release(ctx, GSPLANES[--j]); jbig2_free(ctx->allocator, GSPLANES); return NULL; } @@ -323,9 +322,10 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, } /* C.5 step 2. Set j = GSBPP-2 */ - j = GSBPP - 2; + j = GSBPP - 1; /* C.5 step 3. decode loop */ - while (j >= 0) { + while (j > 0) { + j--; /* C.5 step 3. (a) */ if (GSMMR) { code = jbig2_decode_halftone_mmr(ctx, &rparams, data + consumed_bytes, size - consumed_bytes, GSPLANES[j], &consumed_bytes); @@ -345,7 +345,6 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, GSPLANES[j]->data[i] ^= GSPLANES[j + 1]->data[i]; /* C.5 step 3. (c) */ - --j; } /* allocate GSVALS */ @@ -359,9 +358,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, if (GSVALS[i] == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSH * GSW); /* free already allocated */ - for (j = i - 1; j >= 0; --j) { - jbig2_free(ctx->allocator, GSVALS[j]); - } + for (j = i; j > 0;) + jbig2_free(ctx->allocator, GSVALS[--j]); jbig2_free(ctx->allocator, GSVALS); GSVALS = NULL; goto cleanup; @@ -450,7 +448,7 @@ jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, uint8_t **GI; Jbig2Image *HSKIP = NULL; Jbig2PatternDict *HPATS; - int i; + uint32_t i; uint32_t mg, ng; int32_t x, y; uint8_t gray_val; @@ -476,7 +474,7 @@ jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, /* calculate ceil(log2(HNUMPATS)) */ HBPP = 0; - while (HNUMPATS > (1 << ++HBPP)); + while (HNUMPATS > (1U << ++HBPP)); /* 6.6.5 point 4. decode gray-scale image as mentioned in annex C */ GI = jbig2_decode_gray_scale_image(ctx, segment, data, size,
CWE-119
null
null
14,454
jbig2_hd_new(Jbig2Ctx *ctx, const Jbig2PatternDictParams *params, Jbig2Image *image) { Jbig2PatternDict *new; const int N = params->GRAYMAX + 1; const int HPW = params->HDPW; const int HPH = params->HDPH; int i; /* allocate a new struct */ new = jbig2_new(ctx, Jbig2PatternDict, 1); if (new != NULL) { new->patterns = jbig2_new(ctx, Jbig2Image *, N); if (new->patterns == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate pattern in collective bitmap dictionary"); jbig2_free(ctx->allocator, new); return NULL; } new->n_patterns = N; new->HPW = HPW; new->HPH = HPH; /* 6.7.5(4) - copy out the individual pattern images */ for (i = 0; i < N; i++) { new->patterns[i] = jbig2_image_new(ctx, HPW, HPH); if (new->patterns[i] == NULL) { int j; jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to allocate pattern element image"); for (j = 0; j < i; j++) jbig2_free(ctx->allocator, new->patterns[j]); jbig2_free(ctx->allocator, new); return NULL; } /* compose with the REPLACE operator; the source will be clipped to the destintion, selecting the proper sub image */ jbig2_image_compose(ctx, new->patterns[i], image, -i * HPW, 0, JBIG2_COMPOSE_REPLACE); } } else { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate collective bitmap dictionary"); } return new; }
Overflow
0
jbig2_hd_new(Jbig2Ctx *ctx, const Jbig2PatternDictParams *params, Jbig2Image *image) { Jbig2PatternDict *new; const int N = params->GRAYMAX + 1; const int HPW = params->HDPW; const int HPH = params->HDPH; int i; /* allocate a new struct */ new = jbig2_new(ctx, Jbig2PatternDict, 1); if (new != NULL) { new->patterns = jbig2_new(ctx, Jbig2Image *, N); if (new->patterns == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate pattern in collective bitmap dictionary"); jbig2_free(ctx->allocator, new); return NULL; } new->n_patterns = N; new->HPW = HPW; new->HPH = HPH; /* 6.7.5(4) - copy out the individual pattern images */ for (i = 0; i < N; i++) { new->patterns[i] = jbig2_image_new(ctx, HPW, HPH); if (new->patterns[i] == NULL) { int j; jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to allocate pattern element image"); for (j = 0; j < i; j++) jbig2_free(ctx->allocator, new->patterns[j]); jbig2_free(ctx->allocator, new); return NULL; } /* compose with the REPLACE operator; the source will be clipped to the destintion, selecting the proper sub image */ jbig2_image_compose(ctx, new->patterns[i], image, -i * HPW, 0, JBIG2_COMPOSE_REPLACE); } } else { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate collective bitmap dictionary"); } return new; }
@@ -257,8 +257,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, { uint8_t **GSVALS = NULL; size_t consumed_bytes = 0; - int i, j, code, stride; - int x, y; + uint32_t i, j, stride, x, y; + int code; Jbig2Image **GSPLANES; Jbig2GenericRegionParams rparams; Jbig2WordStream *ws = NULL; @@ -276,9 +276,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, if (GSPLANES[i] == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate %dx%d image for GSPLANES", GSW, GSH); /* free already allocated */ - for (j = i - 1; j >= 0; --j) { - jbig2_image_release(ctx, GSPLANES[j]); - } + for (j = i; j > 0;) + jbig2_image_release(ctx, GSPLANES[--j]); jbig2_free(ctx->allocator, GSPLANES); return NULL; } @@ -323,9 +322,10 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, } /* C.5 step 2. Set j = GSBPP-2 */ - j = GSBPP - 2; + j = GSBPP - 1; /* C.5 step 3. decode loop */ - while (j >= 0) { + while (j > 0) { + j--; /* C.5 step 3. (a) */ if (GSMMR) { code = jbig2_decode_halftone_mmr(ctx, &rparams, data + consumed_bytes, size - consumed_bytes, GSPLANES[j], &consumed_bytes); @@ -345,7 +345,6 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, GSPLANES[j]->data[i] ^= GSPLANES[j + 1]->data[i]; /* C.5 step 3. (c) */ - --j; } /* allocate GSVALS */ @@ -359,9 +358,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, if (GSVALS[i] == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSH * GSW); /* free already allocated */ - for (j = i - 1; j >= 0; --j) { - jbig2_free(ctx->allocator, GSVALS[j]); - } + for (j = i; j > 0;) + jbig2_free(ctx->allocator, GSVALS[--j]); jbig2_free(ctx->allocator, GSVALS); GSVALS = NULL; goto cleanup; @@ -450,7 +448,7 @@ jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, uint8_t **GI; Jbig2Image *HSKIP = NULL; Jbig2PatternDict *HPATS; - int i; + uint32_t i; uint32_t mg, ng; int32_t x, y; uint8_t gray_val; @@ -476,7 +474,7 @@ jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, /* calculate ceil(log2(HNUMPATS)) */ HBPP = 0; - while (HNUMPATS > (1 << ++HBPP)); + while (HNUMPATS > (1U << ++HBPP)); /* 6.6.5 point 4. decode gray-scale image as mentioned in annex C */ GI = jbig2_decode_gray_scale_image(ctx, segment, data, size,
CWE-119
null
null
14,455
jbig2_hd_release(Jbig2Ctx *ctx, Jbig2PatternDict *dict) { int i; if (dict == NULL) return; for (i = 0; i < dict->n_patterns; i++) if (dict->patterns[i]) jbig2_image_release(ctx, dict->patterns[i]); jbig2_free(ctx->allocator, dict->patterns); jbig2_free(ctx->allocator, dict); }
Overflow
0
jbig2_hd_release(Jbig2Ctx *ctx, Jbig2PatternDict *dict) { int i; if (dict == NULL) return; for (i = 0; i < dict->n_patterns; i++) if (dict->patterns[i]) jbig2_image_release(ctx, dict->patterns[i]); jbig2_free(ctx->allocator, dict->patterns); jbig2_free(ctx->allocator, dict); }
@@ -257,8 +257,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, { uint8_t **GSVALS = NULL; size_t consumed_bytes = 0; - int i, j, code, stride; - int x, y; + uint32_t i, j, stride, x, y; + int code; Jbig2Image **GSPLANES; Jbig2GenericRegionParams rparams; Jbig2WordStream *ws = NULL; @@ -276,9 +276,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, if (GSPLANES[i] == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate %dx%d image for GSPLANES", GSW, GSH); /* free already allocated */ - for (j = i - 1; j >= 0; --j) { - jbig2_image_release(ctx, GSPLANES[j]); - } + for (j = i; j > 0;) + jbig2_image_release(ctx, GSPLANES[--j]); jbig2_free(ctx->allocator, GSPLANES); return NULL; } @@ -323,9 +322,10 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, } /* C.5 step 2. Set j = GSBPP-2 */ - j = GSBPP - 2; + j = GSBPP - 1; /* C.5 step 3. decode loop */ - while (j >= 0) { + while (j > 0) { + j--; /* C.5 step 3. (a) */ if (GSMMR) { code = jbig2_decode_halftone_mmr(ctx, &rparams, data + consumed_bytes, size - consumed_bytes, GSPLANES[j], &consumed_bytes); @@ -345,7 +345,6 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, GSPLANES[j]->data[i] ^= GSPLANES[j + 1]->data[i]; /* C.5 step 3. (c) */ - --j; } /* allocate GSVALS */ @@ -359,9 +358,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, if (GSVALS[i] == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSH * GSW); /* free already allocated */ - for (j = i - 1; j >= 0; --j) { - jbig2_free(ctx->allocator, GSVALS[j]); - } + for (j = i; j > 0;) + jbig2_free(ctx->allocator, GSVALS[--j]); jbig2_free(ctx->allocator, GSVALS); GSVALS = NULL; goto cleanup; @@ -450,7 +448,7 @@ jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, uint8_t **GI; Jbig2Image *HSKIP = NULL; Jbig2PatternDict *HPATS; - int i; + uint32_t i; uint32_t mg, ng; int32_t x, y; uint8_t gray_val; @@ -476,7 +474,7 @@ jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, /* calculate ceil(log2(HNUMPATS)) */ HBPP = 0; - while (HNUMPATS > (1 << ++HBPP)); + while (HNUMPATS > (1U << ++HBPP)); /* 6.6.5 point 4. decode gray-scale image as mentioned in annex C */ GI = jbig2_decode_gray_scale_image(ctx, segment, data, size,
CWE-119
null
null
14,456
jbig2_pattern_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data) { Jbig2PatternDictParams params; Jbig2ArithCx *GB_stats = NULL; byte flags; int offset = 0; /* 7.4.4.1 - Data header */ if (segment->data_length < 7) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short"); } flags = segment_data[0]; params.HDMMR = flags & 1; params.HDTEMPLATE = (flags & 6) >> 1; params.HDPW = segment_data[1]; params.HDPH = segment_data[2]; params.GRAYMAX = jbig2_get_uint32(segment_data + 3); offset += 7; jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "pattern dictionary, flags=%02x, %d grays (%dx%d cell)", flags, params.GRAYMAX + 1, params.HDPW, params.HDPH); if (params.HDMMR && params.HDTEMPLATE) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HDTEMPLATE is %d when HDMMR is %d, contrary to spec", params.HDTEMPLATE, params.HDMMR); } if (flags & 0xf8) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "Reserved flag bits non-zero"); } /* 7.4.4.2 */ if (!params.HDMMR) { /* allocate and zero arithmetic coding stats */ int stats_size = jbig2_generic_stats_size(ctx, params.HDTEMPLATE); GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size); if (GB_stats == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GB_stats in pattern dictionary"); return 0; } memset(GB_stats, 0, stats_size); } segment->result = jbig2_decode_pattern_dict(ctx, segment, &params, segment_data + offset, segment->data_length - offset, GB_stats); /* todo: retain GB_stats? */ if (!params.HDMMR) { jbig2_free(ctx->allocator, GB_stats); } return (segment->result != NULL) ? 0 : -1; }
Overflow
0
jbig2_pattern_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data) { Jbig2PatternDictParams params; Jbig2ArithCx *GB_stats = NULL; byte flags; int offset = 0; /* 7.4.4.1 - Data header */ if (segment->data_length < 7) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short"); } flags = segment_data[0]; params.HDMMR = flags & 1; params.HDTEMPLATE = (flags & 6) >> 1; params.HDPW = segment_data[1]; params.HDPH = segment_data[2]; params.GRAYMAX = jbig2_get_uint32(segment_data + 3); offset += 7; jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "pattern dictionary, flags=%02x, %d grays (%dx%d cell)", flags, params.GRAYMAX + 1, params.HDPW, params.HDPH); if (params.HDMMR && params.HDTEMPLATE) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HDTEMPLATE is %d when HDMMR is %d, contrary to spec", params.HDTEMPLATE, params.HDMMR); } if (flags & 0xf8) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "Reserved flag bits non-zero"); } /* 7.4.4.2 */ if (!params.HDMMR) { /* allocate and zero arithmetic coding stats */ int stats_size = jbig2_generic_stats_size(ctx, params.HDTEMPLATE); GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size); if (GB_stats == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GB_stats in pattern dictionary"); return 0; } memset(GB_stats, 0, stats_size); } segment->result = jbig2_decode_pattern_dict(ctx, segment, &params, segment_data + offset, segment->data_length - offset, GB_stats); /* todo: retain GB_stats? */ if (!params.HDMMR) { jbig2_free(ctx->allocator, GB_stats); } return (segment->result != NULL) ? 0 : -1; }
@@ -257,8 +257,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, { uint8_t **GSVALS = NULL; size_t consumed_bytes = 0; - int i, j, code, stride; - int x, y; + uint32_t i, j, stride, x, y; + int code; Jbig2Image **GSPLANES; Jbig2GenericRegionParams rparams; Jbig2WordStream *ws = NULL; @@ -276,9 +276,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, if (GSPLANES[i] == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate %dx%d image for GSPLANES", GSW, GSH); /* free already allocated */ - for (j = i - 1; j >= 0; --j) { - jbig2_image_release(ctx, GSPLANES[j]); - } + for (j = i; j > 0;) + jbig2_image_release(ctx, GSPLANES[--j]); jbig2_free(ctx->allocator, GSPLANES); return NULL; } @@ -323,9 +322,10 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, } /* C.5 step 2. Set j = GSBPP-2 */ - j = GSBPP - 2; + j = GSBPP - 1; /* C.5 step 3. decode loop */ - while (j >= 0) { + while (j > 0) { + j--; /* C.5 step 3. (a) */ if (GSMMR) { code = jbig2_decode_halftone_mmr(ctx, &rparams, data + consumed_bytes, size - consumed_bytes, GSPLANES[j], &consumed_bytes); @@ -345,7 +345,6 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, GSPLANES[j]->data[i] ^= GSPLANES[j + 1]->data[i]; /* C.5 step 3. (c) */ - --j; } /* allocate GSVALS */ @@ -359,9 +358,8 @@ jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment, if (GSVALS[i] == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSH * GSW); /* free already allocated */ - for (j = i - 1; j >= 0; --j) { - jbig2_free(ctx->allocator, GSVALS[j]); - } + for (j = i; j > 0;) + jbig2_free(ctx->allocator, GSVALS[--j]); jbig2_free(ctx->allocator, GSVALS); GSVALS = NULL; goto cleanup; @@ -450,7 +448,7 @@ jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, uint8_t **GI; Jbig2Image *HSKIP = NULL; Jbig2PatternDict *HPATS; - int i; + uint32_t i; uint32_t mg, ng; int32_t x, y; uint8_t gray_val; @@ -476,7 +474,7 @@ jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, /* calculate ceil(log2(HNUMPATS)) */ HBPP = 0; - while (HNUMPATS > (1 << ++HBPP)); + while (HNUMPATS > (1U << ++HBPP)); /* 6.6.5 point 4. decode gray-scale image as mentioned in annex C */ GI = jbig2_decode_gray_scale_image(ctx, segment, data, size,
CWE-119
null
null
14,457
jbig2_build_huffman_table(Jbig2Ctx *ctx, const Jbig2HuffmanParams *params) { int *LENCOUNT; int LENMAX = -1; const int lencountcount = 256; const Jbig2HuffmanLine *lines = params->lines; int n_lines = params->n_lines; int i, j; int max_j; int log_table_size = 0; Jbig2HuffmanTable *result; Jbig2HuffmanEntry *entries; int CURLEN; int firstcode = 0; int CURCODE; int CURTEMP; LENCOUNT = jbig2_new(ctx, int, lencountcount); if (LENCOUNT == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate storage for huffman histogram"); return NULL; } memset(LENCOUNT, 0, sizeof(int) * lencountcount); /* B.3, 1. */ for (i = 0; i < params->n_lines; i++) { int PREFLEN = lines[i].PREFLEN; int lts; if (PREFLEN > LENMAX) { for (j = LENMAX + 1; j < PREFLEN + 1; j++) LENCOUNT[j] = 0; LENMAX = PREFLEN; } LENCOUNT[PREFLEN]++; lts = PREFLEN + lines[i].RANGELEN; if (lts > LOG_TABLE_SIZE_MAX) lts = PREFLEN; if (lts <= LOG_TABLE_SIZE_MAX && log_table_size < lts) log_table_size = lts; } jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "constructing huffman table log size %d", log_table_size); max_j = 1 << log_table_size; result = jbig2_new(ctx, Jbig2HuffmanTable, 1); if (result == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate result storage in jbig2_build_huffman_table"); jbig2_free(ctx->allocator, LENCOUNT); return NULL; } result->log_table_size = log_table_size; entries = jbig2_new(ctx, Jbig2HuffmanEntry, max_j); if (entries == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate entries storage in jbig2_build_huffman_table"); jbig2_free(ctx->allocator, result); jbig2_free(ctx->allocator, LENCOUNT); return NULL; } /* fill now to catch missing JBIG2Globals later */ memset(entries, 0xFF, sizeof(Jbig2HuffmanEntry) * max_j); result->entries = entries; LENCOUNT[0] = 0; for (CURLEN = 1; CURLEN <= LENMAX; CURLEN++) { int shift = log_table_size - CURLEN; /* B.3 3.(a) */ firstcode = (firstcode + LENCOUNT[CURLEN - 1]) << 1; CURCODE = firstcode; /* B.3 3.(b) */ for (CURTEMP = 0; CURTEMP < n_lines; CURTEMP++) { int PREFLEN = lines[CURTEMP].PREFLEN; if (PREFLEN == CURLEN) { int RANGELEN = lines[CURTEMP].RANGELEN; int start_j = CURCODE << shift; int end_j = (CURCODE + 1) << shift; byte eflags = 0; if (end_j > max_j) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "ran off the end of the entries table! (%d >= %d)", end_j, max_j); jbig2_free(ctx->allocator, result->entries); jbig2_free(ctx->allocator, result); jbig2_free(ctx->allocator, LENCOUNT); return NULL; } /* todo: build extension tables */ if (params->HTOOB && CURTEMP == n_lines - 1) eflags |= JBIG2_HUFFMAN_FLAGS_ISOOB; if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2)) eflags |= JBIG2_HUFFMAN_FLAGS_ISLOW; if (PREFLEN + RANGELEN > LOG_TABLE_SIZE_MAX) { for (j = start_j; j < end_j; j++) { entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW; entries[j].PREFLEN = PREFLEN; entries[j].RANGELEN = RANGELEN; entries[j].flags = eflags; } } else { for (j = start_j; j < end_j; j++) { int32_t HTOFFSET = (j >> (shift - RANGELEN)) & ((1 << RANGELEN) - 1); if (eflags & JBIG2_HUFFMAN_FLAGS_ISLOW) entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW - HTOFFSET; else entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW + HTOFFSET; entries[j].PREFLEN = PREFLEN + RANGELEN; entries[j].RANGELEN = 0; entries[j].flags = eflags; } } CURCODE++; } } } jbig2_free(ctx->allocator, LENCOUNT); return result; }
Overflow
0
jbig2_build_huffman_table(Jbig2Ctx *ctx, const Jbig2HuffmanParams *params) { int *LENCOUNT; int LENMAX = -1; const int lencountcount = 256; const Jbig2HuffmanLine *lines = params->lines; int n_lines = params->n_lines; int i, j; int max_j; int log_table_size = 0; Jbig2HuffmanTable *result; Jbig2HuffmanEntry *entries; int CURLEN; int firstcode = 0; int CURCODE; int CURTEMP; LENCOUNT = jbig2_new(ctx, int, lencountcount); if (LENCOUNT == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate storage for huffman histogram"); return NULL; } memset(LENCOUNT, 0, sizeof(int) * lencountcount); /* B.3, 1. */ for (i = 0; i < params->n_lines; i++) { int PREFLEN = lines[i].PREFLEN; int lts; if (PREFLEN > LENMAX) { for (j = LENMAX + 1; j < PREFLEN + 1; j++) LENCOUNT[j] = 0; LENMAX = PREFLEN; } LENCOUNT[PREFLEN]++; lts = PREFLEN + lines[i].RANGELEN; if (lts > LOG_TABLE_SIZE_MAX) lts = PREFLEN; if (lts <= LOG_TABLE_SIZE_MAX && log_table_size < lts) log_table_size = lts; } jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "constructing huffman table log size %d", log_table_size); max_j = 1 << log_table_size; result = jbig2_new(ctx, Jbig2HuffmanTable, 1); if (result == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate result storage in jbig2_build_huffman_table"); jbig2_free(ctx->allocator, LENCOUNT); return NULL; } result->log_table_size = log_table_size; entries = jbig2_new(ctx, Jbig2HuffmanEntry, max_j); if (entries == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate entries storage in jbig2_build_huffman_table"); jbig2_free(ctx->allocator, result); jbig2_free(ctx->allocator, LENCOUNT); return NULL; } /* fill now to catch missing JBIG2Globals later */ memset(entries, 0xFF, sizeof(Jbig2HuffmanEntry) * max_j); result->entries = entries; LENCOUNT[0] = 0; for (CURLEN = 1; CURLEN <= LENMAX; CURLEN++) { int shift = log_table_size - CURLEN; /* B.3 3.(a) */ firstcode = (firstcode + LENCOUNT[CURLEN - 1]) << 1; CURCODE = firstcode; /* B.3 3.(b) */ for (CURTEMP = 0; CURTEMP < n_lines; CURTEMP++) { int PREFLEN = lines[CURTEMP].PREFLEN; if (PREFLEN == CURLEN) { int RANGELEN = lines[CURTEMP].RANGELEN; int start_j = CURCODE << shift; int end_j = (CURCODE + 1) << shift; byte eflags = 0; if (end_j > max_j) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "ran off the end of the entries table! (%d >= %d)", end_j, max_j); jbig2_free(ctx->allocator, result->entries); jbig2_free(ctx->allocator, result); jbig2_free(ctx->allocator, LENCOUNT); return NULL; } /* todo: build extension tables */ if (params->HTOOB && CURTEMP == n_lines - 1) eflags |= JBIG2_HUFFMAN_FLAGS_ISOOB; if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2)) eflags |= JBIG2_HUFFMAN_FLAGS_ISLOW; if (PREFLEN + RANGELEN > LOG_TABLE_SIZE_MAX) { for (j = start_j; j < end_j; j++) { entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW; entries[j].PREFLEN = PREFLEN; entries[j].RANGELEN = RANGELEN; entries[j].flags = eflags; } } else { for (j = start_j; j < end_j; j++) { int32_t HTOFFSET = (j >> (shift - RANGELEN)) & ((1 << RANGELEN) - 1); if (eflags & JBIG2_HUFFMAN_FLAGS_ISLOW) entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW - HTOFFSET; else entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW + HTOFFSET; entries[j].PREFLEN = PREFLEN + RANGELEN; entries[j].RANGELEN = 0; entries[j].flags = eflags; } } CURCODE++; } } } jbig2_free(ctx->allocator, LENCOUNT); return result; }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,458
jbig2_dump_huffman_binary(Jbig2HuffmanState *hs) { const uint32_t word = hs->this_word; int i; fprintf(stderr, "huffman binary "); for (i = 31; i >= 0; i--) fprintf(stderr, ((word >> i) & 1) ? "1" : "0"); fprintf(stderr, "\n"); }
Overflow
0
jbig2_dump_huffman_binary(Jbig2HuffmanState *hs) { const uint32_t word = hs->this_word; int i; fprintf(stderr, "huffman binary "); for (i = 31; i >= 0; i--) fprintf(stderr, ((word >> i) & 1) ? "1" : "0"); fprintf(stderr, "\n"); }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,459
jbig2_dump_huffman_state(Jbig2HuffmanState *hs) { fprintf(stderr, "huffman state %08x %08x offset %d.%d\n", hs->this_word, hs->next_word, hs->offset, hs->offset_bits); }
Overflow
0
jbig2_dump_huffman_state(Jbig2HuffmanState *hs) { fprintf(stderr, "huffman state %08x %08x offset %d.%d\n", hs->this_word, hs->next_word, hs->offset, hs->offset_bits); }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,460
jbig2_dump_huffman_table(const Jbig2HuffmanTable *table) { int i; int table_size = (1 << table->log_table_size); fprintf(stderr, "huffman table %p (log_table_size=%d, %d entries, entryies=%p):\n", table, table->log_table_size, table_size, table->entries); for (i = 0; i < table_size; i++) { fprintf(stderr, "%6d: PREFLEN=%d, RANGELEN=%d, ", i, table->entries[i].PREFLEN, table->entries[i].RANGELEN); if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT) { fprintf(stderr, "ext=%p", table->entries[i].u.ext_table); } else { fprintf(stderr, "RANGELOW=%d", table->entries[i].u.RANGELOW); } if (table->entries[i].flags) { int need_comma = 0; fprintf(stderr, ", flags=0x%x(", table->entries[i].flags); if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISOOB) { fprintf(stderr, "OOB"); need_comma = 1; } if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISLOW) { if (need_comma) fprintf(stderr, ","); fprintf(stderr, "LOW"); need_comma = 1; } if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT) { if (need_comma) fprintf(stderr, ","); fprintf(stderr, "EXT"); } fprintf(stderr, ")"); } fprintf(stderr, "\n"); } fprintf(stderr, "\n"); }
Overflow
0
jbig2_dump_huffman_table(const Jbig2HuffmanTable *table) { int i; int table_size = (1 << table->log_table_size); fprintf(stderr, "huffman table %p (log_table_size=%d, %d entries, entryies=%p):\n", table, table->log_table_size, table_size, table->entries); for (i = 0; i < table_size; i++) { fprintf(stderr, "%6d: PREFLEN=%d, RANGELEN=%d, ", i, table->entries[i].PREFLEN, table->entries[i].RANGELEN); if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT) { fprintf(stderr, "ext=%p", table->entries[i].u.ext_table); } else { fprintf(stderr, "RANGELOW=%d", table->entries[i].u.RANGELOW); } if (table->entries[i].flags) { int need_comma = 0; fprintf(stderr, ", flags=0x%x(", table->entries[i].flags); if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISOOB) { fprintf(stderr, "OOB"); need_comma = 1; } if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISLOW) { if (need_comma) fprintf(stderr, ","); fprintf(stderr, "LOW"); need_comma = 1; } if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT) { if (need_comma) fprintf(stderr, ","); fprintf(stderr, "EXT"); } fprintf(stderr, ")"); } fprintf(stderr, "\n"); } fprintf(stderr, "\n"); }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,461
jbig2_find_table(Jbig2Ctx *ctx, Jbig2Segment *segment, int index) { int i, table_index = 0; for (i = 0; i < segment->referred_to_segment_count; i++) { const Jbig2Segment *const rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[i]); if (rsegment && (rsegment->flags & 63) == 53) { if (table_index == index) return (const Jbig2HuffmanParams *)rsegment->result; ++table_index; } } return NULL; }
Overflow
0
jbig2_find_table(Jbig2Ctx *ctx, Jbig2Segment *segment, int index) { int i, table_index = 0; for (i = 0; i < segment->referred_to_segment_count; i++) { const Jbig2Segment *const rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[i]); if (rsegment && (rsegment->flags & 63) == 53) { if (table_index == index) return (const Jbig2HuffmanParams *)rsegment->result; ++table_index; } } return NULL; }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,462
jbig2_huffman_free(Jbig2Ctx *ctx, Jbig2HuffmanState *hs) { if (hs != NULL) jbig2_free(ctx->allocator, hs); return; }
Overflow
0
jbig2_huffman_free(Jbig2Ctx *ctx, Jbig2HuffmanState *hs) { if (hs != NULL) jbig2_free(ctx->allocator, hs); return; }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,463
jbig2_huffman_get(Jbig2HuffmanState *hs, const Jbig2HuffmanTable *table, bool *oob) { Jbig2HuffmanEntry *entry; byte flags; int offset_bits = hs->offset_bits; uint32_t this_word = hs->this_word; uint32_t next_word; int RANGELEN; int32_t result; if (hs->offset_limit && hs->offset >= hs->offset_limit) { jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, -1, "end of Jbig2WordStream reached at offset %d", hs->offset); if (oob) *oob = -1; return -1; } for (;;) { int log_table_size = table->log_table_size; int PREFLEN; /* SumatraPDF: shifting by the size of the operand is undefined */ entry = &table->entries[log_table_size > 0 ? this_word >> (32 - log_table_size) : 0]; flags = entry->flags; PREFLEN = entry->PREFLEN; if ((flags == (byte) - 1) && (PREFLEN == (byte) - 1) && (entry->u.RANGELOW == -1)) { if (oob) *oob = -1; return -1; } next_word = hs->next_word; offset_bits += PREFLEN; if (offset_bits >= 32) { this_word = next_word; hs->offset += 4; next_word = huff_get_next_word(hs, hs->offset + 4); offset_bits -= 32; hs->next_word = next_word; PREFLEN = offset_bits; } if (PREFLEN) this_word = (this_word << PREFLEN) | (next_word >> (32 - offset_bits)); if (flags & JBIG2_HUFFMAN_FLAGS_ISEXT) { table = entry->u.ext_table; } else break; } result = entry->u.RANGELOW; RANGELEN = entry->RANGELEN; if (RANGELEN > 0) { int32_t HTOFFSET; HTOFFSET = this_word >> (32 - RANGELEN); if (flags & JBIG2_HUFFMAN_FLAGS_ISLOW) result -= HTOFFSET; else result += HTOFFSET; offset_bits += RANGELEN; if (offset_bits >= 32) { this_word = next_word; hs->offset += 4; next_word = huff_get_next_word(hs, hs->offset + 4); offset_bits -= 32; hs->next_word = next_word; RANGELEN = offset_bits; } if (RANGELEN) this_word = (this_word << RANGELEN) | (next_word >> (32 - offset_bits)); } hs->this_word = this_word; hs->offset_bits = offset_bits; if (oob != NULL) *oob = (flags & JBIG2_HUFFMAN_FLAGS_ISOOB); return result; }
Overflow
0
jbig2_huffman_get(Jbig2HuffmanState *hs, const Jbig2HuffmanTable *table, bool *oob) { Jbig2HuffmanEntry *entry; byte flags; int offset_bits = hs->offset_bits; uint32_t this_word = hs->this_word; uint32_t next_word; int RANGELEN; int32_t result; if (hs->offset_limit && hs->offset >= hs->offset_limit) { jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, -1, "end of Jbig2WordStream reached at offset %d", hs->offset); if (oob) *oob = -1; return -1; } for (;;) { int log_table_size = table->log_table_size; int PREFLEN; /* SumatraPDF: shifting by the size of the operand is undefined */ entry = &table->entries[log_table_size > 0 ? this_word >> (32 - log_table_size) : 0]; flags = entry->flags; PREFLEN = entry->PREFLEN; if ((flags == (byte) - 1) && (PREFLEN == (byte) - 1) && (entry->u.RANGELOW == -1)) { if (oob) *oob = -1; return -1; } next_word = hs->next_word; offset_bits += PREFLEN; if (offset_bits >= 32) { this_word = next_word; hs->offset += 4; next_word = huff_get_next_word(hs, hs->offset + 4); offset_bits -= 32; hs->next_word = next_word; PREFLEN = offset_bits; } if (PREFLEN) this_word = (this_word << PREFLEN) | (next_word >> (32 - offset_bits)); if (flags & JBIG2_HUFFMAN_FLAGS_ISEXT) { table = entry->u.ext_table; } else break; } result = entry->u.RANGELOW; RANGELEN = entry->RANGELEN; if (RANGELEN > 0) { int32_t HTOFFSET; HTOFFSET = this_word >> (32 - RANGELEN); if (flags & JBIG2_HUFFMAN_FLAGS_ISLOW) result -= HTOFFSET; else result += HTOFFSET; offset_bits += RANGELEN; if (offset_bits >= 32) { this_word = next_word; hs->offset += 4; next_word = huff_get_next_word(hs, hs->offset + 4); offset_bits -= 32; hs->next_word = next_word; RANGELEN = offset_bits; } if (RANGELEN) this_word = (this_word << RANGELEN) | (next_word >> (32 - offset_bits)); } hs->this_word = this_word; hs->offset_bits = offset_bits; if (oob != NULL) *oob = (flags & JBIG2_HUFFMAN_FLAGS_ISOOB); return result; }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,464
jbig2_huffman_new(Jbig2Ctx *ctx, Jbig2WordStream *ws) { Jbig2HuffmanState *result = NULL; result = jbig2_new(ctx, Jbig2HuffmanState, 1); if (result != NULL) { result->offset = 0; result->offset_bits = 0; result->offset_limit = 0; result->ws = ws; result->ctx = ctx; result->this_word = huff_get_next_word(result, 0); result->next_word = huff_get_next_word(result, 4); } else { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate new huffman coding state"); } return result; }
Overflow
0
jbig2_huffman_new(Jbig2Ctx *ctx, Jbig2WordStream *ws) { Jbig2HuffmanState *result = NULL; result = jbig2_new(ctx, Jbig2HuffmanState, 1); if (result != NULL) { result->offset = 0; result->offset_bits = 0; result->offset_limit = 0; result->ws = ws; result->ctx = ctx; result->this_word = huff_get_next_word(result, 0); result->next_word = huff_get_next_word(result, 4); } else { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate new huffman coding state"); } return result; }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,465
jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3); }
Overflow
0
jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3); }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,466
jbig2_release_huffman_table(Jbig2Ctx *ctx, Jbig2HuffmanTable *table) { if (table != NULL) { jbig2_free(ctx->allocator, table->entries); jbig2_free(ctx->allocator, table); } return; }
Overflow
0
jbig2_release_huffman_table(Jbig2Ctx *ctx, Jbig2HuffmanTable *table) { if (table != NULL) { jbig2_free(ctx->allocator, table->entries); jbig2_free(ctx->allocator, table); } return; }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,467
jbig2_table(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data) { Jbig2HuffmanParams *params = NULL; Jbig2HuffmanLine *line = NULL; segment->result = NULL; if (segment->data_length < 10) goto too_short; { /* B.2 1) (B.2.1) Code table flags */ const int code_table_flags = segment_data[0]; const int HTOOB = code_table_flags & 0x01; /* Bit 0: HTOOB */ /* Bits 1-3: Number of bits used in code table line prefix size fields */ const int HTPS = (code_table_flags >> 1 & 0x07) + 1; /* Bits 4-6: Number of bits used in code table line range size fields */ const int HTRS = (code_table_flags >> 4 & 0x07) + 1; /* B.2 2) (B.2.2) The lower bound of the first table line in the encoded table */ const int32_t HTLOW = jbig2_get_int32(segment_data + 1); /* B.2 3) (B.2.3) One larger than the upeer bound of the last normal table line in the encoded table */ const int32_t HTHIGH = jbig2_get_int32(segment_data + 5); /* estimated number of lines int this table, used for alloacting memory for lines */ const size_t lines_max = (segment->data_length * 8 - HTPS * (HTOOB ? 3 : 2)) / (HTPS + HTRS) + (HTOOB ? 3 : 2); /* points to a first table line data */ const byte *lines_data = segment_data + 9; const size_t lines_data_bitlen = (segment->data_length - 9) * 8; /* length in bit */ /* bit offset: controls bit reading */ size_t boffset = 0; /* B.2 4) */ int32_t CURRANGELOW = HTLOW; size_t NTEMP = 0; #ifdef JBIG2_DEBUG jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "DECODING USER TABLE... Flags: %d, HTOOB: %d, HTPS: %d, HTRS: %d, HTLOW: %d, HTHIGH: %d", code_table_flags, HTOOB, HTPS, HTRS, HTLOW, HTHIGH); #endif /* allocate HuffmanParams & HuffmanLine */ params = jbig2_new(ctx, Jbig2HuffmanParams, 1); if (params == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not allocate Huffman Table Parameter"); goto error_exit; } line = jbig2_new(ctx, Jbig2HuffmanLine, lines_max); if (line == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not allocate Huffman Table Lines"); goto error_exit; } /* B.2 5) */ while (CURRANGELOW < HTHIGH) { /* B.2 5) a) */ if (boffset + HTPS >= lines_data_bitlen) goto too_short; line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS); /* B.2 5) b) */ if (boffset + HTRS >= lines_data_bitlen) goto too_short; line[NTEMP].RANGELEN = jbig2_table_read_bits(lines_data, &boffset, HTRS); /* B.2 5) c) */ line[NTEMP].RANGELOW = CURRANGELOW; CURRANGELOW += (1 << line[NTEMP].RANGELEN); NTEMP++; } /* B.2 6), B.2 7) lower range table line */ if (boffset + HTPS >= lines_data_bitlen) goto too_short; line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS); line[NTEMP].RANGELEN = 32; line[NTEMP].RANGELOW = HTLOW - 1; NTEMP++; /* B.2 8), B.2 9) upper range table line */ if (boffset + HTPS >= lines_data_bitlen) goto too_short; line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS); line[NTEMP].RANGELEN = 32; line[NTEMP].RANGELOW = HTHIGH; NTEMP++; /* B.2 10) */ if (HTOOB) { /* B.2 10) a), B.2 10) b) out-of-bound table line */ if (boffset + HTPS >= lines_data_bitlen) goto too_short; line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS); line[NTEMP].RANGELEN = 0; line[NTEMP].RANGELOW = 0; NTEMP++; } if (NTEMP != lines_max) { Jbig2HuffmanLine *new_line = jbig2_renew(ctx, line, Jbig2HuffmanLine, NTEMP); if (new_line == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not reallocate Huffman Table Lines"); goto error_exit; } line = new_line; } params->HTOOB = HTOOB; params->n_lines = NTEMP; params->lines = line; segment->result = params; #ifdef JBIG2_DEBUG { int i; for (i = 0; i < NTEMP; i++) { jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "Line: %d, PREFLEN: %d, RANGELEN: %d, RANGELOW: %d", i, params->lines[i].PREFLEN, params->lines[i].RANGELEN, params->lines[i].RANGELOW); } } #endif } return 0; too_short: jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short"); error_exit: if (line != NULL) { jbig2_free(ctx->allocator, line); } if (params != NULL) { jbig2_free(ctx->allocator, params); } return -1; }
Overflow
0
jbig2_table(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data) { Jbig2HuffmanParams *params = NULL; Jbig2HuffmanLine *line = NULL; segment->result = NULL; if (segment->data_length < 10) goto too_short; { /* B.2 1) (B.2.1) Code table flags */ const int code_table_flags = segment_data[0]; const int HTOOB = code_table_flags & 0x01; /* Bit 0: HTOOB */ /* Bits 1-3: Number of bits used in code table line prefix size fields */ const int HTPS = (code_table_flags >> 1 & 0x07) + 1; /* Bits 4-6: Number of bits used in code table line range size fields */ const int HTRS = (code_table_flags >> 4 & 0x07) + 1; /* B.2 2) (B.2.2) The lower bound of the first table line in the encoded table */ const int32_t HTLOW = jbig2_get_int32(segment_data + 1); /* B.2 3) (B.2.3) One larger than the upeer bound of the last normal table line in the encoded table */ const int32_t HTHIGH = jbig2_get_int32(segment_data + 5); /* estimated number of lines int this table, used for alloacting memory for lines */ const size_t lines_max = (segment->data_length * 8 - HTPS * (HTOOB ? 3 : 2)) / (HTPS + HTRS) + (HTOOB ? 3 : 2); /* points to a first table line data */ const byte *lines_data = segment_data + 9; const size_t lines_data_bitlen = (segment->data_length - 9) * 8; /* length in bit */ /* bit offset: controls bit reading */ size_t boffset = 0; /* B.2 4) */ int32_t CURRANGELOW = HTLOW; size_t NTEMP = 0; #ifdef JBIG2_DEBUG jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "DECODING USER TABLE... Flags: %d, HTOOB: %d, HTPS: %d, HTRS: %d, HTLOW: %d, HTHIGH: %d", code_table_flags, HTOOB, HTPS, HTRS, HTLOW, HTHIGH); #endif /* allocate HuffmanParams & HuffmanLine */ params = jbig2_new(ctx, Jbig2HuffmanParams, 1); if (params == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not allocate Huffman Table Parameter"); goto error_exit; } line = jbig2_new(ctx, Jbig2HuffmanLine, lines_max); if (line == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not allocate Huffman Table Lines"); goto error_exit; } /* B.2 5) */ while (CURRANGELOW < HTHIGH) { /* B.2 5) a) */ if (boffset + HTPS >= lines_data_bitlen) goto too_short; line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS); /* B.2 5) b) */ if (boffset + HTRS >= lines_data_bitlen) goto too_short; line[NTEMP].RANGELEN = jbig2_table_read_bits(lines_data, &boffset, HTRS); /* B.2 5) c) */ line[NTEMP].RANGELOW = CURRANGELOW; CURRANGELOW += (1 << line[NTEMP].RANGELEN); NTEMP++; } /* B.2 6), B.2 7) lower range table line */ if (boffset + HTPS >= lines_data_bitlen) goto too_short; line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS); line[NTEMP].RANGELEN = 32; line[NTEMP].RANGELOW = HTLOW - 1; NTEMP++; /* B.2 8), B.2 9) upper range table line */ if (boffset + HTPS >= lines_data_bitlen) goto too_short; line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS); line[NTEMP].RANGELEN = 32; line[NTEMP].RANGELOW = HTHIGH; NTEMP++; /* B.2 10) */ if (HTOOB) { /* B.2 10) a), B.2 10) b) out-of-bound table line */ if (boffset + HTPS >= lines_data_bitlen) goto too_short; line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS); line[NTEMP].RANGELEN = 0; line[NTEMP].RANGELOW = 0; NTEMP++; } if (NTEMP != lines_max) { Jbig2HuffmanLine *new_line = jbig2_renew(ctx, line, Jbig2HuffmanLine, NTEMP); if (new_line == NULL) { jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not reallocate Huffman Table Lines"); goto error_exit; } line = new_line; } params->HTOOB = HTOOB; params->n_lines = NTEMP; params->lines = line; segment->result = params; #ifdef JBIG2_DEBUG { int i; for (i = 0; i < NTEMP; i++) { jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "Line: %d, PREFLEN: %d, RANGELEN: %d, RANGELOW: %d", i, params->lines[i].PREFLEN, params->lines[i].RANGELEN, params->lines[i].RANGELOW); } } #endif } return 0; too_short: jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short"); error_exit: if (line != NULL) { jbig2_free(ctx->allocator, line); } if (params != NULL) { jbig2_free(ctx->allocator, params); } return -1; }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,468
jbig2_table_free(Jbig2Ctx *ctx, Jbig2HuffmanParams *params) { if (params != NULL) { if (params->lines != NULL) jbig2_free(ctx->allocator, (void *)params->lines); jbig2_free(ctx->allocator, params); } }
Overflow
0
jbig2_table_free(Jbig2Ctx *ctx, Jbig2HuffmanParams *params) { if (params != NULL) { if (params->lines != NULL) jbig2_free(ctx->allocator, (void *)params->lines); jbig2_free(ctx->allocator, params); } }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,469
main(int argc, char **argv) { Jbig2Ctx *ctx; Jbig2HuffmanTable *tables[5]; Jbig2HuffmanState *hs; Jbig2WordStream ws; bool oob; int32_t code; ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL); tables[0] = NULL; tables[1] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A); tables[2] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_B); tables[3] = NULL; tables[4] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_D); ws.get_next_word = test_get_word; hs = jbig2_huffman_new(ctx, &ws); printf("testing jbig2 huffmann decoding..."); printf("\t(should be 8 5 (oob) 8)\n"); { int i; int sequence_length = sizeof(test_tabindex); for (i = 0; i < sequence_length; i++) { code = jbig2_huffman_get(hs, tables[test_tabindex[i]], &oob); if (oob) printf("(oob) "); else printf("%d ", code); } } printf("\n"); jbig2_ctx_free(ctx); return 0; }
Overflow
0
main(int argc, char **argv) { Jbig2Ctx *ctx; Jbig2HuffmanTable *tables[5]; Jbig2HuffmanState *hs; Jbig2WordStream ws; bool oob; int32_t code; ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL); tables[0] = NULL; tables[1] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A); tables[2] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_B); tables[3] = NULL; tables[4] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_D); ws.get_next_word = test_get_word; hs = jbig2_huffman_new(ctx, &ws); printf("testing jbig2 huffmann decoding..."); printf("\t(should be 8 5 (oob) 8)\n"); { int i; int sequence_length = sizeof(test_tabindex); for (i = 0; i < sequence_length; i++) { code = jbig2_huffman_get(hs, tables[test_tabindex[i]], &oob); if (oob) printf("(oob) "); else printf("%d ", code); } } printf("\n"); jbig2_ctx_free(ctx); return 0; }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,470
main(int argc, char **argv) { Jbig2Ctx *ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL); int i; for (i = 0; i < countof(tests); i++) { Jbig2HuffmanTable *table; Jbig2HuffmanState *hs; test_stream_t st; int32_t code; bool oob; int j; st.ws.get_next_word = test_get_word; st.h = &tests[i]; printf("testing Standard Huffman table %s: ", st.h->name); table = jbig2_build_huffman_table(ctx, st.h->params); if (table == NULL) { printf("jbig2_build_huffman_table() returned NULL!\n"); } else { /* jbig2_dump_huffman_table(table); */ hs = jbig2_huffman_new(ctx, &st.ws); if (hs == NULL) { printf("jbig2_huffman_new() returned NULL!\n"); } else { for (j = 0; j < st.h->output_len; j++) { printf("%d...", st.h->output[j]); code = jbig2_huffman_get(hs, table, &oob); if (code == st.h->output[j] && !oob) { printf("ok, "); } else { int need_comma = 0; printf("NG("); if (code != st.h->output[j]) { printf("%d", code); need_comma = 1; } if (oob) { if (need_comma) printf(","); printf("OOB"); } printf("), "); } } if (st.h->params->HTOOB) { printf("OOB..."); code = jbig2_huffman_get(hs, table, &oob); if (oob) { printf("ok"); } else { printf("NG(%d)", code); } } printf("\n"); jbig2_huffman_free(ctx, hs); } jbig2_release_huffman_table(ctx, table); } } jbig2_ctx_free(ctx); return 0; }
Overflow
0
main(int argc, char **argv) { Jbig2Ctx *ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL); int i; for (i = 0; i < countof(tests); i++) { Jbig2HuffmanTable *table; Jbig2HuffmanState *hs; test_stream_t st; int32_t code; bool oob; int j; st.ws.get_next_word = test_get_word; st.h = &tests[i]; printf("testing Standard Huffman table %s: ", st.h->name); table = jbig2_build_huffman_table(ctx, st.h->params); if (table == NULL) { printf("jbig2_build_huffman_table() returned NULL!\n"); } else { /* jbig2_dump_huffman_table(table); */ hs = jbig2_huffman_new(ctx, &st.ws); if (hs == NULL) { printf("jbig2_huffman_new() returned NULL!\n"); } else { for (j = 0; j < st.h->output_len; j++) { printf("%d...", st.h->output[j]); code = jbig2_huffman_get(hs, table, &oob); if (code == st.h->output[j] && !oob) { printf("ok, "); } else { int need_comma = 0; printf("NG("); if (code != st.h->output[j]) { printf("%d", code); need_comma = 1; } if (oob) { if (need_comma) printf(","); printf("OOB"); } printf("), "); } } if (st.h->params->HTOOB) { printf("OOB..."); code = jbig2_huffman_get(hs, table, &oob); if (oob) { printf("ok"); } else { printf("NG(%d)", code); } } printf("\n"); jbig2_huffman_free(ctx, hs); } jbig2_release_huffman_table(ctx, table); } } jbig2_ctx_free(ctx); return 0; }
@@ -47,16 +47,16 @@ struct _Jbig2HuffmanState { is (offset + 4) * 8. */ uint32_t this_word; uint32_t next_word; - int offset_bits; - int offset; - int offset_limit; + uint32_t offset_bits; + uint32_t offset; + uint32_t offset_limit; Jbig2WordStream *ws; Jbig2Ctx *ctx; }; static uint32_t -huff_get_next_word(Jbig2HuffmanState *hs, int offset) +huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset) { uint32_t word = 0; Jbig2WordStream *ws = hs->ws; @@ -213,7 +213,7 @@ jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset) /* return the offset of the huffman decode pointer (in bytes) * from the beginning of the WordStream */ -int +uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs) { return hs->offset + (hs->offset_bits >> 3);
CWE-119
null
null
14,471
jbig2_image_clear(Jbig2Ctx *ctx, Jbig2Image *image, int value) { const uint8_t fill = value ? 0xFF : 0x00; memset(image->data, fill, image->stride * image->height); }
Overflow
0
jbig2_image_clear(Jbig2Ctx *ctx, Jbig2Image *image, int value) { const uint8_t fill = value ? 0xFF : 0x00; memset(image->data, fill, image->stride * image->height); }
@@ -32,10 +32,10 @@ /* allocate a Jbig2Image structure and its associated bitmap */ Jbig2Image * -jbig2_image_new(Jbig2Ctx *ctx, int width, int height) +jbig2_image_new(Jbig2Ctx *ctx, uint32_t width, uint32_t height) { Jbig2Image *image; - int stride; + uint32_t stride; int64_t check; image = jbig2_new(ctx, Jbig2Image, 1); @@ -99,7 +99,7 @@ jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image) /* resize a Jbig2Image */ Jbig2Image * -jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, int width, int height) +jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, uint32_t width, uint32_t height) { if (width == image->width) { /* check for integer multiplication overflow */ @@ -133,11 +133,11 @@ jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, int width, int height) static int jbig2_image_compose_unopt(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op) { - int i, j; - int sw = src->width; - int sh = src->height; - int sx = 0; - int sy = 0; + uint32_t i, j; + uint32_t sw = src->width; + uint32_t sh = src->height; + uint32_t sx = 0; + uint32_t sy = 0; /* clip to the dst image boundaries */ if (x < 0) { @@ -200,10 +200,10 @@ jbig2_image_compose_unopt(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x int jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op) { - int i, j; - int w, h; - int leftbyte, rightbyte; - int shift; + uint32_t i, j; + uint32_t w, h; + uint32_t leftbyte, rightbyte; + uint32_t shift; uint8_t *s, *ss; uint8_t *d, *dd; uint8_t mask, rightmask; @@ -226,8 +226,8 @@ jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int h += y; y = 0; } - w = (x + w < dst->width) ? w : dst->width - x; - h = (y + h < dst->height) ? h : dst->height - y; + w = ((uint32_t)x + w < dst->width) ? w : ((dst->width >= (uint32_t)x) ? dst->width - (uint32_t)x : 0); + h = ((uint32_t)y + h < dst->height) ? h : ((dst->height >= (uint32_t)y) ? dst->height - (uint32_t)y : 0); #ifdef JBIG2_DEBUG jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "compositing %dx%d at (%d, %d) after clipping\n", w, h, x, y); #endif @@ -249,8 +249,8 @@ jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int } #endif - leftbyte = x >> 3; - rightbyte = (x + w - 1) >> 3; + leftbyte = (uint32_t)x >> 3; + rightbyte = ((uint32_t)x + w - 1) >> 3; shift = x & 7; /* general OR case */
CWE-119
null
null
14,472
jbig2_image_clone(Jbig2Ctx *ctx, Jbig2Image *image) { if (image) image->refcount++; return image; }
Overflow
0
jbig2_image_clone(Jbig2Ctx *ctx, Jbig2Image *image) { if (image) image->refcount++; return image; }
@@ -32,10 +32,10 @@ /* allocate a Jbig2Image structure and its associated bitmap */ Jbig2Image * -jbig2_image_new(Jbig2Ctx *ctx, int width, int height) +jbig2_image_new(Jbig2Ctx *ctx, uint32_t width, uint32_t height) { Jbig2Image *image; - int stride; + uint32_t stride; int64_t check; image = jbig2_new(ctx, Jbig2Image, 1); @@ -99,7 +99,7 @@ jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image) /* resize a Jbig2Image */ Jbig2Image * -jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, int width, int height) +jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, uint32_t width, uint32_t height) { if (width == image->width) { /* check for integer multiplication overflow */ @@ -133,11 +133,11 @@ jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, int width, int height) static int jbig2_image_compose_unopt(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op) { - int i, j; - int sw = src->width; - int sh = src->height; - int sx = 0; - int sy = 0; + uint32_t i, j; + uint32_t sw = src->width; + uint32_t sh = src->height; + uint32_t sx = 0; + uint32_t sy = 0; /* clip to the dst image boundaries */ if (x < 0) { @@ -200,10 +200,10 @@ jbig2_image_compose_unopt(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x int jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op) { - int i, j; - int w, h; - int leftbyte, rightbyte; - int shift; + uint32_t i, j; + uint32_t w, h; + uint32_t leftbyte, rightbyte; + uint32_t shift; uint8_t *s, *ss; uint8_t *d, *dd; uint8_t mask, rightmask; @@ -226,8 +226,8 @@ jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int h += y; y = 0; } - w = (x + w < dst->width) ? w : dst->width - x; - h = (y + h < dst->height) ? h : dst->height - y; + w = ((uint32_t)x + w < dst->width) ? w : ((dst->width >= (uint32_t)x) ? dst->width - (uint32_t)x : 0); + h = ((uint32_t)y + h < dst->height) ? h : ((dst->height >= (uint32_t)y) ? dst->height - (uint32_t)y : 0); #ifdef JBIG2_DEBUG jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "compositing %dx%d at (%d, %d) after clipping\n", w, h, x, y); #endif @@ -249,8 +249,8 @@ jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int } #endif - leftbyte = x >> 3; - rightbyte = (x + w - 1) >> 3; + leftbyte = (uint32_t)x >> 3; + rightbyte = ((uint32_t)x + w - 1) >> 3; shift = x & 7; /* general OR case */
CWE-119
null
null
14,473
jbig2_image_release(Jbig2Ctx *ctx, Jbig2Image *image) { if (image == NULL) return; image->refcount--; if (!image->refcount) jbig2_image_free(ctx, image); }
Overflow
0
jbig2_image_release(Jbig2Ctx *ctx, Jbig2Image *image) { if (image == NULL) return; image->refcount--; if (!image->refcount) jbig2_image_free(ctx, image); }
@@ -32,10 +32,10 @@ /* allocate a Jbig2Image structure and its associated bitmap */ Jbig2Image * -jbig2_image_new(Jbig2Ctx *ctx, int width, int height) +jbig2_image_new(Jbig2Ctx *ctx, uint32_t width, uint32_t height) { Jbig2Image *image; - int stride; + uint32_t stride; int64_t check; image = jbig2_new(ctx, Jbig2Image, 1); @@ -99,7 +99,7 @@ jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image) /* resize a Jbig2Image */ Jbig2Image * -jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, int width, int height) +jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, uint32_t width, uint32_t height) { if (width == image->width) { /* check for integer multiplication overflow */ @@ -133,11 +133,11 @@ jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, int width, int height) static int jbig2_image_compose_unopt(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op) { - int i, j; - int sw = src->width; - int sh = src->height; - int sx = 0; - int sy = 0; + uint32_t i, j; + uint32_t sw = src->width; + uint32_t sh = src->height; + uint32_t sx = 0; + uint32_t sy = 0; /* clip to the dst image boundaries */ if (x < 0) { @@ -200,10 +200,10 @@ jbig2_image_compose_unopt(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x int jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op) { - int i, j; - int w, h; - int leftbyte, rightbyte; - int shift; + uint32_t i, j; + uint32_t w, h; + uint32_t leftbyte, rightbyte; + uint32_t shift; uint8_t *s, *ss; uint8_t *d, *dd; uint8_t mask, rightmask; @@ -226,8 +226,8 @@ jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int h += y; y = 0; } - w = (x + w < dst->width) ? w : dst->width - x; - h = (y + h < dst->height) ? h : dst->height - y; + w = ((uint32_t)x + w < dst->width) ? w : ((dst->width >= (uint32_t)x) ? dst->width - (uint32_t)x : 0); + h = ((uint32_t)y + h < dst->height) ? h : ((dst->height >= (uint32_t)y) ? dst->height - (uint32_t)y : 0); #ifdef JBIG2_DEBUG jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "compositing %dx%d at (%d, %d) after clipping\n", w, h, x, y); #endif @@ -249,8 +249,8 @@ jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int } #endif - leftbyte = x >> 3; - rightbyte = (x + w - 1) >> 3; + leftbyte = (uint32_t)x >> 3; + rightbyte = ((uint32_t)x + w - 1) >> 3; shift = x & 7; /* general OR case */
CWE-119
null
null
14,474
jbig2_image_set_pixel(Jbig2Image *image, int x, int y, bool value) { const int w = image->width; const int h = image->height; int scratch, mask; int bit, byte; if ((x < 0) || (x >= w)) return 0; if ((y < 0) || (y >= h)) return 0; byte = (x >> 3) + y * image->stride; bit = 7 - (x & 7); mask = (1 << bit) ^ 0xff; scratch = image->data[byte] & mask; image->data[byte] = scratch | (value << bit); return 1; }
Overflow
0
jbig2_image_set_pixel(Jbig2Image *image, int x, int y, bool value) { const int w = image->width; const int h = image->height; int scratch, mask; int bit, byte; if ((x < 0) || (x >= w)) return 0; if ((y < 0) || (y >= h)) return 0; byte = (x >> 3) + y * image->stride; bit = 7 - (x & 7); mask = (1 << bit) ^ 0xff; scratch = image->data[byte] & mask; image->data[byte] = scratch | (value << bit); return 1; }
@@ -32,10 +32,10 @@ /* allocate a Jbig2Image structure and its associated bitmap */ Jbig2Image * -jbig2_image_new(Jbig2Ctx *ctx, int width, int height) +jbig2_image_new(Jbig2Ctx *ctx, uint32_t width, uint32_t height) { Jbig2Image *image; - int stride; + uint32_t stride; int64_t check; image = jbig2_new(ctx, Jbig2Image, 1); @@ -99,7 +99,7 @@ jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image) /* resize a Jbig2Image */ Jbig2Image * -jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, int width, int height) +jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, uint32_t width, uint32_t height) { if (width == image->width) { /* check for integer multiplication overflow */ @@ -133,11 +133,11 @@ jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, int width, int height) static int jbig2_image_compose_unopt(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op) { - int i, j; - int sw = src->width; - int sh = src->height; - int sx = 0; - int sy = 0; + uint32_t i, j; + uint32_t sw = src->width; + uint32_t sh = src->height; + uint32_t sx = 0; + uint32_t sy = 0; /* clip to the dst image boundaries */ if (x < 0) { @@ -200,10 +200,10 @@ jbig2_image_compose_unopt(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x int jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op) { - int i, j; - int w, h; - int leftbyte, rightbyte; - int shift; + uint32_t i, j; + uint32_t w, h; + uint32_t leftbyte, rightbyte; + uint32_t shift; uint8_t *s, *ss; uint8_t *d, *dd; uint8_t mask, rightmask; @@ -226,8 +226,8 @@ jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int h += y; y = 0; } - w = (x + w < dst->width) ? w : dst->width - x; - h = (y + h < dst->height) ? h : dst->height - y; + w = ((uint32_t)x + w < dst->width) ? w : ((dst->width >= (uint32_t)x) ? dst->width - (uint32_t)x : 0); + h = ((uint32_t)y + h < dst->height) ? h : ((dst->height >= (uint32_t)y) ? dst->height - (uint32_t)y : 0); #ifdef JBIG2_DEBUG jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "compositing %dx%d at (%d, %d) after clipping\n", w, h, x, y); #endif @@ -249,8 +249,8 @@ jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int } #endif - leftbyte = x >> 3; - rightbyte = (x + w - 1) >> 3; + leftbyte = (uint32_t)x >> 3; + rightbyte = ((uint32_t)x + w - 1) >> 3; shift = x & 7; /* general OR case */
CWE-119
null
null
14,475
jbig2_decode_mmr_consume(Jbig2MmrCtx *mmr, int n_bits) { mmr->word <<= n_bits; mmr->bit_index += n_bits; while (mmr->bit_index >= 8) { mmr->bit_index -= 8; if (mmr->data_index + 4 < mmr->size) mmr->word |= (mmr->data[mmr->data_index + 4] << mmr->bit_index); mmr->data_index++; } }
Overflow
0
jbig2_decode_mmr_consume(Jbig2MmrCtx *mmr, int n_bits) { mmr->word <<= n_bits; mmr->bit_index += n_bits; while (mmr->bit_index >= 8) { mmr->bit_index -= 8; if (mmr->data_index + 4 < mmr->size) mmr->word |= (mmr->data[mmr->data_index + 4] << mmr->bit_index); mmr->data_index++; } }
@@ -38,19 +38,21 @@ #include "jbig2_mmr.h" typedef struct { - int width; - int height; + uint32_t width; + uint32_t height; const byte *data; size_t size; - int data_index; - int bit_index; + uint32_t data_index; + uint32_t bit_index; uint32_t word; } Jbig2MmrCtx; +#define MINUS1 ((uint32_t)-1) + static void jbig2_decode_mmr_init(Jbig2MmrCtx *mmr, int width, int height, const byte *data, size_t size) { - int i; + size_t i; uint32_t word = 0; mmr->width = width; @@ -732,14 +734,14 @@ const mmr_table_node jbig2_mmr_black_decode[] = { #define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 ) static int -jbig2_find_changing_element(const byte *line, int x, int w) +jbig2_find_changing_element(const byte *line, uint32_t x, uint32_t w) { int a, b; if (line == 0) - return w; + return (int)w; - if (x == -1) { + if (x == MINUS1) { a = 0; x = 0; } else { @@ -758,7 +760,7 @@ jbig2_find_changing_element(const byte *line, int x, int w) } static int -jbig2_find_changing_element_of_color(const byte *line, int x, int w, int color) +jbig2_find_changing_element_of_color(const byte *line, uint32_t x, uint32_t w, int color) { if (line == 0) return w; @@ -772,9 +774,9 @@ static const byte lm[8] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 }; static const byte rm[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; static void -jbig2_set_bits(byte *line, int x0, int x1) +jbig2_set_bits(byte *line, uint32_t x0, uint32_t x1) { - int a0, a1, b0, b1, a; + uint32_t a0, a1, b0, b1, a; a0 = x0 >> 3; a1 = x1 >> 3; @@ -831,8 +833,8 @@ jbig2_decode_get_run(Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_ static int jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) { - int a0 = -1; - int a1, a2, b1, b2; + uint32_t a0 = MINUS1; + uint32_t a1, a2, b1, b2; int c = 0; /* 0 is white, black is 1 */ while (1) { @@ -840,7 +842,7 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) /* printf ("%08x\n", word); */ - if (a0 >= mmr->width) + if (a0 != MINUS1 && a0 >= mmr->width) break; if ((word >> (32 - 3)) == 1) { @@ -848,7 +850,7 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) jbig2_decode_mmr_consume(mmr, 3); - if (a0 == -1) + if (a0 == MINUS1) a0 = 0; if (c == 0) { @@ -860,7 +862,7 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) a1 = mmr->width; if (a2 > mmr->width) a2 = mmr->width; - if (a2 < a1 || a1 < 0) + if (a1 == MINUS1 || a2 < a1) return -1; jbig2_set_bits(dst, a1, a2); a0 = a2; @@ -874,7 +876,7 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) a1 = mmr->width; if (a2 > mmr->width) a2 = mmr->width; - if (a1 < a0 || a0 < 0) + if (a0 == MINUS1 || a1 < a0) return -1; jbig2_set_bits(dst, a0, a1); a0 = a2; @@ -888,7 +890,7 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); b2 = jbig2_find_changing_element(ref, b1, mmr->width); if (c) { - if (b2 < a0 || a0 < 0) + if (a0 == MINUS1 || b2 < a0) return -1; jbig2_set_bits(dst, a0, b2); } @@ -900,7 +902,7 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) jbig2_decode_mmr_consume(mmr, 1); b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); if (c) { - if (b1 < a0 || a0 < 0) + if (a0 == MINUS1 || b1 < a0) return -1; jbig2_set_bits(dst, a0, b1); } @@ -915,7 +917,7 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) if (b1 + 1 > mmr->width) break; if (c) { - if (b1 + 1 < a0 || a0 < 0) + if (a0 == MINUS1 || b1 + 1 < a0) return -1; jbig2_set_bits(dst, a0, b1 + 1); } @@ -930,7 +932,7 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) if (b1 + 2 > mmr->width) break; if (c) { - if (b1 + 2 < a0 || a0 < 0) + if (a0 == MINUS1 || b1 + 2 < a0) return -1; jbig2_set_bits(dst, a0, b1 + 2); } @@ -942,10 +944,10 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) /* printf ("VR(3)\n"); */ jbig2_decode_mmr_consume(mmr, 7); b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); - if (b1 + 3 > mmr->width) + if (b1 + 3 > (int)mmr->width) break; if (c) { - if (b1 + 3 < a0 || a0 < 0) + if (a0 == MINUS1 || b1 + 3 < a0) return -1; jbig2_set_bits(dst, a0, b1 + 3); } @@ -957,10 +959,10 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) /* printf ("VL(1)\n"); */ jbig2_decode_mmr_consume(mmr, 3); b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); - if (b1 - 1 < 0) + if (b1 < 1) break; if (c) { - if (b1 - 1 < a0 || a0 < 0) + if (a0 == MINUS1 || b1 - 1 < a0) return -1; jbig2_set_bits(dst, a0, b1 - 1); } @@ -972,7 +974,7 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) /* printf ("VL(2)\n"); */ jbig2_decode_mmr_consume(mmr, 6); b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); - if (b1 - 2 < 0) + if (b1 < 2) break; if (c) { if (b1 - 2 < a0 || a0 < 0) @@ -987,10 +989,10 @@ jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst) /* printf ("VL(3)\n"); */ jbig2_decode_mmr_consume(mmr, 7); b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); - if (b1 - 3 < 0) + if (b1 < 3) break; if (c) { - if (b1 - 3 < a0 || a0 < 0) + if (a0 == MINUS1 || b1 - 3 < a0) return -1; jbig2_set_bits(dst, a0, b1 - 3); } @@ -1009,10 +1011,10 @@ int jbig2_decode_generic_mmr(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image) { Jbig2MmrCtx mmr; - const int rowstride = image->stride; + const uint32_t rowstride = image->stride; byte *dst = image->data; byte *ref = NULL; - int y; + uint32_t y; int code = 0; jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size); @@ -1047,10 +1049,10 @@ int jbig2_decode_halftone_mmr(Jbig2Ctx *ctx, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image, size_t *consumed_bytes) { Jbig2MmrCtx mmr; - const int rowstride = image->stride; + const uint32_t rowstride = image->stride; byte *dst = image->data; byte *ref = NULL; - int y; + uint32_t y; int code = 0; const uint32_t EOFB = 0x001001;
CWE-119
null
null
14,476
dump_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, Jbig2Page *page) { if (page->x_resolution == 0) { jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (unknown res)", page->number, page->width, page->height); } else if (page->x_resolution == page->y_resolution) { jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (%d ppm)", page->number, page->width, page->height, page->x_resolution); } else { jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (%dx%d ppm)", page->number, page->width, page->height, page->x_resolution, page->y_resolution); } if (page->striped) { jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "\tmaximum stripe size: %d", page->stripe_size); } }
Overflow
0
dump_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, Jbig2Page *page) { if (page->x_resolution == 0) { jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (unknown res)", page->number, page->width, page->height); } else if (page->x_resolution == page->y_resolution) { jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (%d ppm)", page->number, page->width, page->height, page->x_resolution); } else { jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (%dx%d ppm)", page->number, page->width, page->height, page->x_resolution, page->y_resolution); } if (page->striped) { jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "\tmaximum stripe size: %d", page->stripe_size); } }
@@ -155,9 +155,9 @@ int jbig2_end_of_stripe(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data) { Jbig2Page page = ctx->pages[ctx->current_page]; - int end_row; + uint32_t end_row; - end_row = jbig2_get_int32(segment_data); + end_row = jbig2_get_uint32(segment_data); if (end_row < page.end_row) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "end of stripe segment with non-positive end row advance" " (new end row %d vs current end row %d)", end_row, page.end_row); @@ -248,7 +248,7 @@ jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *image, int x, /* grow the page to accomodate a new stripe if necessary */ if (page->striped) { - int new_height = y + image->height + page->end_row; + uint32_t new_height = y + image->height + page->end_row; if (page->image->height < new_height) { jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "growing page buffer to %d rows " "to accomodate new stripe", new_height);
CWE-119
null
null
14,477
jbig2_complete_page(Jbig2Ctx *ctx) { int code = 0; /* check for unfinished segments */ if (ctx->segment_index != ctx->n_segments) { Jbig2Segment *segment = ctx->segments[ctx->segment_index]; /* Some versions of Xerox Workcentre generate PDF files with the segment data length field of the last segment set to -1. Try to cope with this here. */ if ((segment->data_length & 0xffffffff) == 0xffffffff) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "File has an invalid segment data length!" " Trying to decode using the available data."); segment->data_length = ctx->buf_wr_ix - ctx->buf_rd_ix; code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix); ctx->buf_rd_ix += segment->data_length; ctx->segment_index++; } } /* ensure image exists before marking page as complete */ if (ctx->pages[ctx->current_page].image != NULL) { ctx->pages[ctx->current_page].state = JBIG2_PAGE_COMPLETE; } return code; }
Overflow
0
jbig2_complete_page(Jbig2Ctx *ctx) { int code = 0; /* check for unfinished segments */ if (ctx->segment_index != ctx->n_segments) { Jbig2Segment *segment = ctx->segments[ctx->segment_index]; /* Some versions of Xerox Workcentre generate PDF files with the segment data length field of the last segment set to -1. Try to cope with this here. */ if ((segment->data_length & 0xffffffff) == 0xffffffff) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "File has an invalid segment data length!" " Trying to decode using the available data."); segment->data_length = ctx->buf_wr_ix - ctx->buf_rd_ix; code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix); ctx->buf_rd_ix += segment->data_length; ctx->segment_index++; } } /* ensure image exists before marking page as complete */ if (ctx->pages[ctx->current_page].image != NULL) { ctx->pages[ctx->current_page].state = JBIG2_PAGE_COMPLETE; } return code; }
@@ -155,9 +155,9 @@ int jbig2_end_of_stripe(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data) { Jbig2Page page = ctx->pages[ctx->current_page]; - int end_row; + uint32_t end_row; - end_row = jbig2_get_int32(segment_data); + end_row = jbig2_get_uint32(segment_data); if (end_row < page.end_row) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "end of stripe segment with non-positive end row advance" " (new end row %d vs current end row %d)", end_row, page.end_row); @@ -248,7 +248,7 @@ jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *image, int x, /* grow the page to accomodate a new stripe if necessary */ if (page->striped) { - int new_height = y + image->height + page->end_row; + uint32_t new_height = y + image->height + page->end_row; if (page->image->height < new_height) { jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "growing page buffer to %d rows " "to accomodate new stripe", new_height);
CWE-119
null
null
14,478
jbig2_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data) { Jbig2Page *page; /* a new page info segment implies the previous page is finished */ page = &(ctx->pages[ctx->current_page]); if ((page->number != 0) && ((page->state == JBIG2_PAGE_NEW) || (page->state == JBIG2_PAGE_FREE))) { page->state = JBIG2_PAGE_COMPLETE; jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unexpected page info segment, marking previous page finished"); } /* find a free page */ { int index, j; index = ctx->current_page; while (ctx->pages[index].state != JBIG2_PAGE_FREE) { index++; if (index >= ctx->max_page_index) { /* grow the list */ ctx->pages = jbig2_renew(ctx, ctx->pages, Jbig2Page, (ctx->max_page_index <<= 2)); for (j = index; j < ctx->max_page_index; j++) { ctx->pages[j].state = JBIG2_PAGE_FREE; ctx->pages[j].number = 0; ctx->pages[j].image = NULL; } } } page = &(ctx->pages[index]); ctx->current_page = index; page->state = JBIG2_PAGE_NEW; page->number = segment->page_association; } /* FIXME: would be nice if we tried to work around this */ if (segment->data_length < 19) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short"); } /* 7.4.8.x */ page->width = jbig2_get_uint32(segment_data); page->height = jbig2_get_uint32(segment_data + 4); page->x_resolution = jbig2_get_uint32(segment_data + 8); page->y_resolution = jbig2_get_uint32(segment_data + 12); page->flags = segment_data[16]; /* 7.4.8.6 */ { int16_t striping = jbig2_get_int16(segment_data + 17); if (striping & 0x8000) { page->striped = TRUE; page->stripe_size = striping & 0x7FFF; } else { page->striped = FALSE; page->stripe_size = 0; /* would page->height be better? */ } } if (page->height == 0xFFFFFFFF && page->striped == FALSE) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "height is unspecified but page is not markes as striped"); page->striped = TRUE; } page->end_row = 0; if (segment->data_length > 19) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extra data in segment"); } dump_page_info(ctx, segment, page); /* allocate an approprate page image buffer */ /* 7.4.8.2 */ if (page->height == 0xFFFFFFFF) { page->image = jbig2_image_new(ctx, page->width, page->stripe_size); } else { page->image = jbig2_image_new(ctx, page->width, page->height); } if (page->image == NULL) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate buffer for page image"); } else { /* 8.2 (3) fill the page with the default pixel value */ jbig2_image_clear(ctx, page->image, (page->flags & 4)); jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %dx%d page image (%d bytes)", page->image->width, page->image->height, page->image->stride * page->image->height); } return 0; }
Overflow
0
jbig2_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data) { Jbig2Page *page; /* a new page info segment implies the previous page is finished */ page = &(ctx->pages[ctx->current_page]); if ((page->number != 0) && ((page->state == JBIG2_PAGE_NEW) || (page->state == JBIG2_PAGE_FREE))) { page->state = JBIG2_PAGE_COMPLETE; jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unexpected page info segment, marking previous page finished"); } /* find a free page */ { int index, j; index = ctx->current_page; while (ctx->pages[index].state != JBIG2_PAGE_FREE) { index++; if (index >= ctx->max_page_index) { /* grow the list */ ctx->pages = jbig2_renew(ctx, ctx->pages, Jbig2Page, (ctx->max_page_index <<= 2)); for (j = index; j < ctx->max_page_index; j++) { ctx->pages[j].state = JBIG2_PAGE_FREE; ctx->pages[j].number = 0; ctx->pages[j].image = NULL; } } } page = &(ctx->pages[index]); ctx->current_page = index; page->state = JBIG2_PAGE_NEW; page->number = segment->page_association; } /* FIXME: would be nice if we tried to work around this */ if (segment->data_length < 19) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short"); } /* 7.4.8.x */ page->width = jbig2_get_uint32(segment_data); page->height = jbig2_get_uint32(segment_data + 4); page->x_resolution = jbig2_get_uint32(segment_data + 8); page->y_resolution = jbig2_get_uint32(segment_data + 12); page->flags = segment_data[16]; /* 7.4.8.6 */ { int16_t striping = jbig2_get_int16(segment_data + 17); if (striping & 0x8000) { page->striped = TRUE; page->stripe_size = striping & 0x7FFF; } else { page->striped = FALSE; page->stripe_size = 0; /* would page->height be better? */ } } if (page->height == 0xFFFFFFFF && page->striped == FALSE) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "height is unspecified but page is not markes as striped"); page->striped = TRUE; } page->end_row = 0; if (segment->data_length > 19) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extra data in segment"); } dump_page_info(ctx, segment, page); /* allocate an approprate page image buffer */ /* 7.4.8.2 */ if (page->height == 0xFFFFFFFF) { page->image = jbig2_image_new(ctx, page->width, page->stripe_size); } else { page->image = jbig2_image_new(ctx, page->width, page->height); } if (page->image == NULL) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate buffer for page image"); } else { /* 8.2 (3) fill the page with the default pixel value */ jbig2_image_clear(ctx, page->image, (page->flags & 4)); jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %dx%d page image (%d bytes)", page->image->width, page->image->height, page->image->stride * page->image->height); } return 0; }
@@ -155,9 +155,9 @@ int jbig2_end_of_stripe(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data) { Jbig2Page page = ctx->pages[ctx->current_page]; - int end_row; + uint32_t end_row; - end_row = jbig2_get_int32(segment_data); + end_row = jbig2_get_uint32(segment_data); if (end_row < page.end_row) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "end of stripe segment with non-positive end row advance" " (new end row %d vs current end row %d)", end_row, page.end_row); @@ -248,7 +248,7 @@ jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *image, int x, /* grow the page to accomodate a new stripe if necessary */ if (page->striped) { - int new_height = y + image->height + page->end_row; + uint32_t new_height = y + image->height + page->end_row; if (page->image->height < new_height) { jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "growing page buffer to %d rows " "to accomodate new stripe", new_height);
CWE-119
null
null
14,479
jbig2_page_out(Jbig2Ctx *ctx) { int index; /* search for a completed page */ for (index = 0; index < ctx->max_page_index; index++) { if (ctx->pages[index].state == JBIG2_PAGE_COMPLETE) { Jbig2Image *img = ctx->pages[index].image; uint32_t page_number = ctx->pages[index].number; ctx->pages[index].state = JBIG2_PAGE_RETURNED; if (img != NULL) { jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "page %d returned to the client", page_number); return jbig2_image_clone(ctx, img); } else { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "page %d returned with no associated image", page_number); ; /* continue */ } } } /* no pages available */ return NULL; }
Overflow
0
jbig2_page_out(Jbig2Ctx *ctx) { int index; /* search for a completed page */ for (index = 0; index < ctx->max_page_index; index++) { if (ctx->pages[index].state == JBIG2_PAGE_COMPLETE) { Jbig2Image *img = ctx->pages[index].image; uint32_t page_number = ctx->pages[index].number; ctx->pages[index].state = JBIG2_PAGE_RETURNED; if (img != NULL) { jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "page %d returned to the client", page_number); return jbig2_image_clone(ctx, img); } else { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "page %d returned with no associated image", page_number); ; /* continue */ } } } /* no pages available */ return NULL; }
@@ -155,9 +155,9 @@ int jbig2_end_of_stripe(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data) { Jbig2Page page = ctx->pages[ctx->current_page]; - int end_row; + uint32_t end_row; - end_row = jbig2_get_int32(segment_data); + end_row = jbig2_get_uint32(segment_data); if (end_row < page.end_row) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "end of stripe segment with non-positive end row advance" " (new end row %d vs current end row %d)", end_row, page.end_row); @@ -248,7 +248,7 @@ jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *image, int x, /* grow the page to accomodate a new stripe if necessary */ if (page->striped) { - int new_height = y + image->height + page->end_row; + uint32_t new_height = y + image->height + page->end_row; if (page->image->height < new_height) { jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "growing page buffer to %d rows " "to accomodate new stripe", new_height);
CWE-119
null
null
14,480
jbig2_release_page(Jbig2Ctx *ctx, Jbig2Image *image) { int index; /* find the matching page struct and mark it released */ for (index = 0; index < ctx->max_page_index; index++) { if (ctx->pages[index].image == image) { jbig2_image_release(ctx, image); ctx->pages[index].state = JBIG2_PAGE_RELEASED; jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "page %d released by the client", ctx->pages[index].number); return 0; } } /* no matching pages */ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "jbig2_release_page called on unknown page"); return 1; }
Overflow
0
jbig2_release_page(Jbig2Ctx *ctx, Jbig2Image *image) { int index; /* find the matching page struct and mark it released */ for (index = 0; index < ctx->max_page_index; index++) { if (ctx->pages[index].image == image) { jbig2_image_release(ctx, image); ctx->pages[index].state = JBIG2_PAGE_RELEASED; jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "page %d released by the client", ctx->pages[index].number); return 0; } } /* no matching pages */ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "jbig2_release_page called on unknown page"); return 1; }
@@ -155,9 +155,9 @@ int jbig2_end_of_stripe(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data) { Jbig2Page page = ctx->pages[ctx->current_page]; - int end_row; + uint32_t end_row; - end_row = jbig2_get_int32(segment_data); + end_row = jbig2_get_uint32(segment_data); if (end_row < page.end_row) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "end of stripe segment with non-positive end row advance" " (new end row %d vs current end row %d)", end_row, page.end_row); @@ -248,7 +248,7 @@ jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *image, int x, /* grow the page to accomodate a new stripe if necessary */ if (page->striped) { - int new_height = y + image->height + page->end_row; + uint32_t new_height = y + image->height + page->end_row; if (page->image->height < new_height) { jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "growing page buffer to %d rows " "to accomodate new stripe", new_height);
CWE-119
null
null
14,481
jbig2_get_region_segment_info(Jbig2RegionSegmentInfo *info, const uint8_t *segment_data) { /* 7.4.1 */ info->width = jbig2_get_int32(segment_data); info->height = jbig2_get_int32(segment_data + 4); info->x = jbig2_get_int32(segment_data + 8); info->y = jbig2_get_int32(segment_data + 12); info->flags = segment_data[16]; info->op = (Jbig2ComposeOp)(info->flags & 0x7); }
Overflow
0
jbig2_get_region_segment_info(Jbig2RegionSegmentInfo *info, const uint8_t *segment_data) { /* 7.4.1 */ info->width = jbig2_get_int32(segment_data); info->height = jbig2_get_int32(segment_data + 4); info->x = jbig2_get_int32(segment_data + 8); info->y = jbig2_get_int32(segment_data + 12); info->flags = segment_data[16]; info->op = (Jbig2ComposeOp)(info->flags & 0x7); }
@@ -39,10 +39,10 @@ jbig2_parse_segment_header(Jbig2Ctx *ctx, uint8_t *buf, size_t buf_size, size_t uint8_t rtscarf; uint32_t rtscarf_long; uint32_t *referred_to_segments; - int referred_to_segment_count; - int referred_to_segment_size; - int pa_size; - int offset; + uint32_t referred_to_segment_count; + uint32_t referred_to_segment_size; + uint32_t pa_size; + uint32_t offset; /* minimum possible size of a jbig2 segment header */ if (buf_size < 11) @@ -83,7 +83,7 @@ jbig2_parse_segment_header(Jbig2Ctx *ctx, uint8_t *buf, size_t buf_size, size_t /* 7.2.5 */ if (referred_to_segment_count) { - int i; + uint32_t i; referred_to_segments = jbig2_new(ctx, uint32_t, referred_to_segment_count * referred_to_segment_size); if (referred_to_segments == NULL) {
CWE-119
null
null
14,482
jbig2_parse_extension_segment(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data) { uint32_t type = jbig2_get_uint32(segment_data); bool reserved = type & 0x20000000; /* bool dependent = type & 0x40000000; (NYI) */ bool necessary = type & 0x80000000; if (necessary && !reserved) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extension segment is marked 'necessary' but " "not 'reservered' contrary to spec"); } switch (type) { case 0x20000000: return jbig2_comment_ascii(ctx, segment, segment_data); case 0x20000002: return jbig2_comment_unicode(ctx, segment, segment_data); default: if (necessary) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unhandled necessary extension segment type 0x%08x", type); } else { return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled extension segment"); } } }
Overflow
0
jbig2_parse_extension_segment(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data) { uint32_t type = jbig2_get_uint32(segment_data); bool reserved = type & 0x20000000; /* bool dependent = type & 0x40000000; (NYI) */ bool necessary = type & 0x80000000; if (necessary && !reserved) { jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extension segment is marked 'necessary' but " "not 'reservered' contrary to spec"); } switch (type) { case 0x20000000: return jbig2_comment_ascii(ctx, segment, segment_data); case 0x20000002: return jbig2_comment_unicode(ctx, segment, segment_data); default: if (necessary) { return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unhandled necessary extension segment type 0x%08x", type); } else { return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled extension segment"); } } }
@@ -39,10 +39,10 @@ jbig2_parse_segment_header(Jbig2Ctx *ctx, uint8_t *buf, size_t buf_size, size_t uint8_t rtscarf; uint32_t rtscarf_long; uint32_t *referred_to_segments; - int referred_to_segment_count; - int referred_to_segment_size; - int pa_size; - int offset; + uint32_t referred_to_segment_count; + uint32_t referred_to_segment_size; + uint32_t pa_size; + uint32_t offset; /* minimum possible size of a jbig2 segment header */ if (buf_size < 11) @@ -83,7 +83,7 @@ jbig2_parse_segment_header(Jbig2Ctx *ctx, uint8_t *buf, size_t buf_size, size_t /* 7.2.5 */ if (referred_to_segment_count) { - int i; + uint32_t i; referred_to_segments = jbig2_new(ctx, uint32_t, referred_to_segment_count * referred_to_segment_size); if (referred_to_segments == NULL) {
CWE-119
null
null
14,483
const ECDSA_METHOD *ECDSA_OpenSSL(void) { return &openssl_ecdsa_meth; }
null
0
const ECDSA_METHOD *ECDSA_OpenSSL(void) { return &openssl_ecdsa_meth; }
@@ -187,11 +187,37 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, while (BN_is_zero(r)); /* compute the inverse of k */ - if (!BN_mod_inverse(k, k, order, ctx)) - { - ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); - goto err; - } + if (EC_GROUP_get_mont_data(group) != NULL) + { + /* We want inverse in constant time, therefore we utilize the + * fact order must be prime and use Fermats Little Theorem + * instead. */ + if (!BN_set_word(X, 2) ) + { + ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); + goto err; + } + if (!BN_mod_sub(X, order, X, order, ctx)) + { + ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); + goto err; + } + BN_set_flags(X, BN_FLG_CONSTTIME); + if (!BN_mod_exp_mont_consttime(k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) + { + ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); + goto err; + } + } + else + { + if (!BN_mod_inverse(k, k, order, ctx)) + { + ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); + goto err; + } + } + /* clear old values if necessary */ if (*rp != NULL) BN_clear_free(*rp);
CWE-320
null
null
14,484
static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig, EC_KEY *eckey) { int ret = -1, i; BN_CTX *ctx; BIGNUM *order, *u1, *u2, *m, *X; EC_POINT *point = NULL; const EC_GROUP *group; const EC_POINT *pub_key; /* check input values */ if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL || (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS); return -1; } ctx = BN_CTX_new(); if (!ctx) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE); return -1; } BN_CTX_start(ctx); order = BN_CTX_get(ctx); u1 = BN_CTX_get(ctx); u2 = BN_CTX_get(ctx); m = BN_CTX_get(ctx); X = BN_CTX_get(ctx); if (!X) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } if (!EC_GROUP_get_order(group, order, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB); goto err; } if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) || BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE); ret = 0; /* signature is invalid */ goto err; } /* calculate tmp1 = inv(S) mod order */ if (!BN_mod_inverse(u2, sig->s, order, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } /* digest -> m */ i = BN_num_bits(order); /* Need to truncate digest if it is too long: first truncate whole * bytes. */ if (8 * dgst_len > i) dgst_len = (i + 7)/8; if (!BN_bin2bn(dgst, dgst_len, m)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } /* If still too long truncate remaining bits with a shift */ if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } /* u1 = m * tmp mod order */ if (!BN_mod_mul(u1, m, u2, order, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } /* u2 = r * w mod q */ if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } if ((point = EC_POINT_new(group)) == NULL) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE); goto err; } if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB); goto err; } if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) { if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB); goto err; } } #ifndef OPENSSL_NO_EC2M else /* NID_X9_62_characteristic_two_field */ { if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB); goto err; } } #endif if (!BN_nnmod(u1, X, order, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } /* if the signature is correct u1 is equal to sig->r */ ret = (BN_ucmp(u1, sig->r) == 0); err: BN_CTX_end(ctx); BN_CTX_free(ctx); if (point) EC_POINT_free(point); return ret; }
null
0
static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig, EC_KEY *eckey) { int ret = -1, i; BN_CTX *ctx; BIGNUM *order, *u1, *u2, *m, *X; EC_POINT *point = NULL; const EC_GROUP *group; const EC_POINT *pub_key; /* check input values */ if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL || (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS); return -1; } ctx = BN_CTX_new(); if (!ctx) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE); return -1; } BN_CTX_start(ctx); order = BN_CTX_get(ctx); u1 = BN_CTX_get(ctx); u2 = BN_CTX_get(ctx); m = BN_CTX_get(ctx); X = BN_CTX_get(ctx); if (!X) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } if (!EC_GROUP_get_order(group, order, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB); goto err; } if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) || BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE); ret = 0; /* signature is invalid */ goto err; } /* calculate tmp1 = inv(S) mod order */ if (!BN_mod_inverse(u2, sig->s, order, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } /* digest -> m */ i = BN_num_bits(order); /* Need to truncate digest if it is too long: first truncate whole * bytes. */ if (8 * dgst_len > i) dgst_len = (i + 7)/8; if (!BN_bin2bn(dgst, dgst_len, m)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } /* If still too long truncate remaining bits with a shift */ if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } /* u1 = m * tmp mod order */ if (!BN_mod_mul(u1, m, u2, order, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } /* u2 = r * w mod q */ if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } if ((point = EC_POINT_new(group)) == NULL) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE); goto err; } if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB); goto err; } if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) { if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB); goto err; } } #ifndef OPENSSL_NO_EC2M else /* NID_X9_62_characteristic_two_field */ { if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB); goto err; } } #endif if (!BN_nnmod(u1, X, order, ctx)) { ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); goto err; } /* if the signature is correct u1 is equal to sig->r */ ret = (BN_ucmp(u1, sig->r) == 0); err: BN_CTX_end(ctx); BN_CTX_free(ctx); if (point) EC_POINT_free(point); return ret; }
@@ -187,11 +187,37 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, while (BN_is_zero(r)); /* compute the inverse of k */ - if (!BN_mod_inverse(k, k, order, ctx)) - { - ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); - goto err; - } + if (EC_GROUP_get_mont_data(group) != NULL) + { + /* We want inverse in constant time, therefore we utilize the + * fact order must be prime and use Fermats Little Theorem + * instead. */ + if (!BN_set_word(X, 2) ) + { + ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); + goto err; + } + if (!BN_mod_sub(X, order, X, order, ctx)) + { + ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); + goto err; + } + BN_set_flags(X, BN_FLG_CONSTTIME); + if (!BN_mod_exp_mont_consttime(k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) + { + ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); + goto err; + } + } + else + { + if (!BN_mod_inverse(k, k, order, ctx)) + { + ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); + goto err; + } + } + /* clear old values if necessary */ if (*rp != NULL) BN_clear_free(*rp);
CWE-320
null
null
14,485
XcursorLibraryLoadImage (const char *file, const char *theme, int size) { FILE *f = NULL; XcursorImage *image = NULL; if (!file) return NULL; if (theme) f = XcursorScanTheme (theme, file); if (!f) f = XcursorScanTheme ("default", file); if (f == XCURSOR_SCAN_CORE) return NULL; if (f) { image = XcursorFileLoadImage (f, size); fclose (f); } return image; }
DoS Exec Code Overflow
0
XcursorLibraryLoadImage (const char *file, const char *theme, int size) { FILE *f = NULL; XcursorImage *image = NULL; if (!file) return NULL; if (theme) f = XcursorScanTheme (theme, file); if (!f) f = XcursorScanTheme ("default", file); if (f == XCURSOR_SCAN_CORE) return NULL; if (f) { image = XcursorFileLoadImage (f, size); fclose (f); } return image; }
@@ -180,7 +180,7 @@ _XcursorThemeInherits (const char *full) if (*l != '=') continue; l++; while (*l == ' ') l++; - result = malloc (strlen (l)); + result = malloc (strlen (l) + 1); if (result) { r = result;
CWE-119
null
null
14,486
XcursorLibraryLoadImages (const char *file, const char *theme, int size) { FILE *f = NULL; XcursorImages *images = NULL; if (!file) return NULL; if (theme) f = XcursorScanTheme (theme, file); if (!f) f = XcursorScanTheme ("default", file); if (f == XCURSOR_SCAN_CORE) return NULL; if (f) { images = XcursorFileLoadImages (f, size); if (images) XcursorImagesSetName (images, file); fclose (f); } return images; }
DoS Exec Code Overflow
0
XcursorLibraryLoadImages (const char *file, const char *theme, int size) { FILE *f = NULL; XcursorImages *images = NULL; if (!file) return NULL; if (theme) f = XcursorScanTheme (theme, file); if (!f) f = XcursorScanTheme ("default", file); if (f == XCURSOR_SCAN_CORE) return NULL; if (f) { images = XcursorFileLoadImages (f, size); if (images) XcursorImagesSetName (images, file); fclose (f); } return images; }
@@ -180,7 +180,7 @@ _XcursorThemeInherits (const char *full) if (*l != '=') continue; l++; while (*l == ' ') l++; - result = malloc (strlen (l)); + result = malloc (strlen (l) + 1); if (result) { r = result;
CWE-119
null
null
14,487
XcursorLibraryShape (const char *library) { int low, high; int mid; int c; low = 0; high = NUM_STANDARD_NAMES - 1; while (low < high - 1) { mid = (low + high) >> 1; c = strcmp (library, STANDARD_NAME (mid)); if (c == 0) return (mid << 1); if (c > 0) low = mid; else high = mid; } while (low <= high) { if (!strcmp (library, STANDARD_NAME (low))) return (low << 1); low++; } return -1; }
DoS Exec Code Overflow
0
XcursorLibraryShape (const char *library) { int low, high; int mid; int c; low = 0; high = NUM_STANDARD_NAMES - 1; while (low < high - 1) { mid = (low + high) >> 1; c = strcmp (library, STANDARD_NAME (mid)); if (c == 0) return (mid << 1); if (c > 0) low = mid; else high = mid; } while (low <= high) { if (!strcmp (library, STANDARD_NAME (low))) return (low << 1); low++; } return -1; }
@@ -180,7 +180,7 @@ _XcursorThemeInherits (const char *full) if (*l != '=') continue; l++; while (*l == ' ') l++; - result = malloc (strlen (l)); + result = malloc (strlen (l) + 1); if (result) { r = result;
CWE-119
null
null
14,488
XcursorScanTheme (const char *theme, const char *name) { FILE *f = NULL; char *full; char *dir; const char *path; char *inherits = NULL; const char *i; if (!theme || !name) return NULL; /* * XCURSOR_CORE_THEME is a magic name; cursors from the core set * are never found in any directory. Instead, a magic value is * returned which truncates any search so that overlying functions * can switch to equivalent core cursors */ if (!strcmp (theme, XCURSOR_CORE_THEME) && XcursorLibraryShape (name) >= 0) return XCURSOR_SCAN_CORE; /* * Scan this theme */ for (path = XcursorLibraryPath (); path && f == NULL; path = _XcursorNextPath (path)) { dir = _XcursorBuildThemeDir (path, theme); if (dir) { full = _XcursorBuildFullname (dir, "cursors", name); if (full) { f = fopen (full, "r"); free (full); } if (!f && !inherits) { full = _XcursorBuildFullname (dir, "", "index.theme"); if (full) { inherits = _XcursorThemeInherits (full); free (full); } } free (dir); } } /* * Recurse to scan inherited themes */ for (i = inherits; i && f == NULL; i = _XcursorNextPath (i)) f = XcursorScanTheme (i, name); if (inherits != NULL) free (inherits); return f; }
DoS Exec Code Overflow
0
XcursorScanTheme (const char *theme, const char *name) { FILE *f = NULL; char *full; char *dir; const char *path; char *inherits = NULL; const char *i; if (!theme || !name) return NULL; /* * XCURSOR_CORE_THEME is a magic name; cursors from the core set * are never found in any directory. Instead, a magic value is * returned which truncates any search so that overlying functions * can switch to equivalent core cursors */ if (!strcmp (theme, XCURSOR_CORE_THEME) && XcursorLibraryShape (name) >= 0) return XCURSOR_SCAN_CORE; /* * Scan this theme */ for (path = XcursorLibraryPath (); path && f == NULL; path = _XcursorNextPath (path)) { dir = _XcursorBuildThemeDir (path, theme); if (dir) { full = _XcursorBuildFullname (dir, "cursors", name); if (full) { f = fopen (full, "r"); free (full); } if (!f && !inherits) { full = _XcursorBuildFullname (dir, "", "index.theme"); if (full) { inherits = _XcursorThemeInherits (full); free (full); } } free (dir); } } /* * Recurse to scan inherited themes */ for (i = inherits; i && f == NULL; i = _XcursorNextPath (i)) f = XcursorScanTheme (i, name); if (inherits != NULL) free (inherits); return f; }
@@ -180,7 +180,7 @@ _XcursorThemeInherits (const char *full) if (*l != '=') continue; l++; while (*l == ' ') l++; - result = malloc (strlen (l)); + result = malloc (strlen (l) + 1); if (result) { r = result;
CWE-119
null
null
14,489
XcursorShapeLoadCursor (Display *dpy, unsigned int shape) { unsigned int id = shape >> 1; if (id < NUM_STANDARD_NAMES) return XcursorLibraryLoadCursor (dpy, STANDARD_NAME (id)); else return 0; }
DoS Exec Code Overflow
0
XcursorShapeLoadCursor (Display *dpy, unsigned int shape) { unsigned int id = shape >> 1; if (id < NUM_STANDARD_NAMES) return XcursorLibraryLoadCursor (dpy, STANDARD_NAME (id)); else return 0; }
@@ -180,7 +180,7 @@ _XcursorThemeInherits (const char *full) if (*l != '=') continue; l++; while (*l == ' ') l++; - result = malloc (strlen (l)); + result = malloc (strlen (l) + 1); if (result) { r = result;
CWE-119
null
null
14,490
XcursorShapeLoadCursors (Display *dpy, unsigned int shape) { unsigned int id = shape >> 1; if (id < NUM_STANDARD_NAMES) return XcursorLibraryLoadCursors (dpy, STANDARD_NAME (id)); else return NULL; }
DoS Exec Code Overflow
0
XcursorShapeLoadCursors (Display *dpy, unsigned int shape) { unsigned int id = shape >> 1; if (id < NUM_STANDARD_NAMES) return XcursorLibraryLoadCursors (dpy, STANDARD_NAME (id)); else return NULL; }
@@ -180,7 +180,7 @@ _XcursorThemeInherits (const char *full) if (*l != '=') continue; l++; while (*l == ' ') l++; - result = malloc (strlen (l)); + result = malloc (strlen (l) + 1); if (result) { r = result;
CWE-119
null
null
14,491
XcursorShapeLoadImage (unsigned int shape, const char *theme, int size) { unsigned int id = shape >> 1; if (id < NUM_STANDARD_NAMES) return XcursorLibraryLoadImage (STANDARD_NAME (id), theme, size); else return NULL; }
DoS Exec Code Overflow
0
XcursorShapeLoadImage (unsigned int shape, const char *theme, int size) { unsigned int id = shape >> 1; if (id < NUM_STANDARD_NAMES) return XcursorLibraryLoadImage (STANDARD_NAME (id), theme, size); else return NULL; }
@@ -180,7 +180,7 @@ _XcursorThemeInherits (const char *full) if (*l != '=') continue; l++; while (*l == ' ') l++; - result = malloc (strlen (l)); + result = malloc (strlen (l) + 1); if (result) { r = result;
CWE-119
null
null
14,492
XcursorShapeLoadImages (unsigned int shape, const char *theme, int size) { unsigned int id = shape >> 1; if (id < NUM_STANDARD_NAMES) return XcursorLibraryLoadImages (STANDARD_NAME (id), theme, size); else return NULL; }
DoS Exec Code Overflow
0
XcursorShapeLoadImages (unsigned int shape, const char *theme, int size) { unsigned int id = shape >> 1; if (id < NUM_STANDARD_NAMES) return XcursorLibraryLoadImages (STANDARD_NAME (id), theme, size); else return NULL; }
@@ -180,7 +180,7 @@ _XcursorThemeInherits (const char *full) if (*l != '=') continue; l++; while (*l == ' ') l++; - result = malloc (strlen (l)); + result = malloc (strlen (l) + 1); if (result) { r = result;
CWE-119
null
null
14,493
_XcursorAddPathElt (char *path, const char *elt, int len) { size_t pathlen = strlen (path); /* append / if the path doesn't currently have one */ if (path[0] == '\0' || path[pathlen - 1] != '/') { strcat (path, "/"); pathlen++; } if (len == -1) len = strlen (elt); /* strip leading slashes */ while (len && elt[0] == '/') { elt++; len--; } strncpy (path + pathlen, elt, len); path[pathlen + len] = '\0'; }
DoS Exec Code Overflow
0
_XcursorAddPathElt (char *path, const char *elt, int len) { size_t pathlen = strlen (path); /* append / if the path doesn't currently have one */ if (path[0] == '\0' || path[pathlen - 1] != '/') { strcat (path, "/"); pathlen++; } if (len == -1) len = strlen (elt); /* strip leading slashes */ while (len && elt[0] == '/') { elt++; len--; } strncpy (path + pathlen, elt, len); path[pathlen + len] = '\0'; }
@@ -180,7 +180,7 @@ _XcursorThemeInherits (const char *full) if (*l != '=') continue; l++; while (*l == ' ') l++; - result = malloc (strlen (l)); + result = malloc (strlen (l) + 1); if (result) { r = result;
CWE-119
null
null
14,494
static void abort_unzip(STATE_PARAM_ONLY) { huft_free_all(PASS_STATE_ONLY); longjmp(error_jmp, 1); }
null
0
static void abort_unzip(STATE_PARAM_ONLY) { huft_free_all(PASS_STATE_ONLY); longjmp(error_jmp, 1); }
@@ -305,11 +305,12 @@ static int huft_build(const unsigned *b, const unsigned n, unsigned i; /* counter, current code */ unsigned j; /* counter */ int k; /* number of bits in current code */ - unsigned *p; /* pointer into c[], b[], or v[] */ + const unsigned *p; /* pointer into c[], b[], or v[] */ huft_t *q; /* points to current table */ huft_t r; /* table entry for structure assignment */ huft_t *u[BMAX]; /* table stack */ unsigned v[N_MAX]; /* values in order of bit length */ + unsigned v_end; int ws[BMAX + 1]; /* bits decoded stack */ int w; /* bits decoded */ unsigned x[BMAX + 1]; /* bit offsets, then code stack */ @@ -324,7 +325,7 @@ static int huft_build(const unsigned *b, const unsigned n, /* Generate counts for each bit length */ memset(c, 0, sizeof(c)); - p = (unsigned *) b; /* cast allows us to reuse p for pointing to b */ + p = b; i = n; do { c[*p]++; /* assume all entries <= BMAX */ @@ -365,12 +366,14 @@ static int huft_build(const unsigned *b, const unsigned n, } /* Make a table of values in order of bit lengths */ - p = (unsigned *) b; + p = b; i = 0; + v_end = 0; do { j = *p++; if (j != 0) { v[x[j]++] = i; + v_end = x[j]; } } while (++i < n); @@ -432,7 +435,7 @@ static int huft_build(const unsigned *b, const unsigned n, /* set up table entry in r */ r.b = (unsigned char) (k - w); - if (p >= v + n) { + if (p >= v + v_end) { // Was "if (p >= v + n)" but v[] can be shorter! r.e = 99; /* out of values--invalid code */ } else if (*p < s) { r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
CWE-476
null
null
14,495
static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required) { while (*current < required) { if (bytebuffer_offset >= bytebuffer_size) { unsigned sz = bytebuffer_max - 4; if (to_read >= 0 && to_read < sz) /* unzip only */ sz = to_read; /* Leave the first 4 bytes empty so we can always unwind the bitbuffer * to the front of the bytebuffer */ bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], sz); if ((int)bytebuffer_size < 1) { error_msg = "unexpected end of file"; abort_unzip(PASS_STATE_ONLY); } if (to_read >= 0) /* unzip only */ to_read -= bytebuffer_size; bytebuffer_size += 4; bytebuffer_offset = 4; } bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current; bytebuffer_offset++; *current += 8; } return bitbuffer; }
null
0
static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current, const unsigned required) { while (*current < required) { if (bytebuffer_offset >= bytebuffer_size) { unsigned sz = bytebuffer_max - 4; if (to_read >= 0 && to_read < sz) /* unzip only */ sz = to_read; /* Leave the first 4 bytes empty so we can always unwind the bitbuffer * to the front of the bytebuffer */ bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], sz); if ((int)bytebuffer_size < 1) { error_msg = "unexpected end of file"; abort_unzip(PASS_STATE_ONLY); } if (to_read >= 0) /* unzip only */ to_read -= bytebuffer_size; bytebuffer_size += 4; bytebuffer_offset = 4; } bitbuffer |= ((unsigned) bytebuffer[bytebuffer_offset]) << *current; bytebuffer_offset++; *current += 8; } return bitbuffer; }
@@ -305,11 +305,12 @@ static int huft_build(const unsigned *b, const unsigned n, unsigned i; /* counter, current code */ unsigned j; /* counter */ int k; /* number of bits in current code */ - unsigned *p; /* pointer into c[], b[], or v[] */ + const unsigned *p; /* pointer into c[], b[], or v[] */ huft_t *q; /* points to current table */ huft_t r; /* table entry for structure assignment */ huft_t *u[BMAX]; /* table stack */ unsigned v[N_MAX]; /* values in order of bit length */ + unsigned v_end; int ws[BMAX + 1]; /* bits decoded stack */ int w; /* bits decoded */ unsigned x[BMAX + 1]; /* bit offsets, then code stack */ @@ -324,7 +325,7 @@ static int huft_build(const unsigned *b, const unsigned n, /* Generate counts for each bit length */ memset(c, 0, sizeof(c)); - p = (unsigned *) b; /* cast allows us to reuse p for pointing to b */ + p = b; i = n; do { c[*p]++; /* assume all entries <= BMAX */ @@ -365,12 +366,14 @@ static int huft_build(const unsigned *b, const unsigned n, } /* Make a table of values in order of bit lengths */ - p = (unsigned *) b; + p = b; i = 0; + v_end = 0; do { j = *p++; if (j != 0) { v[x[j]++] = i; + v_end = x[j]; } } while (++i < n); @@ -432,7 +435,7 @@ static int huft_build(const unsigned *b, const unsigned n, /* set up table entry in r */ r.b = (unsigned char) (k - w); - if (p >= v + n) { + if (p >= v + v_end) { // Was "if (p >= v + n)" but v[] can be shorter! r.e = 99; /* out of values--invalid code */ } else if (*p < s) { r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
CWE-476
null
null
14,496
static void huft_free_all(STATE_PARAM_ONLY) { huft_free(inflate_codes_tl); huft_free(inflate_codes_td); inflate_codes_tl = NULL; inflate_codes_td = NULL; }
null
0
static void huft_free_all(STATE_PARAM_ONLY) { huft_free(inflate_codes_tl); huft_free(inflate_codes_td); inflate_codes_tl = NULL; inflate_codes_td = NULL; }
@@ -305,11 +305,12 @@ static int huft_build(const unsigned *b, const unsigned n, unsigned i; /* counter, current code */ unsigned j; /* counter */ int k; /* number of bits in current code */ - unsigned *p; /* pointer into c[], b[], or v[] */ + const unsigned *p; /* pointer into c[], b[], or v[] */ huft_t *q; /* points to current table */ huft_t r; /* table entry for structure assignment */ huft_t *u[BMAX]; /* table stack */ unsigned v[N_MAX]; /* values in order of bit length */ + unsigned v_end; int ws[BMAX + 1]; /* bits decoded stack */ int w; /* bits decoded */ unsigned x[BMAX + 1]; /* bit offsets, then code stack */ @@ -324,7 +325,7 @@ static int huft_build(const unsigned *b, const unsigned n, /* Generate counts for each bit length */ memset(c, 0, sizeof(c)); - p = (unsigned *) b; /* cast allows us to reuse p for pointing to b */ + p = b; i = n; do { c[*p]++; /* assume all entries <= BMAX */ @@ -365,12 +366,14 @@ static int huft_build(const unsigned *b, const unsigned n, } /* Make a table of values in order of bit lengths */ - p = (unsigned *) b; + p = b; i = 0; + v_end = 0; do { j = *p++; if (j != 0) { v[x[j]++] = i; + v_end = x[j]; } } while (++i < n); @@ -432,7 +435,7 @@ static int huft_build(const unsigned *b, const unsigned n, /* set up table entry in r */ r.b = (unsigned char) (k - w); - if (p >= v + n) { + if (p >= v + v_end) { // Was "if (p >= v + n)" but v[] can be shorter! r.e = 99; /* out of values--invalid code */ } else if (*p < s) { r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
CWE-476
null
null
14,497
static int cbk_http_request(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) { static int dummy; struct MHD_Response *response; int ret; char *nurl; unsigned int resp_code; if (strcmp(method, "GET")) return MHD_NO; if (&dummy != *ptr) { /* The first time only the headers are valid, do not respond in the first round... */ *ptr = &dummy; return MHD_YES; } if (*upload_data_size) return MHD_NO; *ptr = NULL; /* clear context pointer */ log_debug(_("HTTP Request: %s"), url); nurl = url_normalize(url); pmutex_lock(&mutex); response = create_response(nurl, method, &resp_code); pmutex_unlock(&mutex); ret = MHD_queue_response(connection, resp_code, response); MHD_destroy_response(response); free(nurl); return ret; }
Dir. Trav.
0
static int cbk_http_request(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) { static int dummy; struct MHD_Response *response; int ret; char *nurl; unsigned int resp_code; if (strcmp(method, "GET")) return MHD_NO; if (&dummy != *ptr) { /* The first time only the headers are valid, do not respond in the first round... */ *ptr = &dummy; return MHD_YES; } if (*upload_data_size) return MHD_NO; *ptr = NULL; /* clear context pointer */ log_debug(_("HTTP Request: %s"), url); nurl = url_normalize(url); pmutex_lock(&mutex); response = create_response(nurl, method, &resp_code); pmutex_unlock(&mutex); ret = MHD_queue_response(connection, resp_code, response); MHD_destroy_response(response); free(nurl); return ret; }
@@ -23,6 +23,7 @@ #include <libintl.h> #define _(str) gettext(str) +#include <limits.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -245,15 +246,25 @@ static struct MHD_Response *create_response_file(const char *nurl, static struct MHD_Response * create_response(const char *nurl, const char *method, unsigned int *rp_code) { - char *page, *fpath; + char *page, *fpath, *rpath; struct MHD_Response *resp = NULL; + int n; if (!strncmp(nurl, URL_BASE_API_1_1, strlen(URL_BASE_API_1_1))) { resp = create_response_api(nurl, method, rp_code); } else { fpath = get_path(nurl, server_data.www_dir); - resp = create_response_file(nurl, method, rp_code, fpath); + rpath = realpath(fpath, NULL); + if (rpath) { + n = strlen(server_data.www_dir); + if (!strncmp(server_data.www_dir, rpath, n)) + resp = create_response_file(nurl, + method, + rp_code, + fpath); + free(rpath); + } free(fpath); } @@ -349,7 +360,7 @@ int main(int argc, char *argv[]) switch (optc) { case 'w': if (optarg) - server_data.www_dir = strdup(optarg); + server_data.www_dir = realpath(optarg, NULL); break; case 'p': if (optarg) @@ -388,8 +399,14 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (!server_data.www_dir) - server_data.www_dir = strdup(DEFAULT_WWW_DIR); + if (!server_data.www_dir) { + server_data.www_dir = realpath(DEFAULT_WWW_DIR, NULL); + if (!server_data.www_dir) { + fprintf(stderr, + _("Webserver directory does not exist.\n")); + exit(EXIT_FAILURE); + } + } if (!log_file) log_file = strdup(DEFAULT_LOG_FILE);
CWE-22
null
null
14,498
create_response_api(const char *nurl, const char *method, unsigned int *rp_code) { struct MHD_Response *resp; struct psensor *s; char *page = NULL; if (!strcmp(nurl, URL_BASE_API_1_1_SENSORS)) { page = sensors_to_json_string(server_data.sensors); #ifdef HAVE_GTOP } else if (!strcmp(nurl, URL_API_1_1_SYSINFO)) { page = sysinfo_to_json_string(&server_data.psysinfo); } else if (!strcmp(nurl, URL_API_1_1_CPU_USAGE)) { page = sensor_to_json_string(server_data.cpu_usage); #endif } else if (!strncmp(nurl, URL_BASE_API_1_1_SENSORS, strlen(URL_BASE_API_1_1_SENSORS)) && nurl[strlen(URL_BASE_API_1_1_SENSORS)] == '/') { const char *sid = nurl + strlen(URL_BASE_API_1_1_SENSORS) + 1; s = psensor_list_get_by_id(server_data.sensors, sid); if (s) page = sensor_to_json_string(s); } else if (!strcmp(nurl, URL_API_1_1_SERVER_STOP)) { server_stop_requested = 1; page = strdup(HTML_STOP_REQUESTED); } if (page) { *rp_code = MHD_HTTP_OK; resp = MHD_create_response_from_data(strlen(page), page, MHD_YES, MHD_NO); MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE, "application/json"); return resp; } return NULL; }
Dir. Trav.
0
create_response_api(const char *nurl, const char *method, unsigned int *rp_code) { struct MHD_Response *resp; struct psensor *s; char *page = NULL; if (!strcmp(nurl, URL_BASE_API_1_1_SENSORS)) { page = sensors_to_json_string(server_data.sensors); #ifdef HAVE_GTOP } else if (!strcmp(nurl, URL_API_1_1_SYSINFO)) { page = sysinfo_to_json_string(&server_data.psysinfo); } else if (!strcmp(nurl, URL_API_1_1_CPU_USAGE)) { page = sensor_to_json_string(server_data.cpu_usage); #endif } else if (!strncmp(nurl, URL_BASE_API_1_1_SENSORS, strlen(URL_BASE_API_1_1_SENSORS)) && nurl[strlen(URL_BASE_API_1_1_SENSORS)] == '/') { const char *sid = nurl + strlen(URL_BASE_API_1_1_SENSORS) + 1; s = psensor_list_get_by_id(server_data.sensors, sid); if (s) page = sensor_to_json_string(s); } else if (!strcmp(nurl, URL_API_1_1_SERVER_STOP)) { server_stop_requested = 1; page = strdup(HTML_STOP_REQUESTED); } if (page) { *rp_code = MHD_HTTP_OK; resp = MHD_create_response_from_data(strlen(page), page, MHD_YES, MHD_NO); MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE, "application/json"); return resp; } return NULL; }
@@ -23,6 +23,7 @@ #include <libintl.h> #define _(str) gettext(str) +#include <limits.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -245,15 +246,25 @@ static struct MHD_Response *create_response_file(const char *nurl, static struct MHD_Response * create_response(const char *nurl, const char *method, unsigned int *rp_code) { - char *page, *fpath; + char *page, *fpath, *rpath; struct MHD_Response *resp = NULL; + int n; if (!strncmp(nurl, URL_BASE_API_1_1, strlen(URL_BASE_API_1_1))) { resp = create_response_api(nurl, method, rp_code); } else { fpath = get_path(nurl, server_data.www_dir); - resp = create_response_file(nurl, method, rp_code, fpath); + rpath = realpath(fpath, NULL); + if (rpath) { + n = strlen(server_data.www_dir); + if (!strncmp(server_data.www_dir, rpath, n)) + resp = create_response_file(nurl, + method, + rp_code, + fpath); + free(rpath); + } free(fpath); } @@ -349,7 +360,7 @@ int main(int argc, char *argv[]) switch (optc) { case 'w': if (optarg) - server_data.www_dir = strdup(optarg); + server_data.www_dir = realpath(optarg, NULL); break; case 'p': if (optarg) @@ -388,8 +399,14 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (!server_data.www_dir) - server_data.www_dir = strdup(DEFAULT_WWW_DIR); + if (!server_data.www_dir) { + server_data.www_dir = realpath(DEFAULT_WWW_DIR, NULL); + if (!server_data.www_dir) { + fprintf(stderr, + _("Webserver directory does not exist.\n")); + exit(EXIT_FAILURE); + } + } if (!log_file) log_file = strdup(DEFAULT_LOG_FILE);
CWE-22
null
null
14,499
static char *get_path(const char *url, const char *www_dir) { const char *p; char *res; if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/")) p = "/index.html"; else p = url; res = malloc(strlen(www_dir)+strlen(p)+1); strcpy(res, www_dir); strcat(res, p); return res; }
Dir. Trav.
0
static char *get_path(const char *url, const char *www_dir) { const char *p; char *res; if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/")) p = "/index.html"; else p = url; res = malloc(strlen(www_dir)+strlen(p)+1); strcpy(res, www_dir); strcat(res, p); return res; }
@@ -23,6 +23,7 @@ #include <libintl.h> #define _(str) gettext(str) +#include <limits.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -245,15 +246,25 @@ static struct MHD_Response *create_response_file(const char *nurl, static struct MHD_Response * create_response(const char *nurl, const char *method, unsigned int *rp_code) { - char *page, *fpath; + char *page, *fpath, *rpath; struct MHD_Response *resp = NULL; + int n; if (!strncmp(nurl, URL_BASE_API_1_1, strlen(URL_BASE_API_1_1))) { resp = create_response_api(nurl, method, rp_code); } else { fpath = get_path(nurl, server_data.www_dir); - resp = create_response_file(nurl, method, rp_code, fpath); + rpath = realpath(fpath, NULL); + if (rpath) { + n = strlen(server_data.www_dir); + if (!strncmp(server_data.www_dir, rpath, n)) + resp = create_response_file(nurl, + method, + rp_code, + fpath); + free(rpath); + } free(fpath); } @@ -349,7 +360,7 @@ int main(int argc, char *argv[]) switch (optc) { case 'w': if (optarg) - server_data.www_dir = strdup(optarg); + server_data.www_dir = realpath(optarg, NULL); break; case 'p': if (optarg) @@ -388,8 +399,14 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (!server_data.www_dir) - server_data.www_dir = strdup(DEFAULT_WWW_DIR); + if (!server_data.www_dir) { + server_data.www_dir = realpath(DEFAULT_WWW_DIR, NULL); + if (!server_data.www_dir) { + fprintf(stderr, + _("Webserver directory does not exist.\n")); + exit(EXIT_FAILURE); + } + } if (!log_file) log_file = strdup(DEFAULT_LOG_FILE);
CWE-22
null
null