Skip to content

Commit

Permalink
JENKINS-31200 HexUtils fails to parse values over max int
Browse files Browse the repository at this point in the history
  • Loading branch information
Thierry Lacour committed Oct 28, 2015
1 parent ecfe709 commit ec8aa0d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 74 deletions.
Expand Up @@ -92,50 +92,6 @@ public String getUrlName() {
return null;
}

/**
* Returns an indication whether as to the requirements are met. You do one
* check per set of values you wish to compare.
*
* @param threshold
* @param valuenames
* @return
*/
public boolean validateThreshold(int threshold, String... valuenames) {
return sumOfValues(valuenames) <= threshold;
}

public boolean validateThreshold(int threshold, List<String> valuenames) {
return sumOfValues(valuenames) <= threshold;
}

public int sumOfValues(String... valuenames) {
int sum = 0;
/*
for(MemoryMapParsingResult res : getResults()) {
for(String s : valuenames) {
if(res.getName().equals(s)) {
sum+=res.getValue();
}
}
}
*/
return sum;
}

public int sumOfValues(List<String> values) {
int sum = 0;
/*
for(MemoryMapParsingResult res : getResults()) {
for(String s : values) {
if(res.getName().equals(s)) {
sum+=res.getValue();
}
}
}
*/
return sum;
}

/**
* Fetches the previous MemoryMap build. Takes all successful, but failed
* builds.
Expand Down Expand Up @@ -186,30 +142,28 @@ public MemoryMapBuildAction getPreviousAction() {
* remove the marker, and concatenate the label.
*/
private void filterMarkers(HashMap<String, ValueMarker> markers) {

HashMap<Integer, String> maxLabel = new HashMap<Integer, String>();
HashMap<Long, String> maxLabel = new HashMap<>();

for (String markerLabel : markers.keySet()) {
int max = (int) markers.get(markerLabel).getValue();
long max = (long) markers.get(markerLabel).getValue();
if (maxLabel.containsKey(max)) {
//If the label already is contained. Store the Original value. And add the new one.
String current = maxLabel.get(max);

maxLabel.put(max, current + " " + markerLabel);
} else {
maxLabel.put(max, markerLabel);
}
}

markers.clear();
for (Integer key : maxLabel.keySet()) {
for (Long key : maxLabel.keySet()) {
makeMarker(maxLabel.get(key), (double) key, markers);
}

}

public void doDrawMemoryMapUsageGraph(StaplerRequest req, StaplerResponse rsp) throws IOException {
DataSetBuilder<String, ChartUtil.NumberOnlyBuildLabel> dataset = new DataSetBuilder<String, ChartUtil.NumberOnlyBuildLabel>();
DataSetBuilder<String, ChartUtil.NumberOnlyBuildLabel> dataset = new DataSetBuilder<>();

String members = req.getParameter("categories");
String graphTitle = req.getParameter("title");
Expand All @@ -220,7 +174,7 @@ public void doDrawMemoryMapUsageGraph(StaplerRequest req, StaplerResponse rsp) t

List<String> memberList = Arrays.asList(members.split(","));

HashMap<String, ValueMarker> markers = new HashMap<String, ValueMarker>();
HashMap<String, ValueMarker> markers = new HashMap<>();

String scale = getRecorder().scale;

Expand Down
Expand Up @@ -121,8 +121,8 @@ public class MemoryMapMemItemComparator implements Comparator<MemoryMapConfigMem

@Override
public int compare(MemoryMapConfigMemoryItem t, MemoryMapConfigMemoryItem t1) {
int vt = new HexifiableString(t.getOrigin()).getIntegerValue();
int vt1 = new HexifiableString(t1.getOrigin()).getIntegerValue();
long vt = new HexifiableString(t.getOrigin()).getLongValue();
long vt1 = new HexifiableString(t1.getOrigin()).getLongValue();
return (vt < vt1 ? -1 : (vt == vt1 ? 1 : 0));
}

Expand Down
32 changes: 16 additions & 16 deletions src/main/java/net/praqma/jenkins/memorymap/util/HexUtils.java
Expand Up @@ -36,12 +36,12 @@ public class HexUtils {
private static final int HEXA_RADIX = 16;
private static final int BITS_PER_BYTE = 8;

private static final double DEFAULT = 1d;
private static final double KILO = 1024;
private static final double MEGA = KILO*1024;
private static final double GIGA = MEGA*1024;
private static final long DEFAULT = 1;
private static final long KILO = 1024;
private static final long MEGA = KILO*1024;
private static final long GIGA = MEGA*1024;

private static final Map<String, Double> scale = new HashMap<>();
private static final Map<String, Long> scale = new HashMap<>();
private static final Pattern VALID_HEX = Pattern.compile("^[0xX]*[0-9a-fA-F]+$");
private static final Pattern VALID_NUMERICAL = Pattern.compile("^\\d+[mMgGkK]+$");

Expand All @@ -61,7 +61,7 @@ public static double byteCount(String hexString, int wordSize, String scale) {
}

private static double getRadix(String hexString, int radix) {
Double i = (double)(Integer.parseInt(hexString.replace("0x","").replaceAll("\\s", ""), radix));
Double i = (double)(Long.parseLong(hexString.replace("0x","").replaceAll("\\s", ""), radix));
return i;
}

Expand All @@ -73,12 +73,12 @@ public HexifiableString(String rawString) {
this.rawString = rawString.replaceAll("\\s", "");
}

public HexifiableString(Integer value) {
this.rawString = Integer.toHexString(value);
public HexifiableString(Long value) {
this.rawString = Long.toHexString(value);
}

public Integer getIntegerValue() {
return Integer.parseInt(rawString.replace("0x",""), 16);
public Long getLongValue() {
return Long.parseLong(rawString.replace("0x",""), 16);
}

public boolean isValidMetricValue() {
Expand All @@ -100,11 +100,11 @@ private HexifiableString convertToHexForm() {
} else {
HexifiableString newString = null;
if (rawString.contains("M") || rawString.contains("m")) {
newString = new HexifiableString(Integer.parseInt(rawString.replaceAll("[mM]", ""))*(int)MEGA);
newString = new HexifiableString(Long.parseLong(rawString.replaceAll("[mM]", "")) * MEGA);
} else if (rawString.contains("G") || rawString.contains("g")) {
newString = new HexifiableString(Integer.parseInt(rawString.replaceAll("[gG]", ""))*(int)GIGA);
newString = new HexifiableString(Long.parseLong(rawString.replaceAll("[gG]", ""))* GIGA);
} else if (rawString.contains("K") || rawString.contains("k")) {
newString = new HexifiableString(Integer.parseInt(rawString.replaceAll("[kK]", ""))*(int)KILO);
newString = new HexifiableString(Long.parseLong(rawString.replaceAll("[kK]", ""))* KILO);
} else {
throw new UnsupportedOperationException(String.format("The string %s contains invalid metric symbols", rawString));
}
Expand All @@ -121,14 +121,14 @@ public HexifiableString toValidHexString() {
}

public HexUtils.HexifiableString getLengthAsHex(HexifiableString other) {
int diff = Math.abs(getIntegerValue() - other.getIntegerValue());
long diff = Math.abs(getLongValue() - other.getLongValue());
return new HexUtils.HexifiableString(diff);
}

@Override
public int compareTo(HexifiableString t) {
int current = Integer.parseInt(rawString.trim().replace("0x",""), 16);
int other = Integer.parseInt(t.rawString.trim().replace("0x",""), 16);
long current = Long.parseLong(rawString.trim().replace("0x",""), 16);
long other = Long.parseLong(t.rawString.trim().replace("0x",""), 16);

if (other > current) {
return 1;
Expand Down
12 changes: 7 additions & 5 deletions src/test/java/net/praqma/jenkins/unit/HexUtilsTest.java
Expand Up @@ -164,12 +164,14 @@ public void testFormatting() {
assertEquals("Failure for test '" + test.getKey() + "': Expected formatted hex string.", test.getValue(), hexString.toFormattedHexString().rawString);
}

Map<Integer, String> intTests = new HashMap<Integer,String>(){{
put(1,"0x00000001");
put(316, "0x0000013c");
put(2000000000, "0x77359400");
Map<Long, String> nrTests = new HashMap<Long,String>(){{
put(1L,"0x00000001");
put(316L, "0x0000013c");
put(2000000000L, "0x77359400");
put(1095216660480L, "0x000000ff00000000"); //JENKINS-31200

}};
for (Map.Entry<Integer, String> test : intTests.entrySet()) {
for (Map.Entry<Long, String> test : nrTests.entrySet()) {
HexUtils.HexifiableString hexString = new HexUtils.HexifiableString(test.getKey()).toValidHexString();
assertTrue("Failure for test '" + test.getKey() + "': Not a valid hex string.", hexString.isValidHexString());
assertEquals("Failure for test '" + test.getKey() + "': Expected formatted hex string.", test.getValue(), hexString.toFormattedHexString().rawString);
Expand Down

0 comments on commit ec8aa0d

Please sign in to comment.