activemq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Endre Stølsvik (JIRA) <j...@apache.org>
Subject [jira] [Updated] (AMQ-5632) MapMessage.getBytes crashes with NPE if no value present
Date Tue, 03 Mar 2015 14:32:04 GMT

     [ https://issues.apache.org/jira/browse/AMQ-5632?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Endre Stølsvik updated AMQ-5632:
--------------------------------
    Description: 
Compare to AMQ-5628 and AMQ-5629

Read up on http://docs.oracle.com/javaee/1.4/api/javax/jms/MapMessage.html

Notice the wording "if there is no item by this name, a null value is returned."

ActiveMqMapMessage, line ~494:
{code}
    /**
     * Returns the byte array value with the specified name.
     *
     * @param name the name of the byte array
     * @return a copy of the byte array value with the specified name; if there
     *         is no item by this name, a null value is returned.
     * @throws JMSException if the JMS provider fails to read the message due to
     *                 some internal error.
     * @throws MessageFormatException if this type conversion is invalid.
     */
    @Override
    public byte[] getBytes(String name) throws JMSException {
        initializeReading();
        Object value = map.get(name);
        if (value instanceof byte[]) {
            return (byte[])value;
        } else {
            throw new MessageFormatException(" cannot read a byte[] from " + value.getClass().getName());
        }
    }
{code}

Notice how the "else"-block will kick in on null value, whereby we get the "value.getClass()"
executed when inside the constructor argument creation of the wrongly thrown MessageFormatException,
resulting in NPE.

Instead, a code block like every other getter has should be employed, here from getString
right above:
{code}
    public String getString(String name) throws JMSException {
        initializeReading();
        Object value = map.get(name);
        if (value == null) {
            return null;
        }
        if (value instanceof byte[]) {
            throw new MessageFormatException("Use getBytes to read a byte array");
        } else {
            return value.toString();
        }
    }
{code}

  was:
Compare to AMQ-5628 and AMQ-5629

Read up on http://docs.oracle.com/javaee/1.4/api/javax/jms/MapMessage.html

Notice the wording "if there is no item by this name, a null value is returned."

{code}
    /**
     * Returns the byte array value with the specified name.
     *
     * @param name the name of the byte array
     * @return a copy of the byte array value with the specified name; if there
     *         is no item by this name, a null value is returned.
     * @throws JMSException if the JMS provider fails to read the message due to
     *                 some internal error.
     * @throws MessageFormatException if this type conversion is invalid.
     */
    @Override
    public byte[] getBytes(String name) throws JMSException {
        initializeReading();
        Object value = map.get(name);
        if (value instanceof byte[]) {
            return (byte[])value;
        } else {
            throw new MessageFormatException(" cannot read a byte[] from " + value.getClass().getName());
        }
    }
{code}

Notice how the "else"-block will kick in on null value, whereby we get the "value.getClass()"
executed, resulting in NPE.


> MapMessage.getBytes crashes with NPE if no value present
> --------------------------------------------------------
>
>                 Key: AMQ-5632
>                 URL: https://issues.apache.org/jira/browse/AMQ-5632
>             Project: ActiveMQ
>          Issue Type: Bug
>            Reporter: Endre Stølsvik
>
> Compare to AMQ-5628 and AMQ-5629
> Read up on http://docs.oracle.com/javaee/1.4/api/javax/jms/MapMessage.html
> Notice the wording "if there is no item by this name, a null value is returned."
> ActiveMqMapMessage, line ~494:
> {code}
>     /**
>      * Returns the byte array value with the specified name.
>      *
>      * @param name the name of the byte array
>      * @return a copy of the byte array value with the specified name; if there
>      *         is no item by this name, a null value is returned.
>      * @throws JMSException if the JMS provider fails to read the message due to
>      *                 some internal error.
>      * @throws MessageFormatException if this type conversion is invalid.
>      */
>     @Override
>     public byte[] getBytes(String name) throws JMSException {
>         initializeReading();
>         Object value = map.get(name);
>         if (value instanceof byte[]) {
>             return (byte[])value;
>         } else {
>             throw new MessageFormatException(" cannot read a byte[] from " + value.getClass().getName());
>         }
>     }
> {code}
> Notice how the "else"-block will kick in on null value, whereby we get the "value.getClass()"
executed when inside the constructor argument creation of the wrongly thrown MessageFormatException,
resulting in NPE.
> Instead, a code block like every other getter has should be employed, here from getString
right above:
> {code}
>     public String getString(String name) throws JMSException {
>         initializeReading();
>         Object value = map.get(name);
>         if (value == null) {
>             return null;
>         }
>         if (value instanceof byte[]) {
>             throw new MessageFormatException("Use getBytes to read a byte array");
>         } else {
>             return value.toString();
>         }
>     }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message