Vice City: Multiplayer

VC:MP Discussion => General Discussion => Topic started by: ysc3839 on Jul 23, 2015, 04:38 AM

Title: Solve to the "print()" issue.
Post by: ysc3839 on Jul 23, 2015, 04:38 AM
void printfunc(HSQUIRRELVM v, const SQChar *s, ...)
{
va_list arglist;

va_start(arglist, s);
{
int nChars = strlen(s);
char * szBuffer = (char*)malloc(nChars + 1);
if (szBuffer == NULL)
{
char szInitBuffer[128];
sprintf(szInitBuffer, "Error could not be printed: failed to malloc the buffer at %d nChars.", nChars + 1);
pCore->rawprint(szInitBuffer);
}
else
{
memset(szBuffer, 0, nChars + 1);
vsnprintf(szBuffer, nChars, s, arglist);
OutputScriptInfo(szBuffer);

free(szBuffer);
}
}
va_end(arglist);
}

I wrote a program to test it, nothing wrong.
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

void printfunc(const char *s, ...);
void _printfunc(const char *s, ...);

int main(int argc, char* argv[])
{
char test1[513];
char test2[514];
memset(test1, 'A', sizeof(test1));
test1[512] = 0;
memset(test2, 'A', sizeof(test2));
test2[513] = 0;

printfunc(test1);
_printfunc(test1);

printfunc(test2);
_printfunc(test2);//Crash
return 0;
}

void printfunc(const char *s, ...)
{
va_list arglist;

va_start(arglist, s);
{
int nChars = strlen(s);
char * szBuffer = (char*)malloc(nChars + 1);
memset(szBuffer, 0, nChars + 1);
if (szBuffer == NULL)
{
printf("Error could not be printed: failed to malloc the buffer at %d nChars.", nChars + 1);
}
else
{
vsnprintf(szBuffer, nChars, s, arglist);
printf("%s", szBuffer);

free(szBuffer);
}

}
va_end(arglist);
}

void _printfunc(const char *s, ...)
{
va_list arglist;
char szInitBuffer[512];

va_start(arglist, s);
{
int nChars = vsnprintf(szInitBuffer, sizeof(szInitBuffer), s, arglist);
if (nChars > sizeof(szInitBuffer) - 1)
{
char * szBuffer = (char*)calloc(nChars + 1, sizeof(char));
if (szBuffer == NULL)
{
sprintf(szInitBuffer, "Error could not be printed: failed to malloc the buffer at %d nChars.", nChars + 1);
printf("%s", szInitBuffer);
}
else
{
vsnprintf(szBuffer, nChars, s, arglist);
printf("%s", szBuffer);

free(szBuffer);
}
}
else
printf("%s", szInitBuffer);
}
va_end(arglist);
}
Title: Re: Solve to the "print()" issue.
Post by: ysc3839 on Jul 28, 2015, 12:16 PM
@Stormeus
Title: Re: Solve to the "print()" issue.
Post by: Thijn on Jul 28, 2015, 12:31 PM
I still don't understand what on earth is 512 characters, and needs to be printed in the console.
Title: Re: Solve to the "print()" issue.
Post by: ysc3839 on Jul 28, 2015, 01:04 PM
Quote from: Thijn on Jul 28, 2015, 12:31 PMI still don't understand what on earth is 512 characters, and needs to be printed in the console.
Some people likes to print players' message on the console. If somebody send a message larger than 512 chars, the server will crash.
And even make a buffer overflow attack!
Title: Re: Solve to the "print()" issue.
Post by: Thijn on Jul 28, 2015, 01:07 PM
Then they shouldn't print it. What's the goddamn point...
Title: Re: Solve to the "print()" issue.
Post by: ysc3839 on Jul 28, 2015, 01:20 PM
Quote from: Thijn on Jul 28, 2015, 01:07 PMThen they shouldn't print it. What's the goddamn point...
But they don't listen to us.
Title: Re: Solve to the "print()" issue.
Post by: ysc3839 on Jul 28, 2015, 01:25 PM
I have made a pull request to the Squirrel project. :)
Title: Re: Solve to the "print()" issue.
Post by: DizzasTeR on Jul 28, 2015, 01:27 PM
Quote from: Thijn on Jul 28, 2015, 12:31 PMI still don't understand what on earth is 512 characters, and needs to be printed in the console.

My map editor fails to print all the objects CreateObject syntax because of exceeding 512 characters, so its a damn need of this.
Title: Re: Solve to the "print()" issue.
Post by: DeViL_JiN on Jul 28, 2015, 02:06 PM
Quote from: Doom_Killer on Jul 28, 2015, 01:27 PM
Quote from: Thijn on Jul 28, 2015, 12:31 PMI still don't understand what on earth is 512 characters, and needs to be printed in the console.

My map editor fails to print all the objects CreateObject syntax because of exceeding 512 characters, so its a damn need of this.
vcmp 1st map with 700 objects :P
Title: Re: Solve to the "print()" issue.
Post by: EK.IceFlake on Jul 29, 2015, 06:21 AM
untested, works on windows only
print <- function (wrt)
{
    system("echo " + wrt);
}
Title: Re: Solve to the "print()" issue.
Post by: ysc3839 on Jul 29, 2015, 11:46 AM
Quote from: NE.CrystalBlue on Jul 29, 2015, 06:21 AMuntested, works on windows only
print <- function (wrt)
{
    system("echo " + wrt);
}
If I print some space(" "), it will be "ECHO is on."

And then, I can do what I want to do!
print("1234 && ver");result:
Quote1234

Microsoft Windows [Version 6.3.9600]
;D