Run Check Failure #2

goth_nerd

Expert Member
Joined
Apr 4, 2013
Messages
1,226
Hi Guys

Is there anyone around who is good with C++?
I keep getting an error when trying to copy a char array to a char pointer e.g

strncpy_s(buffer, sizeof(temp), temp, _TRUNCATE);

I get a "Stack around the variable 'temp' was corrupted"

Please let me know if more information is needed.

P.S: The subject is meant to say RunTime Check Failure #2
 

Mr.Jax

Expert Member
Joined
Sep 22, 2009
Messages
1,460
Show me all the code.
eg. declaration and init of buffer, temp and _TRUNCATE.
 

Ancalagon

Honorary Master
Joined
Feb 23, 2010
Messages
18,140
Isn't the sizeof parameter supposed to be the number of bytes?

In which case, it would be the size of a char multiplied by the length of temp.
 

goth_nerd

Expert Member
Joined
Apr 4, 2013
Messages
1,226
Code:
void fixupstr(char * buffer)
{
	char     temp[1024] = { 0 };
	char *   pos = buffer;
	char *   destpos = temp;
	char     lastchar = 0;

	while (*pos != 0)
	{
		if (((*pos == '{') ||
			(*pos == '}') ||
			(*pos == ';') ||
			(*pos == ',') ||
			(*pos == '=')) && (lastchar != ' ') || (lastchar == ',') || (lastchar == '='))
		{
			*destpos++ = ' ';
			*destpos++ = *pos;
		}
		else
		{
			*destpos++ = *pos;
		}
		lastchar = *pos++;
	}
	*destpos = 0;
	if (*(destpos - 1) == '\n')
	{
		*(destpos - 2) = 0;
	}
	
	strncpy_s(buffer, sizeof(buffer), temp, _TRUNCATE);
}

int main()
{
      char buffer[1024] = {0};
      fgets(buffer, sizeof(buffer), *inFile); //inFile is the input file
      fixupstr(buffer);
}
 

cguy

Executive Member
Joined
Jan 2, 2013
Messages
8,527
BTW, sizeof(buffer) in fixupstr will be 8 (well, pointer size) not 1024.

To debug, you may want to verify that (destpos - temp) is less than 1024 (buffer overflow can cause stack corruption that isn't detected immediately).
 

goth_nerd

Expert Member
Joined
Apr 4, 2013
Messages
1,226
BTW, sizeof(buffer) in fixupstr will be 8 (well, pointer size) not 1024.

To debug, you may want to verify that (destpos - temp) is less than 1024 (buffer overflow can cause stack corruption that isn't detected immediately).

I just checked and it is less than 1024.
 

Ancalagon

Honorary Master
Joined
Feb 23, 2010
Messages
18,140
What runtime library are you using? Microsoft Visual C++?

The definitions that I can find for the strncpy_s function have the destination first and then the source. So, you should swap your parameters around.
 

goth_nerd

Expert Member
Joined
Apr 4, 2013
Messages
1,226
What runtime library are you using? Microsoft Visual C++?

The definitions that I can find for the strncpy_s function have the destination first and then the source. So, you should swap your parameters around.

I am using MS Visual C++. But buffer is my destination and temp is my source.
 

Mr.Jax

Expert Member
Joined
Sep 22, 2009
Messages
1,460
Code:
void fixupstr(char * buffer)
{
	char     temp[1024] = { 0 };
	char *   pos = buffer;
	char *   destpos = temp;
	char     lastchar = 0;

	while (*pos != 0)
	{
		if (((*pos == '{') ||
			(*pos == '}') ||
			(*pos == ';') ||
			(*pos == ',') ||
			(*pos == '=')) && (lastchar != ' ') || (lastchar == ',') || (lastchar == '='))
		{
			*destpos++ = ' ';
			*destpos++ = *pos;
		}
		else
		{
			*destpos++ = *pos;
		}
		lastchar = *pos++;
	}
	*destpos = 0;
	if (*(destpos - 1) == '\n')
	{
		*(destpos - 2) = 0;
	}
	
	strncpy_s(buffer, sizeof(buffer), temp, _TRUNCATE);
}

int main()
{
      char buffer[1024] = {0};
      fgets(buffer, sizeof(buffer), *inFile); //inFile is the input file
      fixupstr(buffer);
}

- Since you're adding a space when you encounter that set of chars, temp should be at least double the size of buffer. But you're copying back to buffer, so....buffer should be 2x its allowed content size really.
- strmcpy_s: sizeof(buffer) is going to be 4(32bit exe) or 8(64bit exe)


My guess is that you're overflowing temp[1024] due to the fact that worst case your function can produce double the number of characters given the input.
 

goth_nerd

Expert Member
Joined
Apr 4, 2013
Messages
1,226
- Since you're adding a space when you encounter that set of chars, temp should be at least double the size of buffer. But you're copying back to buffer, so....buffer should be 2x its allowed content size really.
- strmcpy_s: sizeof(buffer) is going to be 4(32bit exe) or 8(64bit exe)


My guess is that you're overflowing temp[1024] due to the fact that worst case your function can produce double the number of characters given the input.

Well the runtime error only shows when temp = '\n'. So it hasn't encountered the characters as yet. So for example;

Buffer comes in as "#ifndef __STATUSCODES_H__\n" what the method is supposed to do is to remove the new line character; "#ifndef __STATUSCODES_H__". The second time Buffer comes in as "\n" the new line character needs to be removed and an empty string copied back to buffer.

I hope this makes sense
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Well the runtime error only shows when temp = '\n'. So it hasn't encountered the characters as yet. So for example;

Buffer comes in as "#ifndef __STATUSCODES_H__\n" what the method is supposed to do is to remove the new line character; "#ifndef __STATUSCODES_H__". The second time Buffer comes in as "\n" the new line character needs to be removed and an empty string copied back to buffer.

I hope this makes sense
Where did you get this snippet?
 
Last edited:

goth_nerd

Expert Member
Joined
Apr 4, 2013
Messages
1,226
[)roi(];19266448 said:
Yes, Because logically it's not only dealing '\n'

It's code I got from work. The guy who wrote it left years ago. I don't understand what you mean when you say it's not only dealing with '\n'
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
It's code I got from work. The guy who wrote it left years ago. I don't understand what you mean when you say it's not only dealing with '\n'

What's the purpose of this part then?.
PHP:
if (((*pos == '{') ||
			(*pos == '}') ||
			(*pos == ';') ||
			(*pos == ',') ||
			(*pos == '=')) && (lastchar != ' ') || (lastchar == ',') || (lastchar == '='))


/edit Anyway got to run... the snippet you provided is certainly not only intended to focus on substituting newline with spaces.

However if all you really need is to remove or substitute one for the other, then here's some options:
PHP:
std::string str = "abc\ndef\nghi\n";

//For remove, try this.
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end()); // abcdefghi

// for substitution, try this.
std::string substitute(std::string source, char current, char target) {
  for (int i = 0; i < source.length();i++) {
    if (source[i] == current) {
      source[i] = target;
    }
  }
  return source;
}

std::string str2 = substitute(str, '\n', ' '); // abc def chi
 
Last edited:

goth_nerd

Expert Member
Joined
Apr 4, 2013
Messages
1,226
[)roi(];19266650 said:
What's the purpose of this part then?.
PHP:
if (((*pos == '{') ||
			(*pos == '}') ||
			(*pos == ';') ||
			(*pos == ',') ||
			(*pos == '=')) && (lastchar != ' ') || (lastchar == ',') || (lastchar == '='))


/edit Anyway got to run... the snippet you provided is certainly not only intended to focus on substituting newline with spaces.

However if all you really need is to remove or substitute one for the other, then here's some options:
PHP:
std::string str = "abc\ndef\nghi\n";

//For remove, try this.
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end()); // abcdefghi

// for substitution, try this.
std::string substitute(std::string source, char current, char target) {
  for (int i = 0; i < source.length();i++) {
    if (source[i] == current) {
      source[i] = target;
    }
  }
  return source;
}

std::string str2 = substitute(str, '\n', ' '); // abc def chi

Hey just to update you I ended up just creating a string and just used pop_back() to remove the end of line;

Code:
strDest = temp;
destLen = strDest.length()  + 1;
strDest.pop_back();

strcpy_s(buffer, destLen, strDest.c_str());

And then the other section just checks if the current character and last character are equal to those characters it just adds a space before the current character. (I think)
 
Top