Program 1

import java.util.*;
public class Overflow {
  static final int INPUT_SIZE=10;
  public static void main(String[] args) {
    char[] vals = new char[INPUT_SIZE];

    Scanner scan = new Scanner(System.in);
    String s1 = getString(scan);

    copyVals(s1,vals);

    String sub = getSubstring(scan,vals);
    System.out.println("sub string: "+sub);
  }

  public static String getString(Scanner scan) {
    System.out.print("Please type a string: ");
    String s = scan.nextLine();
    return s;
  }

  public static void copyVals(String s,char[] vals) {
    for (int i = 0; i < s.length(); i++) {
      vals[i] = s.charAt(i);
    }
  }

  public static String getSubstring(Scanner scan,char[] vals) {
    System.out.print("Starting point: ");
    int start = scan.nextInt();
    System.out.print("Ending point: ");
    int end = scan.nextInt();

    char[] newChars = getChars(start,end,vals);
    return new String(newChars);
   }
public static char[] getChars(int start,int end,char[] vals) {

    int sz = end-start;
    char[] result = new char[sz];
    for (int i=0; i< sz; i++){
        result[i] = vals[start+i];
    }
    return result;
  }
}

Security Checklist

Vulnerability:BufferOverflowCourse:CS2  
Task – Check each line of code Completed
 1. Finding Arrays:  
1.1 Underline each array declaration  
1.2 For each array, underline all subsequent references  
2. Index Variables – legal range for an array of size n is 0 <= i < n  
2.1 For each underlined access that uses a variable as an index, write the legal range next to it.  
2.2 For each index marked in 2.1, underline all occurrences of that variable.  
2.3. Mark with a V any assignments, inputs or operations that may modify these index variables.  
3. Loops that modify index variables  
3.1 Find loops that modify variables used to index arrays. For any index that occurs as part of a loop conditional, underline the loop limit. For example, if i < max is the conditional in a for loop, underline max  
3.2. Write the legal range of the array index next to the loop limit as you did in step 2.1. Mark with a V if the loop limit could exceed the legal range of the array index. Watch out for loop that go until i <=max , as the largest valid index is max-1  
3.3 If the upper or lower loop limit is a variable, it must be checked just as indices are checked in Step 2  
Highlighted areas indicate vulnerabilities!  
 
Copyright © Towson University