Fix: Correctly compute Java agent list loggers response size
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Thu, 19 May 2016 19:59:50 +0000 (15:59 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 19 May 2016 22:46:19 +0000 (18:46 -0400)
The code was assuming that (number of characters == number of bytes),
which is not always the case!

The notions of number of bytes and data sizes only make sense when
the strings are encoded into byte arrays. The response's getBytes()
method should the only one handling these concepts.

Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondCommand.java
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java

index 7cde4a3598a516c5cbcd1beb4562e03ce4ea9475..8b04f4bb38e658d0907451f9c02ea8854c8372d0 100644 (file)
@@ -82,17 +82,17 @@ abstract class SessiondCommand {
         *         formatted.
         */
        protected static String readNextString(ByteBuffer buffer) {
-               int length = buffer.getInt();
-               if (length < 0) {
+               int nbBytes = buffer.getInt();
+               if (nbBytes < 0) {
                        /* The string length should be positive */
                        return null;
                }
-               if (length == 0) {
+               if (nbBytes == 0) {
                        /* The string is explicitly an empty string */
                        return "";
                }
 
-               byte[] stringBytes = new byte[length];
+               byte[] stringBytes = new byte[nbBytes];
                buffer.get(stringBytes);
                return new String(stringBytes, SESSIOND_PROTOCOL_CHARSET).trim();
        }
index 1c7ef9b400f019e7611c36b5bb349c99cc628b5d..4dee6ae406c954466899357c0020257e41ddc49f 100644 (file)
@@ -34,13 +34,7 @@ class SessiondListLoggersCommand extends SessiondCommand {
        @Override
        public LttngAgentResponse execute(ILttngTcpClientListener agent) {
                final Collection<String> loggerList = agent.listAvailableEvents();
-               int dataSize = 0;
-
-               for (String event : agent.listAvailableEvents()) {
-                       dataSize += event.length() + 1;
-               }
-
-               return new SessiondListLoggersResponse(loggerList, dataSize);
+               return new SessiondListLoggersResponse(loggerList);
        }
 
        private static class SessiondListLoggersResponse extends LttngAgentResponse {
@@ -48,11 +42,9 @@ class SessiondListLoggersCommand extends SessiondCommand {
                private final static int SIZE = 12;
 
                private final Collection<String> loggers;
-               private final int dataSize;
 
-               public SessiondListLoggersResponse(Collection<String> loggers, int dataSize) {
+               public SessiondListLoggersResponse(Collection<String> loggers) {
                        this.loggers = loggers;
-                       this.dataSize = dataSize;
                }
 
                @Override
@@ -63,15 +55,26 @@ class SessiondListLoggersCommand extends SessiondCommand {
 
                @Override
                public byte[] getBytes() {
+                       /*
+                        * Compute the data size, which is the number of bytes of each
+                        * encoded string, +1 per string for the \0
+                        */
+                       int dataSize = 0;
+                       for (String logger : loggers) {
+                               dataSize += logger.getBytes(SESSIOND_PROTOCOL_CHARSET).length + 1;
+                       }
+
+                       /* Prepare the buffer */
                        byte data[] = new byte[SIZE + dataSize];
                        ByteBuffer buf = ByteBuffer.wrap(data);
                        buf.order(ByteOrder.BIG_ENDIAN);
 
-                       /* Returned code */
+                       /* Write the header section of the response */
                        buf.putInt(getReturnCode().getCode());
                        buf.putInt(dataSize);
                        buf.putInt(loggers.size());
 
+                       /* Write the payload */
                        for (String logger : loggers) {
                                buf.put(logger.getBytes(SESSIOND_PROTOCOL_CHARSET));
                                /* NULL terminated byte after the logger name. */
This page took 0.026097 seconds and 4 git commands to generate.