<commit_before>/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2005, University of Maryland
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.detect;


import org.apache.bcel.Repository;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.JavaClass;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;
import edu.umd.cs.findbugs.OpcodeStack;
import edu.umd.cs.findbugs.OpcodeStack.Item;

public class FindPuzzlers extends BytecodeScanningDetector {


	BugReporter bugReporter;
	public FindPuzzlers(BugReporter bugReporter) {
		this.bugReporter =  bugReporter;
	}



	@Override
         public void visit(Code obj) {
		prevOpcodeIncrementedRegister = -1;
		best_priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG = priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG = LOW_PRIORITY+1;
		prevOpCode = NOP;
		stack.resetForMethodEntry(this);
		badlyComputingOddState = 0;
		super.visit(obj);
	}

	int priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG;
	int prevOpcodeIncrementedRegister;
	int valueOfConstantArgumentToShift;
	int best_priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG ;
	boolean constantArgumentToShift;
	
	int badlyComputingOddState;
	int prevOpCode;
	OpcodeStack stack = new OpcodeStack();
	
	private int adjustPriority(Object constant, int priority) {
		if (!(constant instanceof Integer)) return priority;
		int value = Math.abs(((Integer) constant).intValue());
		if (value <= 4) return LOW_PRIORITY+2;
		if (value <= 10000) return priority+1;
		if (value <= 60*60*1000) return priority;
		return priority-1;
	}
	@Override
         public void sawOpcode(int seen) {
		stack.mergeJumps(this);
		
		if (seen == IMUL) {
			priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG = NORMAL_PRIORITY;
			if (stack.getStackDepth() > 1) {
				OpcodeStack.Item item0 = stack.getStackItem(0);
				OpcodeStack.Item item1 = stack.getStackItem(1);
				priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG = adjustPriority(item0.getConstant(), priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG);
				priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG = adjustPriority(item1.getConstant(), priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG);
				if (priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG >= LOW_PRIORITY && (item0.isInitialParameter() || item1.isInitialParameter()))
					priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG = NORMAL_PRIORITY;
			}}
		if (prevOpCode == IMUL && seen == I2L)
			if (priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG <= best_priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG) {
				best_priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG = priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG;
			bugReporter.reportBug(new BugInstance(this, 
					"ICAST_INTEGER_MULTIPLY_CAST_TO_LONG", 
					priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG)
						.addClassAndMethod(this)
						.addSourceLine(this));
			}
		
		if (getMethodName().equals("<clinit>") && (seen == PUTSTATIC || seen == GETSTATIC || seen == INVOKESTATIC)) {
			 String clazz = getClassConstantOperand();
			 if (!clazz.equals(getClassName())) {
				 try {
					 JavaClass targetClass = Repository.lookupClass(clazz);
					if (Repository.instanceOf(targetClass, getThisClass())) {
						int priority = NORMAL_PRIORITY;
						if (seen == GETSTATIC) priority--;
						if (!targetClass.isPublic()) priority++;
						 bugReporter.reportBug(new BugInstance(this, 
								 "IC_SUPERCLASS_USES_SUBCLASS_DURING_INITIALIZATION", 
								priority)
						 .addClassAndMethod(this).addClass(getDottedClassConstantOperand())
						 .addSourceLine(this)
							);
					}
				} catch (ClassNotFoundException e) {
					// ignore it
				}
				 
			 }
		}
         if (false && (seen == INVOKEVIRTUAL)
                &&   getNameConstantOperand().equals("equals")
                &&   getSigConstantOperand().equals("(Ljava/lang/Object;)Z")
		&& stack.getStackDepth() > 1) {
			OpcodeStack.Item item0 = stack.getStackItem(0);<commit_msg>check for code like days*24*60*60*1000 for ICAST_IMUL_TO_LONG<commit_after>/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2005, University of Maryland
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.detect;


import org.apache.bcel.Repository;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.JavaClass;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;
import edu.umd.cs.findbugs.OpcodeStack;
import edu.umd.cs.findbugs.OpcodeStack.Item;

public class FindPuzzlers extends BytecodeScanningDetector {


	BugReporter bugReporter;
	public FindPuzzlers(BugReporter bugReporter) {
		this.bugReporter =  bugReporter;
	}



	@Override
         public void visit(Code obj) {
		prevOpcodeIncrementedRegister = -1;
		best_priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG = LOW_PRIORITY+1;
		prevOpCode = NOP;
		stack.resetForMethodEntry(this);
		badlyComputingOddState = 0;
		resetIMulCastLong();
		imul_distance = 10000;
		super.visit(obj);
	}

	int imul_constant;
	int imul_distance;
	boolean imul_operand_is_parameter;
	int prevOpcodeIncrementedRegister;
	int valueOfConstantArgumentToShift;
	int best_priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG ;
	boolean constantArgumentToShift;
	
	int badlyComputingOddState;
	int prevOpCode;
	OpcodeStack stack = new OpcodeStack();
	
	private void resetIMulCastLong() {
		imul_constant = 1;
		imul_operand_is_parameter = false;
	}
	private int adjustPriority(int factor, int priority) {
		if (factor <= 4) return LOW_PRIORITY+2;
		if (factor <= 10000) return priority+1;
		if (factor <= 60*60*1000) return priority;
		return priority-1;
	}
	private int adjustMultiplier(Object constant, int mul) {
		if (!(constant instanceof Integer)) return mul;
		return Math.abs(((Integer) constant).intValue()) * mul;
		
	}
	@Override
         public void sawOpcode(int seen) {
		stack.mergeJumps(this);
		
		if (seen == IMUL) {
			if (imul_distance != 1) resetIMulCastLong();
			imul_distance = 0;
			if (stack.getStackDepth() > 1) {
				OpcodeStack.Item item0 = stack.getStackItem(0);
				OpcodeStack.Item item1 = stack.getStackItem(1);
				imul_constant = adjustMultiplier(item0.getConstant(), imul_constant);
				imul_constant = adjustMultiplier(item1.getConstant(), imul_constant);

				if (item0.isInitialParameter() || item1.isInitialParameter())
					imul_operand_is_parameter = true;
			}} else {
				imul_distance++;
			}
		
		if (prevOpCode == IMUL && seen == I2L) {
			int priority = adjustPriority(imul_constant, NORMAL_PRIORITY);
			if (priority >= LOW_PRIORITY && imul_operand_is_parameter) priority = NORMAL_PRIORITY;
			if (priority <= best_priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG) {
				best_priority_for_ICAST_INTEGER_MULTIPLY_CAST_TO_LONG = priority;
			bugReporter.reportBug(new BugInstance(this, 
					"ICAST_INTEGER_MULTIPLY_CAST_TO_LONG", 
					priority)
						.addClassAndMethod(this)
						.addSourceLine(this));
			}
		}
		
		if (getMethodName().equals("<clinit>") && (seen == PUTSTATIC || seen == GETSTATIC || seen == INVOKESTATIC)) {
			 String clazz = getClassConstantOperand();
			 if (!clazz.equals(getClassName())) {
				 try {
					 JavaClass targetClass = Repository.lookupClass(clazz);
					if (Repository.instanceOf(targetClass, getThisClass())) {
						int priority = NORMAL_PRIORITY;
						if (seen == GETSTATIC) priority--;
						if (!targetClass.isPublic()) priority++;
						 bugReporter.reportBug(new BugInstance(this, 
								 "IC_SUPERCLASS_USES_SUBCLASS_DURING_INITIALIZATION", 
								priority)
						 .addClassAndMethod(this).addClass(getDottedClassConstantOperand())
						 .addSourceLine(this)
							);
					}
				} catch (ClassNotFoundException e) {
					// ignore it
				}
				 
			 }
		}
         if (false && (seen == INVOKEVIRTUAL)
                &&   getNameConstantOperand().equals("equals")
                &&   getSigConstantOperand().equals("(Ljava/lang/Object;)Z")
		&& stack.getStackDepth() > 1) {
			OpcodeStack.Item item0 = stack.getStackItem(0);
{'fraction_non_alphanumeric': 0.09488821244882964, 'fraction_numerical': 0.013015639760680172, 'mean_word_length': 4.311036789297659, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1888802', 'n_tokens_mistral': 3551, 'n_tokens_neox': 3057, 'n_words': 692}