sanitize outside inputs

This commit is contained in:
Justin Meza 2015-07-29 21:53:03 -07:00
parent e97cf5296b
commit 4154149e45
8 changed files with 87 additions and 3 deletions

View File

@ -1,6 +1,42 @@
#include "binding.h"
#include "inet.h" /* for sockets */
char *sanitizeInput(char *input)
{
unsigned int size = 16;
unsigned int cur = 0;
char *temp = malloc(sizeof(char) * size);
int pos = 0;
int c;
void *mem = NULL;
while (c = input[pos]) {
/* Reserve space to escape colon in input */
if (c == ':') {
cur++;
}
if (cur > size - 1) {
/* Increase buffer size */
size *= 2;
mem = realloc(temp, sizeof(char) * size);
if (!mem) {
perror("realloc");
free(temp);
return NULL;
}
temp = mem;
}
/* Escape colon in input */
if (c == ':') {
temp[cur - 1] = ':';
}
temp[cur] = (char)c;
cur++;
pos++;
}
temp[cur] = '\0';
return temp;
}
ValueObject *getArg(struct scopeobject *scope, char *name)
{
IdentifierNode *id = createIdentifierNode(IT_DIRECT, (void *)copyString(name), NULL, NULL, 0);
@ -106,7 +142,10 @@ ReturnObject *ireceiveWrapper(struct scopeobject *scope)
int len = inet_receive(remote, local, data, amount, -1);
data[len] = '\0';
ValueObject *ret = createStringValueObject(data);
char *sanitized = sanitizeInput(data);
free(data);
ValueObject *ret = createStringValueObject(sanitized);
return createReturnObject(RT_RETURN, ret);
}
@ -134,7 +173,10 @@ ReturnObject *freadWrapper(struct scopeobject *scope)
int len = fread(buf, 1, length, file);
buf[len] = '\0';
ValueObject *ret = createStringValueObject(buf);
char *sanitized = sanitizeInput(buf);
free(buf);
ValueObject *ret = createStringValueObject(sanitized);
return createReturnObject(RT_RETURN, ret);
}

View File

@ -1,7 +1,7 @@
INCLUDE(ParseArguments)
FUNCTION(ADD_LOL_TEST TEST_NAME)
PARSE_ARGUMENTS(ARG "LOLCODE;OUTPUT;INPUT" "ERROR" ${ARGN})
PARSE_ARGUMENTS(ARG "LOLCODE;OUTPUT;INPUT" "ERROR;CWD" ${ARGN})
IF(NOT ARG_LOLCODE)
SET(ARG_LOLCODE ${CMAKE_CURRENT_SOURCE_DIR}/test.lol)
@ -17,6 +17,10 @@ FUNCTION(ADD_LOL_TEST TEST_NAME)
LIST(APPEND TEST_COMMAND -i=${CMAKE_CURRENT_SOURCE_DIR}/${ARG_INPUT})
ENDIF(ARG_INPUT)
IF(ARG_CWD)
LIST(APPEND TEST_COMMAND -w=${CMAKE_CURRENT_SOURCE_DIR})
ENDIF(ARG_CWD)
IF(ARG_ERROR)
LIST(APPEND TEST_COMMAND -e)
ENDIF(ARG_ERROR)

View File

@ -0,0 +1,2 @@
INCLUDE(AddLolTest)
ADD_LOL_TEST(2-fread OUTPUT test.out CWD)

View File

@ -0,0 +1,9 @@
Now close the windows and hush all the fields:
If the trees must, let them silently toss;
No bird is singing now, and if there is,
Be it my loss.
It will be long ere the marshes resume,
I will be long ere the earliest bird:
So close the windows and not hear the wind,
But see all wind-stirred.

View File

@ -0,0 +1,19 @@
HAI 1.4
CAN HAS STDIO?
I HAS A file
file R I IZ STDIO'Z OPEN YR "read.dat" AN YR "r" MKAY
I HAS A var
var R I IZ STDIO'Z LUK YR file AN YR 45 MKAY
VISIBLE var
I HAS A var2
var2 R I IZ STDIO'Z LUK YR file AN YR 1 MKAY
VISIBLE var2 AN " - " AN "::"
BOTH SAEM var2 AN "::", O RLY?
YA RLY
VISIBLE "success"
NO WAI
VISIBLE "fail"
OIC
KTHXBYE

View File

@ -0,0 +1,3 @@
Now close the windows and hush all the fields
: - :
success

View File

@ -1 +1,2 @@
add_subdirectory(1-fopen)
add_subdirectory(2-fread)

View File

@ -3,6 +3,7 @@ import subprocess
import sys
import tempfile
import argparse
import os
MEMERR = 127
@ -13,9 +14,12 @@ parser.add_argument('-o', '--outputFile', type=argparse.FileType('r'), default=N
parser.add_argument('-i', '--inputFile', type=argparse.FileType('r'), default=None, help="File to be used as input")
parser.add_argument('-e', '--expectError', action="store_true", help="Specify that an error should occur")
parser.add_argument('-m', '--memCheck', action='store_true', help="Do a memory check")
parser.add_argument('-w', '--workingDirectory', default=None, help="Set the working directory")
args = parser.parse_args()
if args.workingDirectory:
os.chdir(args.workingDirectory)
if args.inputFile == None:
print("Not using an input file")