What's new

C/C++ [XBOX] Error Replacing String in Hook

  • Thread starter Fire30
  • Start date
  • Views 847
Fire30

Fire30

Seasoned Member
Messages
7,222
Reaction score
2,364
Points
635
Sin$
0
So I need to replace certain parts of a char * buffer in a function that I am hooking so I found the function that works fine on pc, but when I run it on xbox I get an error in watson and the xbox turns off. It is just using standard library functions so idk why the xbox is having so much trouble.

The error I am getting is:

This heap cannot be used from the current thread.

------------------------------------------------------------------------

stop code: 0xf4 (CRITICAL_OBJECT_TERMINATION)
(0x78C50000,0x91844F38,0x000005EA,0x000000E8)
------------------------------------------------------------------------

Here is the function:

C:
void replace(char * o_string, char * s_string, char * r_string) {
  //a buffer variable to do all replace things
  char buffer[8092];
  //to store the pointer returned from strstr
  char * ch;
  //first exit condition
  if(!(ch = strstr(o_string, s_string)))
  return;
  //copy all the content to buffer before the first occurrence of the search string
  strncpy(buffer, o_string, ch-o_string);
  //prepare the buffer for appending by adding a null to the end of it
  buffer[ch-o_string] = 0;
  //append using sprintf function
  sprintf(buffer+(ch - o_string), "%s%s", r_string, ch + strlen(s_string));
  //empty o_string for copying
  o_string[0] = 0;
  strcpy(o_string, buffer);
  //pass recursively to replace other occurrences
  return replace(o_string, s_string, r_string);
}

Here is how I am calling it:
C:
int   functionHook(char FAR *buf, int len)
{
   int ret = function(buf,len);
   replace(buf,"THISISREPLACED","THISISSUBSITUTEDIN");
   return ret;
}

I think it has to do with the strcpy or strncpy but I am still confused.
 
Last edited:
Fire30

Fire30

Seasoned Member
Messages
7,222
Reaction score
2,364
Points
635
Sin$
0
okay so after some more debugging. It seems like this is the problem line
C:
sprintf(buffer+(ch - o_string), "%s%s", r_string, ch + strlen(s_string));
Nothing really seems off tbh?

Is it because my buffer is from game memory while I am calling it from a dll or something? When I search the error I get a lot of stuff about BSOD on windows which is useless lol..
 
Last edited:
Im4eversmart

Im4eversmart

The hacks are real
Glitcher Modder Programmer
Messages
2,156
Reaction score
1,903
Points
455
Sin$
7
okay so after some more debugging. It seems like this is the problem line
C:
sprintf(buffer+(ch - o_string), "%s%s", r_string, ch + strlen(s_string));
Nothing really seems off tbh?

Is it because my buffer is from game memory while I am calling it from a dll or something? When I search the error I get a lot of stuff about BSOD on windows which is useless lol..

Isn't the last argument an int, so you would want %s%u?
 
Xeren

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
okay so after some more debugging. It seems like this is the problem line
C:
sprintf(buffer+(ch - o_string), "%s%s", r_string, ch + strlen(s_string));
Nothing really seems off tbh?

Is it because my buffer is from game memory while I am calling it from a dll or something? When I search the error I get a lot of stuff about BSOD on windows which is useless lol..
I would try a different approach. The error is something because of like you said. I'm not sure how to explain it, but I'd try something other than sprintf, and if that doesn't work, then to change the method entirely.
 
Fire30

Fire30

Seasoned Member
Messages
7,222
Reaction score
2,364
Points
635
Sin$
0
Isn't the last argument an int, so you would want %s%u?
nah it is a char* . strstr returns a char * so basically it is adding the length of the substituted string to the pointer value of ch so that it add the rest of the string after the subsitution.
 
Fire30

Fire30

Seasoned Member
Messages
7,222
Reaction score
2,364
Points
635
Sin$
0
I would try a different approach. The error is something because of like you said. I'm not sure how to explain it, but I'd try something other than sprintf, and if that doesn't work, then to change the method entirely.
Yeah I decided to try to use strcat to replicate what the sprintf is doing and it magically works. Both ways are functionally equivalent so idk why it didn't work. I guess I would still like to know the root reason. Here is what I subsituted the sprintf for.

C:
strcat(buffer+(ch - o_string), r_string);
strcat(buffer+(ch - o_string) + strlen(r_string) , ch + strlen(s_string));
 
Xeren

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
Yeah I decided to try to use strcat to replicate what the sprintf is doing and it magically works. Both ways are functionally equivalent so idk why it didn't work. I guess I would still like to know the root reason. Here is what I subsituted the sprintf for.

C:
strcat(buffer+(ch - o_string), r_string);
strcat(buffer+(ch - o_string) + strlen(r_string) , ch + strlen(s_string));
Yeah, I had that problem a while ago and substituting sprintf for another method worked for me. I don't remember why, but it works.
 
Fire30

Fire30

Seasoned Member
Messages
7,222
Reaction score
2,364
Points
635
Sin$
0
Yeah, I had that problem a while ago and substituting sprintf for another method worked for me. I don't remember why, but it works.
iirc I had problem with printf throwing the same error before. idk how printing something could cause that error lol.
 
Xeren

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
iirc I had problem with printf throwing the same error before. idk how printing something could cause that error lol.
Yeah, I think you could also solve the problem by changing some settings in Visual Studio, but I'm glad it works now anyway.
 
Im4eversmart

Im4eversmart

The hacks are real
Glitcher Modder Programmer
Messages
2,156
Reaction score
1,903
Points
455
Sin$
7
Did a little searching. The GNU documentation states that, "The behavior of this function is undefined if copying takes place between objects that overlap." That could be why you are getting memory errors.
 
Fire30

Fire30

Seasoned Member
Messages
7,222
Reaction score
2,364
Points
635
Sin$
0
Yeah, I think you could also solve the problem by changing some settings in Visual Studio, but I'm glad it works now anyway.
Ok so it turns out that function didn't really work well for what I needed so I found one without recursion. I am getting the same error when I use malloc now lol. Do you know what settings in Visual Studio that I could change?
 
Fire30

Fire30

Seasoned Member
Messages
7,222
Reaction score
2,364
Points
635
Sin$
0
hmm I compiled it for release and it seems to be working? This whole thing is confusing.
 
Xeren

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
hmm I compiled it for release and it seems to be working? This whole thing is confusing.
Yep, that's the setting thing I was talking about. Release and debug each have their own limitations. I'd just take whatever works.
 
Top Bottom
Login
Register