Click here to Skip to main content
14,331,729 members

C / C++ / MFC

 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
leon de boer12-Jan-19 3:36
memberleon de boer12-Jan-19 3:36 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student12-Jan-19 4:59
memberHS_C_Student12-Jan-19 4:59 
AnswerRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Bram van Kampen12-Jan-19 14:01
memberBram van Kampen12-Jan-19 14:01 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student12-Jan-19 16:50
memberHS_C_Student12-Jan-19 16:50 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Richard MacCutchan12-Jan-19 22:51
protectorRichard MacCutchan12-Jan-19 22:51 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student13-Jan-19 7:10
memberHS_C_Student13-Jan-19 7:10 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Richard MacCutchan13-Jan-19 22:12
protectorRichard MacCutchan13-Jan-19 22:12 
AnswerDemonstrating the relevant case and potential best practice Pin
HS_C_Student13-Jan-19 6:46
memberHS_C_Student13-Jan-19 6:46 
I wrote a small demonstration of the issue. Peter and Paul are writing ticketing system software and Peter makes a mistake swapping two variables and creating a negative number which is passed as an unsigned integer to Paul.

Paul makes a mistake thinking an unsigned int will not cause an out of (lower) bounds array access if it passes the test "index >= 0".

Peter makes a mistake, Paul fails to catch it, the memory is potentially corrupted and undefined behavior results. That's the PRINCIPLE the code is meant to demonstrate, which is the only purpose of this code.

As an alternative I put in a boundary check based on the address the index would have us access and the addresses of the first and last members of the array. I think that's a better approach. It should match the way the array is accessed exactly and it should be completely independent of variable size, type, typecasting, and bitwise representation.

As an aside, VS on the maximum warning level does not warn for the ints passed to the function that was declared with unsigned ints. Also, the compiler can't foresee that a negative value will be passed to the function because the sign of that variable is determined at run time.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//Paul writes this code
int get_available_seat_count(unsigned int *seating_counts, unsigned int row_index, unsigned int num_rows, unsigned int seats_per_row)
{
	if(row_index < 0)
	{
		printf("row_index < 0: TRUE\n");
		return -1;
	}
	else
		printf("row_index < 0: FALSE\n");

//preferable method?:
	if(&seating_counts[row_index] < &seating_counts[0])
		printf("Array minimum boundary violation, index is %d\n", row_index);

	if(&seating_counts[row_index] > &seating_counts[num_rows-1])
		printf("Array maximum boundary violation, index is %d\n", row_index);

	return seats_per_row - seating_counts[row_index];
}

//Peter writes this code
void main()
{
	//row 0 is the back of the theater, row[num_rows-1]  is at the stage
	int num_rows = 12;
	int seats_per_row = 40;
	int n_available_seats;
	int row_index;
	unsigned int seating_counts[12];
	int i;

	//set the current state of the theater:
	srand((int)time(NULL));
	for(i = 0; i < num_rows; i++)
		seating_counts[i] = (unsigned int)rand() % seats_per_row;

	printf("Customer: are there any seats available in the third row from the stage?\n\n");
	row_index = 3;

	//rows are stored in reverse order from customer request, subtract row_index to get offset
	n_available_seats = get_available_seat_count(seating_counts, row_index - num_rows, num_rows, seats_per_row);

	printf("\nTeller: there are %d seats available in that row\n", n_available_seats);
}


Quote:
Customer: are there any seats available in the third row from the stage?

row_index < 0: FALSE
Array minimum boundary violation, index is -9

Teller: there are 858993500 seats available in that row

GeneralRe: Demonstrating the relevant case and potential best practice Pin
Richard MacCutchan13-Jan-19 22:15
protectorRichard MacCutchan13-Jan-19 22:15 
AnswerRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
Stefan_Lang16-Jan-19 23:13
memberStefan_Lang16-Jan-19 23:13 
GeneralRe: (C) Robust code Should an unsigned int array index be tested for "< 0" Pin
HS_C_Student17-Jan-19 13:05
memberHS_C_Student17-Jan-19 13:05 
Questionacademic question - usage of class variable Pin
Vaclav_10-Jan-19 5:20
memberVaclav_10-Jan-19 5:20 
AnswerRe: academic question - usage of class variable Pin
Richard MacCutchan10-Jan-19 6:27
protectorRichard MacCutchan10-Jan-19 6:27 
AnswerRe: academic question - usage of class variable Pin
CPallini10-Jan-19 21:28
mveCPallini10-Jan-19 21:28 
GeneralRe: academic question - usage of class variable Pin
Vaclav_11-Jan-19 3:25
memberVaclav_11-Jan-19 3:25 
GeneralRe: academic question - usage of class variable Pin
CPallini11-Jan-19 5:21
mveCPallini11-Jan-19 5:21 
GeneralRe: academic question - usage of class variable Pin
David Crow11-Jan-19 5:44
memberDavid Crow11-Jan-19 5:44 
AnswerRe: academic question - usage of class variable Pin
Stefan_Lang16-Jan-19 22:26
memberStefan_Lang16-Jan-19 22:26 
QuestionChanging CMainFrame Minimize ICON Pin
ForNow9-Jan-19 14:02
memberForNow9-Jan-19 14:02 
QuestionRe: Changing CMainFrame Minimize ICON Pin
David Crow10-Jan-19 4:07
memberDavid Crow10-Jan-19 4:07 
AnswerRe: Changing CMainFrame Minimize ICON Pin
ForNow10-Jan-19 4:39
memberForNow10-Jan-19 4:39 
GeneralRe: Changing CMainFrame Minimize ICON Pin
David Crow10-Jan-19 4:42
memberDavid Crow10-Jan-19 4:42 
GeneralRe: Changing CMainFrame Minimize ICON Pin
ForNow10-Jan-19 5:09
memberForNow10-Jan-19 5:09 
QuestionUsage of bitset ? Pin
Vaclav_9-Jan-19 5:12
memberVaclav_9-Jan-19 5:12 
AnswerRe: Usage of bitset ? Pin
Daniel Pfeffer9-Jan-19 5:46
professionalDaniel Pfeffer9-Jan-19 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.