[OTR-dev] [PATCH 5/9] Fix potential memory leak and simplify if-statement.

Andreas Schlick ninox+otr at posteo.net
Wed Oct 22 14:39:23 EDT 2014


---
 src/message.c | 71 +++++++++++++++--------------------------------------------
 1 file changed, 18 insertions(+), 53 deletions(-)

diff --git a/src/message.c b/src/message.c
index c3a9389..600800b 100644
--- a/src/message.c
+++ b/src/message.c
@@ -1310,8 +1310,6 @@ int otrl_message_receiving(OtrlUserState us, const OtrlMessageAppOps *ops,
 		gcry_error_t err;
 		OtrlTLV *tlvs, *tlv;
 		char *plaintext;
-		char *buf;
-		const char *err_msg;
 		unsigned char *extrakey;
 		unsigned char flags;
 		NextExpectedSMP nextMsg;
@@ -1335,57 +1333,24 @@ int otrl_message_receiving(OtrlUserState us, const OtrlMessageAppOps *ops,
                    break;
 
 		case OTRL_MSGSTATE_ENCRYPTED:
-		    extrakey = gcry_malloc_secure(OTRL_EXTRAKEY_BYTES);
-		    err = otrl_proto_accept_data(&plaintext, &tlvs, context,
-				    message, &flags, extrakey);
-		    if (err) {
-			int is_conflict =
-				(gpg_err_code(err) == GPG_ERR_CONFLICT);
-			if ((flags & OTRL_MSGFLAGS_IGNORE_UNREADABLE)) {
-			    edata.ignore_message = 1;
-			    break;
-			}
-			if (is_conflict) {
-			    if (ops->handle_msg_event) {
-				ops->handle_msg_event(opdata,
-					OTRL_MSGEVENT_RCVDMSG_UNREADABLE,
-					context, NULL,
-					gcry_error(GPG_ERR_NO_ERROR));
-			    }
-			} else {
-			    if (ops->handle_msg_event) {
-				ops->handle_msg_event(opdata,
-					OTRL_MSGEVENT_RCVDMSG_MALFORMED,
-					context, NULL,
-					gcry_error(GPG_ERR_NO_ERROR));
-			    }
-			}
-			if (ops->inject_message && ops->otr_error_message) {
-			    err_msg = ops->otr_error_message(opdata,
-					context,
-					is_conflict ?
-					    OTRL_ERRCODE_MSG_UNREADABLE :
-					    OTRL_ERRCODE_MSG_MALFORMED);
-			    if (err_msg) {
-				buf = malloc(strlen(OTR_ERROR_PREFIX) +
-						strlen(err_msg) + 1);
-				if (buf) {
-				    strcpy(buf, OTR_ERROR_PREFIX);
-				    strcat(buf, err_msg);
-				    ops->inject_message(opdata,
-					    accountname, protocol,
-					    sender, buf);
-				    free(buf);
-				}
-			    }
-			    if (ops->otr_error_message_free) {
-				ops->otr_error_message_free(opdata,
-					err_msg);
-			    }
-			}
-			edata.ignore_message = 1;
-			break;
-		    }
+                    extrakey = gcry_malloc_secure(OTRL_EXTRAKEY_BYTES);
+                    err = otrl_proto_accept_data(&plaintext, &tlvs, context,
+                                                 message, &flags, extrakey);
+                    if (err) {
+                        gcry_free(extrakey);
+                        edata.ignore_message = 1;
+                        if (flags & OTRL_MSGFLAGS_IGNORE_UNREADABLE) {
+                            break;
+                        }
+                        if (gpg_err_code(err) == GPG_ERR_CONFLICT) {
+                            report_error(ops, opdata, OTRL_MSGEVENT_RCVDMSG_UNREADABLE,
+                                        OTRL_ERRCODE_MSG_UNREADABLE, context);
+                        } else {
+                            report_error(ops, opdata, OTRL_MSGEVENT_RCVDMSG_MALFORMED,
+                                         OTRL_ERRCODE_MSG_MALFORMED, context);
+                        }
+                        break;
+                    }
 
 		    /* If the other side told us he's disconnected his
 		     * private connection, make a note of that so we
-- 
2.1.2



More information about the OTR-dev mailing list