Need some help and info. about buffer

Hey guys I was wondering if its possible to bounds check the buffer like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <string>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
	char sBuffer[100];

	// here is the file
	ifstream file("MyCharFile.txt");

	// fill sBuffer with zeros (char strings must be null-terminated)
	fill_n(sBuffer, 100, '\0');

	// read only 100 bytes (chars) only from file and stick it in sBuffer[100]
	file.read(sBuffer, 100);

	file.close();

	//convert that char array(sBuffer) into a STL string, and show user what we got
	string s(sBuffer);
	cout << s << endl;

	char z;
	cin >> z;

	return 0;
}


would I have to add bounds checking here or would it be alright? Just in case for some reason if it read more the what can hold is there a way to check so it can notify if somehow it reads more and stores too much within the buffer?

It seems I see "buffer" around alot so I want to practice on it and i've seen how if you don't check if its going over it could lead to serious issues and people(attackers) can use this to their advantage.

thanks for anyone who can help me out since I'm planning to get into 2d graphics programming.

~Zeph~
Since your code explicitly determines the size of your buffer "char sBuffer[100];" and how many bytes are read, "file.read(sBuffer, 100);
" I don't see how someone could try to overflow your buffer but if the file read has more than 100 characters and the 100th character is not a terminating null then your string won't be null terminated. If you only read 99 bytes into sBuffer then you know the 100th character is going to be a terminating null.
Topic archived. No new replies allowed.