JDK-7190219 : (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2012-08-09
  • Updated: 2013-06-26
  • Resolved: 2012-09-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8
7u40Fixed 8 b55Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :


ADDITIONAL OS VERSION INFORMATION :
Linux  3.2.0-27-generic-pae #43-Ubuntu SMP i686 i686 i386 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
In CharBuffer.put(String,int,int), if the requested space is not enough the BufferOverflowException will be thrown and the CharBuffer will be left unchanged, but actually CharBuffer's position get modified even after BufferOverflowException.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached test case.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Quiet exit.
ACTUAL -
Exceptions.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.Exception: Buffer position should not changed!
	at CharBufferPutTest.run(CharBufferPutTest.java:57)
	at CharBufferPutTest.main(CharBufferPutTest.java:63)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/*
 * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * Portions Copyright (c) 2012 IBM Corporation
 */

/* @test
 * @summanry CharBuffer.put(String, int, int) changes position after exception
 * @bug
 */

import java.nio.BufferOverflowException;
import java.nio.CharBuffer;

public class CharBufferPutTest {

    CharBuffer buf;

    public CharBufferPutTest () {
        char[] chars = "123456789a".toCharArray();
        buf = CharBuffer.wrap(chars);
    }

    public void run() throws Exception {
        buf.clear();

        try {
            buf.put(String.valueOf(new char[buf.capacity() + 1]), 0,
                    buf.capacity() + 1);
            throw new Exception("Should throw BufferOverflowException");
        } catch (BufferOverflowException e) {
            // ignore
        }

        if (0 != buf.position()) {
            throw new Exception("Buffer position should not changed!");
        }
    }

    public static void main(String[] args) throws Exception {
        CharBufferPutTest test = new CharBufferPutTest();
        test.run();
    }
}

---------- END SOURCE ----------

Comments
EVALUATION http://hg.openjdk.java.net/jdk8/tl/jdk/rev/bf0c6f91bc22
13-08-2012

EVALUATION Yes, this is a bug as it should check the size before copying the characters. Easy fix.
09-08-2012