Solve to the "print()" issue.

Started by ysc3839, Jul 23, 2015, 04:38 AM

Previous topic - Next topic

ysc3839

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);
}

ysc3839


Thijn

I still don't understand what on earth is 512 characters, and needs to be printed in the console.

ysc3839

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!

Thijn

Then they shouldn't print it. What's the goddamn point...

ysc3839

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.

ysc3839

I have made a pull request to the Squirrel project. :)

DizzasTeR

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.

DeViL_JiN

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

EK.IceFlake

#9
untested, works on windows only
print <- function (wrt)
{
    system("echo " + wrt);
}

ysc3839

#10
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