Skip to content

Commit f11a8cf

Browse files
authored
HADOOP-13147. Constructors must not call overrideable methods in PureJavaCrc32C (#6408). Contributed by Sebb.
1 parent 47be1ab commit f11a8cf

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/PureJavaCrc32C.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class PureJavaCrc32C implements Checksum {
3939

4040
/** Create a new PureJavaCrc32 object. */
4141
public PureJavaCrc32C() {
42-
reset();
42+
resetFinal(); // safe to call as it cannot be overridden
4343
}
4444

4545
@Override
@@ -50,6 +50,12 @@ public long getValue() {
5050

5151
@Override
5252
public void reset() {
53+
resetFinal();
54+
}
55+
56+
// This must be final as it is called by the ctor
57+
// (can't also be private, as checkstyle then complains)
58+
final void resetFinal() {
5359
crc = 0xffffffff;
5460
}
5561

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.util;
19+
20+
import java.util.zip.Checksum;
21+
22+
import org.junit.Test;
23+
24+
import static org.junit.Assert.assertEquals;
25+
26+
27+
public class TestPureJavaCrc32C {
28+
29+
@Test
30+
public void testChecksumInit() {
31+
Checksum csum = new PureJavaCrc32C();
32+
long crc1 = csum.getValue();
33+
csum.reset();
34+
long crc2 = csum.getValue();
35+
assertEquals("reset should give same as initial value", crc1, crc2);
36+
}
37+
}

0 commit comments

Comments
 (0)