Uploaded image for project: 'Cassandra'
  1. Cassandra
  2. CASSANDRA-14752

serializers/BooleanSerializer.java is using static bytebuffers which may cause problem for subsequent operations

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Patch Available
    • Priority: Normal
    • Resolution: Unresolved
    • Fix Version/s: 4.x
    • Component/s: Legacy/Core
    • Labels:
      None
    • Severity:
      Normal

      Description

      https://github.com/apache/cassandra/blob/trunk/src/java/org/apache/cassandra/serializers/BooleanSerializer.java#L26 It has two static Bytebuffer variables:-

      private static final ByteBuffer TRUE = ByteBuffer.wrap(new byte[]{1});
      private static final ByteBuffer FALSE = ByteBuffer.wrap(new byte[]{0});

      What will happen if the position of these Bytebuffers is being changed by some other operations? It'll affect other subsequent operations. IMO Using static is not a good idea here.

      A potential place where it can become problematic: https://github.com/apache/cassandra/blob/cassandra-2.1.13/src/java/org/apache/cassandra/db/marshal/AbstractCompositeType.java#L243 Since we are calling `.remaining()` It may give wrong results i.e 0 if these Bytebuffers have been used previously.

      Solution:
      https://github.com/apache/cassandra/blob/trunk/src/java/org/apache/cassandra/serializers/BooleanSerializer.java#L42 Every time we return new bytebuffer object. Please do let me know If there is a better way. I'd like to contribute. Thanks!!

      public ByteBuffer serialize(Boolean value)
      {
      return (value == null) ? ByteBufferUtil.EMPTY_BYTE_BUFFER
      : value ? ByteBuffer.wrap(new byte[] {1}) : ByteBuffer.wrap(new byte[] {0}); // false
      }
      

        Attachments

        1. patch-modified
          1 kB
          Varun Barala
        2. patch
          1 kB
          Varun Barala

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              varuna Varun Barala
            • Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated: