Skip to content

Commit

Permalink
fix: check buffer limits
Browse files Browse the repository at this point in the history
  • Loading branch information
darlanalves committed Feb 24, 2024
1 parent 9f967ba commit 99954ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/esp8266.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#define MAX_DELAY 6871000
#define NUMBER_OF_PINS 4

#include "sdk.h"

#define Timer os_timer_t
Expand Down
35 changes: 26 additions & 9 deletions src/include/vm_types.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "osapi.h"

#define MAX_SLOTS 256
#define MAX_STACK_SIZE 64
#define MAX_PRINT_BUFFER 1024

#define vt_null 0
#define vt_identifier 1
Expand All @@ -12,7 +11,7 @@
#define vt_signedInteger 6
#define vt_string 7

static char *printBuffer = (char *)malloc(1024);
static char *printBuffer = (char *)malloc(MAX_PRINT_BUFFER);

typedef unsigned char byte;
typedef unsigned char *byteref;
Expand Down Expand Up @@ -184,12 +183,16 @@ class Program

void printf(const char *format, va_list args)
{
os_memset(printBuffer, 0, 1024);
char *p = (char *)format;
os_memset(printBuffer, 0, MAX_PRINT_BUFFER);
int len = 0;

char *p = (char *)format;

for (; *p; p++)
{
if (len >= MAX_PRINT_BUFFER)
break;

if (*p != '%')
{
printBuffer[len++] = (*p);
Expand All @@ -199,15 +202,20 @@ class Program
{
case 'd':
{
if (sizeof(int) > MAX_PRINT_BUFFER - len)
{
break;
}

int d = va_arg(args, int);
int c = ets_sprintf(printBuffer + len, "%d", d);
int c = os_sprintf(printBuffer + len, "%d", d);
len += c - 1;
break;
}
case 'x':
{
int d = va_arg(args, int);
int c = ets_sprintf(printBuffer + len, "%02x", d);
int c = os_sprintf(printBuffer + len, "%02x", d);
len += c - 1;
break;
}
Expand All @@ -220,7 +228,13 @@ class Program
case 's':
{
char *s = va_arg(args, char *);
int slen = strlen(s);
int slen = os_strlen(s);

if (slen > MAX_PRINT_BUFFER - len)
{
slen = MAX_PRINT_BUFFER - len - 1;
}

os_memcpy(printBuffer + len, s, slen);
len += slen;
break;
Expand All @@ -231,6 +245,9 @@ class Program
}
}

onSend(printBuffer, len);
if (len)
{
onSend(printBuffer, len);
}
}
};

0 comments on commit 99954ad

Please sign in to comment.