eclipse-ee4j / openmq

OpenMQ
https://projects.eclipse.org/projects/ee4j.openmq/
Other
52 stars 34 forks source link

JMSException is not caused when invalid argument is set for MapMessage class setBytes method #330

Closed glassfishrobot closed 5 months ago

glassfishrobot commented 11 years ago

When invalid values are set for setBytes method of MapMessage, JMSException is not caused.

void setBytes(String name,byte[] value,int offset,int length)

I found a same bug in writeBytes method in StreamMessage as well.

void writeBytes(byte[] value, int offset, int length)

Environment

glassfish-4.0.1-b02-07_24_2013 Windows

glassfishrobot commented 6 years ago
glassfishrobot commented 11 years ago

@glassfishrobot Commented Reported by tak09

glassfishrobot commented 11 years ago

@glassfishrobot Commented tak09 said: https://www.dropbox.com/sh/jk2re1abamtmg37/vwJQ6DQwfv

Test program.

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.MessageProducer;
import javax.jms.MapMessage;
import javax.jms.StreamMessage;
import javax.jms.JMSException;
import javax.annotation.Resource;

public class ProducerMapMessageSetBytes {
    @Resource(mappedName = "jms/ConnectionFactory")
    private static ConnectionFactory connectionFactory;
    @Resource(mappedName = "jms/Queue")
    private static Queue queue;

    public static void main(String[] args) {
        Connection connection = null;
        Destination dest = null;

        try {
            dest = (Destination) queue;
        } catch (Exception e) {
            System.err.println("Error setting destination: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }

        MapMessage mapMessage = null;
        StreamMessage streamMessage = null;
        MessageProducer producer = null;

        try {
            connection = connectionFactory.createConnection();

            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);

            producer = session.createProducer(dest);
            mapMessage = session.createMapMessage();
            streamMessage = session.createStreamMessage();

        } catch (JMSException e) {
            System.err.println("JMSException occurred: " + e.toString());
        } 

        String str10 = new String("abcdfghijk");
        byte[] b = str10.getBytes();
        System.out.println("Size of byte[] b is "+ b.length );
        System.out.println("mapMessage.setBytes(\"name\", b , 0, 11)");
        // void setBytes(String name,byte[] value,int offset,int length)
        // ArrayIndexOutOfBoundsException is caused when value < length.
        // It should cause JMSException instead.
        try {
            mapMessage.setBytes("name", b , 0, 11);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        System.out.println("mapMessage.setBytes(\"name\", b , 0, -1)");
        // void setBytes(String name,byte[] value,int offset,int length)
        // When -1 is specified for length, NagativeArraySizeException is caused. 
        // It should cause JMSException instead.
        try {
            mapMessage.setBytes("name", b , 0, -1);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        // Same bug for streamMessage 
        System.out.println("streamMessage.writetBytes(b , 0, 11)");
        try {
            streamMessage.writeBytes(b , 0, 11);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        // Same bug for streamMessage 
        System.out.println("streamMessage.writeBytes(b , 0, -1)");
        try {
            streamMessage.writeBytes(b , 0, -1);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        try {
            producer.send(mapMessage);
        } catch (JMSException e1) {
            e1.printStackTrace();
        }

        try {
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}
glassfishrobot commented 11 years ago

@glassfishrobot Commented @nigeldeakin said: The JMS spec doesn't define what should happen in this case (though it is clearly invalid and an exception of some kind should result). Throwing a JMSException would probably be valid, but throwing a more specific unchecked exception would be valid too, in my opinion.

As a general policy, I don't think the existence of JMSException should force the provider to catch every possible unchecked exception (e.g. java.lang.OutOfMemoryError) and wrap it in a JMSException. The only cases where a JMSException must be thrown are in the cases listed in the API documentation, which relate to using an inappropriate method to read data from the byte array.

glassfishrobot commented 7 years ago

@glassfishrobot Commented This issue was imported from java.net JIRA MQ-330

pzygielo commented 5 months ago

Closing with https://github.com/eclipse-ee4j/openmq/issues/330#issuecomment-364671002