Skip to content

Commit

Permalink
Fix undefined behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlin96069 committed Feb 26, 2024
1 parent a62f855 commit a8f9835
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ DBGDIR = debug
DBGEXE = $(DBGDIR)/$(EXE)
DBGOBJS = $(addprefix $(DBGDIR)/, $(OBJS))
DBGDEPS = $(addprefix $(DBGDIR)/, $(DEPS))
DBGCFLAGS = -Og -g3 -D_DEBUG
DBGCFLAGS = -Og -g3 -D_DEBUG -fsanitize=address,undefined

# Default target
all: prep release
Expand Down
8 changes: 4 additions & 4 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@ CON_COMMAND(help, "Find help about a convar/concommand.") {
CON_COMMAND(crash, "Cause the editor to crash. (Debug!!)") {
int crash_type = 0;
if (args.argc > 1) {
crash_type = atoi(args.argv[1]);
crash_type = strToInt(args.argv[1]);
}

switch (crash_type) {
case 0:
// SIGSEGV
*(char*)0 = 0;
*(volatile char*)0 = 0;
break;
case 1:
// SIGABRT
Expand Down Expand Up @@ -618,9 +618,9 @@ void editorOpenConfigPrompt(void) {
}

void editorSetConVar(EditorConVar* thisptr, const char* string_val) {
strncpy(thisptr->string_val, string_val, COMMAND_MAX_LENGTH);
strncpy(thisptr->string_val, string_val, COMMAND_MAX_LENGTH - 1);
thisptr->string_val[COMMAND_MAX_LENGTH - 1] = '\0';
thisptr->int_val = atoi(string_val);
thisptr->int_val = strToInt(string_val);

if (thisptr->callback) {
thisptr->callback();
Expand Down
5 changes: 4 additions & 1 deletion src/highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

void editorUpdateSyntax(EditorFile* file, EditorRow* row) {
row->hl = realloc_s(row->hl, row->size);
memset(row->hl, HL_NORMAL, row->size);
if (row->hl) {
// realloc might returns NULL when row->size == 0
memset(row->hl, HL_NORMAL, row->size);
}

EditorSyntax* s = file->syntax;

Expand Down
19 changes: 1 addition & 18 deletions src/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,24 +226,7 @@ static void editorGotoCallback(char* query, int key) {
return;
}

int line = 0;

int sign = 1;
for (int i = 0; query[i]; i++) {
if (query[i] >= '0' && query[i] <= '9') {
line *= 10;
line += query[i] - '0';
} else if (i == 0 && (query[i] == '+' || query[i] == '-')) {
if (query[i] == '-') {
sign = -1;
}
} else {
line = 0;
break;
}
}

line *= sign;
int line = strToInt(query);

if (line < 0) {
line = gCurFile->num_rows + 1 + line;
Expand Down
49 changes: 45 additions & 4 deletions src/utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "utils.h"

#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -68,15 +69,15 @@ void abufAppendN(abuf *ab, const char *s, size_t n) {

void abufFree(abuf *ab) { free(ab->buf); }

static bool isValidColor(const char *color) {
static int isValidColor(const char *color) {
if (strlen(color) != 6)
return false;
return 0;
for (int i = 0; i < 6; i++) {
if (!(('0' <= color[i]) || (color[i] <= '9') || ('A' <= color[i]) ||
(color[i] <= 'F') || ('a' <= color[i]) || (color[i] <= 'f')))
return false;
return 0;
}
return true;
return 1;
}

Color strToColor(const char *color) {
Expand Down Expand Up @@ -274,6 +275,46 @@ char *strCaseStr(const char *str, const char *sub_str) {
return NULL;
}

int strToInt(const char *str) {
if (!str) {
return 0;
}

// Skip front spaces
while (*str == ' ' || *str == '\t') {
str++;
}

int sign = 1;
if (*str == '+' || *str == '-') {
sign = (*str++ == '-') ? -1 : 1;
}

int result = 0;
while (*str >= '0' && *str <= '9') {
if (result > INT_MAX / 10 ||
(result == INT_MAX / 10 && (*str - '0') > INT_MAX % 10)) {
// Overflow
return (sign == -1) ? INT_MIN : INT_MAX;
}

result = result * 10 + (*str - '0');
str++;
}

result = sign * result;

// Skip trailing spaces
while (*str != '\0') {
if (*str != ' ' && *str != '\t') {
return 0;
}
str++;
}

return result;
}

// https://opensource.apple.com/source/QuickTimeStreamingServer/QuickTimeStreamingServer-452/CommonUtilitiesLib/base64.c

static const char basis_64[] =
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef UTILS_H
#define UTILS_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
Expand Down Expand Up @@ -120,6 +119,7 @@ int getDigit(int n);
int64_t getLine(char** lineptr, size_t* n, FILE* stream);
int strCaseCmp(const char* s1, const char* s2);
char* strCaseStr(const char* str, const char* sub_str);
int strToInt(const char* str);

// Base64
static inline int base64EncodeLen(int len) { return ((len + 2) / 3 * 4) + 1; }
Expand Down

0 comments on commit a8f9835

Please sign in to comment.