pointers – Buffer Overflow issue in C code

I’ve been writing the following bit of code, and someone had informed me that it runs of the risk of having a buffer overflow.

Now admittedly my knowledge of buffer overflows may be not be as robust as I would like, but I thought that a buffer overflow is when the data being written will not fit within the bounderies of the buffer and will spill over to adjacent memory location.

I assumed that the problem may be related to the fread, that it wouldn’t be a safe function to use, but reading through the documentation doesn’t seem to tell me that its unsafe like say strcpy() is compared to strncpy(). So I’m rather uncertain on where the problem could be located or how to handle it. And also if anyone has any suggestions on where I can go (or what book to read) that would help expand my knowledge on this subject or other vulnerability weaknesses, I would be appreciative.

bool readLong(FILE *f, long *n)
{
    unsigned char *ptr,tmp;

    if (fread(n,8,1,f) != 1)
        return false;

    ptr = (unsigned char *)n;
    tmp = ptr(0);
    ptr(0) = ptr(7);
    ptr(7) = tmp;
    tmp = ptr(1);
    ptr(1) = ptr(6);
    ptr(6) = tmp;
    tmp = ptr(2);
    ptr(2) = ptr(5);
    ptr(5) = tmp;
    tmp = ptr(3);
    ptr(3) = ptr(4);
    ptr(4) = tmp;

    return true;
}

oauth2 – Authorization Code: Is there a specific format it has to follow?

I am currently digging into OAuth 2.0 and especially the authorization code grant. What I couldn’t find from within RFC6749 is if the authorization code has to follow a specific format.

The RFC says that “[…] Authorization codes operate as plaintext bearer credentials”. Does that mean it is up to the authorization server to what form it takes? I mean is it just a random string that can be considered a one-time-password?

java – Bank Card Generator Code

This program is used to create a random bank card number. Card number is created using account type and client location for the first eight digits and the remaining 8 digits are completely random. I’m new to this site, but I believe it’s just for code review to get opinions on code. If i’m incorrect please let me know and I can take this down. Otherwise, feel free to give me feedback. Thanks!

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    private static ArrayList<String> storedCardNumbers = new ArrayList<String>();

    public static void main(String() args) {

        menu();

    }

    //create menu for checking list of cards or creating a new card
    public static void menu(){
        //used to check if client wants to exit program
        boolean exit = false;

        System.out.println("___________________n" +
                "Welcome to our bank!n" + "Choose an option below: n" +
                "1. Create new bank card.n" +
                "2. Check existing cards.n" +
                "3. Exit.");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        if(choice == 1) {
            createNewCard();
        } else if(choice == 2){
            System.out.println("-------------------------");
            System.out.println("There are " + storedCardNumbers.size() + " cards in the system");
            for (int i = 0; i < storedCardNumbers.size(); i++) {
                System.out.println(storedCardNumbers.get(i).toString());
            }
            System.out.println("-------------------------");
        } else if(choice == 3) {
            System.out.println("Thank you for coming in!");
            exit = true;
        } else {
                System.out.println("Incorrect choice.n" +
                        "Please choose a valid option: ");
                menu();
            }

        if(exit == false) {
            System.out.println("Would you like to do anything else?");
            menu();
        } else {

        }
        }


    //create new bank card with user params
    public static String createNewCard() {
//        String accountType = setCardAccountType();
//        String location = setAccountLocation();
        String cardNumber = getCardNumber(setCardAccountType(),setAccountLocation());
        System.out.println("New card successfully created.n" +
                "Your new bank card number is: n" +
                cardNumber);
        storedCardNumbers.add(cardNumber);
        return cardNumber;
        }

        public static String setAccountLocation() {

            String location = "other";

            System.out.println("What is your location?n" +
                    "1. US Eastn" +
                    "2. US Westn" +
                    "3. Hawaiin" +
                    "4. Alaskan" +
                    "5. Other.");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();

            if (choice == 1) {
                location.equalsIgnoreCase("useast");
            } else if (choice == 2) {
                location.equalsIgnoreCase("uswest");
            } else if (choice == 3) {
                location.equalsIgnoreCase("alaska");
            } else if (choice == 4) {
                location.equalsIgnoreCase("hawaii");
            } else if(choice == 5) {
                location.equalsIgnoreCase("other");
            }else {
                System.out.println("Entered invalid selection.n" +
                        "Please enter valid selection.");
                setCardAccountType();
            }

            return location;
        }

        //set the card account type for the creatNewCard method to use
        public static String setCardAccountType() {

            String accountType = "other";

            System.out.println("Which type of account do you have?n" +
                    "1. Checking.n" +
                    "2. Savings.n" +
                    "3. Credit.n" +
                    "4. Other.n" +
                    "Enter corresponding digit: ");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();

            if (choice == 1) {
                accountType.equalsIgnoreCase("checking");
            } else if (choice == 2) {
                accountType.equalsIgnoreCase("savings");
            } else if (choice == 3) {
                accountType.equalsIgnoreCase("credit");
            } else if (choice == 4) {
                accountType.equalsIgnoreCase("other");
            } else {
                System.out.println("Entered invalid selection.n" +
                        "Please enter valid selection.");
                setCardAccountType();
            }

            return accountType;
        }

    //create random number for card
    public static int getRandom(int min, int max) {
        return (int) (Math.random()*(max - min)) + min;
    }

    public static String getCardNumber(String accountType, String location) {
        //first set of four
        int accountKeySet;
        //second set of four
        int locationKeySet;
        //third set of four
        int lastEightFirst;
        //fourth set of four
        int lastEightSecond;

        //assign a value to each account type
        if(accountType.equalsIgnoreCase("checking")) {
            accountKeySet = 4400;
        } else if(accountType.equalsIgnoreCase("savings")) {
            accountKeySet = 4300;
        } else if(accountType.equalsIgnoreCase("credit")) {
            accountKeySet = 4200;
        } else {
            accountKeySet = 4000;
        }


        //Assign a value to each location of client
        if(location.equalsIgnoreCase("useast")) {
            locationKeySet = 1011;
        } else if(location.equalsIgnoreCase("uswest")) {
            locationKeySet = 2011;
        } else if (location.equalsIgnoreCase("Alaska")) {
            locationKeySet = 3011;
        } else if (location.equalsIgnoreCase("hawaii")) {
            locationKeySet = 4011;
        } else {
            locationKeySet = 1022;
        }

        //create a randomized number for last of card number
        lastEightFirst = getRandom(1000,9999);
        lastEightSecond = getRandom(1000,9999);

        //construct card number
        StringBuilder key = new StringBuilder();
        key.append(accountKeySet);
        key.append(" ");
        key.append(locationKeySet);
        key.append(" ");
        key.append(lastEightFirst);
        key.append(" ");
        key.append(lastEightSecond);
        return key.toString();
    }
}

jenkins – How to Get a list of job source code management and build for all jobs

I am looking for a way to get a list of build and source code management section in the configuration.

For Example: I have jobs with Testcomplete and SoupUI Pro plug ins, in each jobs, in the configuration section, we input the git repo name, testsuite name, directory etc …

Currently, I am having to go to each job, click on configuration and get the values I need, would be nice where I can get all this information for all the jobs. I looked in configuration slicing, but it does not have the section I need.

Thank you for your help in advance.

matrix – Matlab code to perform Gaussian elimination

I am creating a MATLAB function that would perform Gaussian elimination. My code script is shown below. However, the code is neither efficient nor correct.

   % function to perform row eliminination
function (x,U)=triangleform(A,b)
%FORWARD ELIMINATION
n=length(b);
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
      %compute the kth column of M
      m(k+1:n) = A(k+1:n,k)/A(k,k);
      %compute
      %An=Mn*An-1;
      %bn=Mn*bn-1;
      for i=k+1:n
          A(i, k+1:n) = A(i,k+1:n)-m(i)*A(k,k+1:n);
      end;
      b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end
U= triu(A);
%BACKWARD ELIMINATION
x(n)=b(n)/A(n,n);
for k =n-1:-1:1;
      b(1:k)=b(1:k)-x(k+1)* U(1:k,k+1);
      x(k)=b(k)/U(k,k);
end
end

Improving the code, events in individual functions (refactoring)

code

It is necessary from this piece of code

const rectPos = { x: 0, y: 0 };
const currPos = { x: 0, y: 0 };

const mouseEvent = () => {
canvas.onmousedown = (e) => {
    const start = { x: e.offsetX, y: e.offsetY };
        canvas.onmousemove = (e) => {
            currPos.x = getXPixelRatio * Math.round((rectPos.x + e.offsetX - start.x) / getXPixelRatio);
            currPos.y = getYPixelRatio * Math.round((rectPos.y + e.offsetY - start.y) / getYPixelRatio);
            currPos.x = edge(currPos.x, 0, w - 25);
            currPos.y = edge(currPos.y, 0, h - 25);         
            renderAll()
            drawRect(currPos.x, currPos.y);
        }
        document.onmouseup = (e) => {
            rectPos.x = currPos.x;
            rectPos.y = currPos.y;
            canvas.onmousemove = null
        }
    }
}

It is necessary to separate the event handler and the function itself

Something like this

const mouseDown = () =>{}
const mouseMove = () =>{}
const mouseUp = () =>{}

function mouseEvents() {
    canvas.addEventListiner("mousedown", mousedown)
    ....
}