Elminate calls to sprintf
This commit is contained in:
parent
03d917bf40
commit
d1535de8d2
@ -125,7 +125,7 @@ static KrkValue _hex(int argc, KrkValue argv[], int hasKw) {
|
||||
if (argc != 1 || !IS_INTEGER(argv[0])) return krk_runtimeError(vm.exceptions->argumentError, "hex() expects one int argument");
|
||||
char tmp[20];
|
||||
krk_integer_type x = AS_INTEGER(argv[0]);
|
||||
size_t len = sprintf(tmp, "%s0x" PRIkrk_hex, x < 0 ? "-" : "", x < 0 ? -x : x);
|
||||
size_t len = snprintf(tmp, 20, "%s0x" PRIkrk_hex, x < 0 ? "-" : "", x < 0 ? -x : x);
|
||||
return OBJECT_VAL(krk_copyString(tmp,len));
|
||||
}
|
||||
|
||||
@ -221,14 +221,16 @@ static KrkValue _module_repr(int argc, KrkValue argv[], int hasKw) {
|
||||
KrkValue file = NONE_VAL();
|
||||
krk_tableGet(&self->fields, vm.specialMethodNames[METHOD_FILE], &file);
|
||||
|
||||
char * tmp = malloc(50 + AS_STRING(name)->length + (IS_STRING(file) ? AS_STRING(file)->length : 20));
|
||||
size_t allocSize = 50 + AS_STRING(name)->length + (IS_STRING(file) ? AS_STRING(file)->length : 20);
|
||||
char * tmp = malloc(allocSize);
|
||||
size_t len;
|
||||
if (IS_STRING(file)) {
|
||||
sprintf(tmp, "<module '%s' from '%s'>", AS_CSTRING(name), AS_CSTRING(file));
|
||||
len = snprintf(tmp, allocSize, "<module '%s' from '%s'>", AS_CSTRING(name), AS_CSTRING(file));
|
||||
} else {
|
||||
sprintf(tmp, "<module '%s' (built-in)>", AS_CSTRING(name));
|
||||
len = snprintf(tmp, allocSize, "<module '%s' (built-in)>", AS_CSTRING(name));
|
||||
}
|
||||
|
||||
KrkValue out = OBJECT_VAL(krk_copyString(tmp, strlen(tmp)));
|
||||
KrkValue out = OBJECT_VAL(krk_copyString(tmp, len));
|
||||
free(tmp);
|
||||
return out;
|
||||
}
|
||||
@ -252,14 +254,15 @@ static KrkValue obj_hash(int argc, KrkValue argv[], int hasKw) {
|
||||
*/
|
||||
static KrkValue _strBase(int argc, KrkValue argv[], int hasKw) {
|
||||
KrkClass * type = krk_getType(argv[0]);
|
||||
size_t len = sizeof("<instance of . at 0x1234567812345678>") + type->name->length;
|
||||
char * tmp = malloc(len);
|
||||
size_t allocSize = sizeof("<instance of . at 0x1234567812345678>") + type->name->length;
|
||||
char * tmp = malloc(allocSize);
|
||||
size_t len;
|
||||
if (IS_OBJECT(argv[0])) {
|
||||
sprintf(tmp, "<instance of %s at %p>", type->name->chars, (void*)AS_OBJECT(argv[0]));
|
||||
len = snprintf(tmp, allocSize, "<instance of %s at %p>", type->name->chars, (void*)AS_OBJECT(argv[0]));
|
||||
} else {
|
||||
sprintf(tmp, "<instance of %s>", type->name->chars);
|
||||
len = snprintf(tmp, allocSize, "<instance of %s>", type->name->chars);
|
||||
}
|
||||
KrkValue out = OBJECT_VAL(krk_copyString(tmp, strlen(tmp)));
|
||||
KrkValue out = OBJECT_VAL(krk_copyString(tmp, len));
|
||||
free(tmp);
|
||||
return out;
|
||||
}
|
||||
|
@ -197,8 +197,9 @@ KRK_FUNC(dis,{
|
||||
KrkFunction * func = ((KrkClosure*)AS_BOUND_METHOD(argv[0])->method)->function;
|
||||
const char * methodName = func->name ? func->name->chars : "(unnamed)";
|
||||
const char * typeName = IS_CLASS(AS_BOUND_METHOD(argv[0])->receiver) ? AS_CLASS(AS_BOUND_METHOD(argv[0])->receiver)->name->chars : krk_typeName(AS_BOUND_METHOD(argv[0])->receiver);
|
||||
char * tmp = malloc(strlen(methodName) + strlen(typeName) + 2);
|
||||
sprintf(tmp, "%s.%s", typeName, methodName);
|
||||
size_t allocSize = strlen(methodName) + strlen(typeName) + 2;
|
||||
char * tmp = malloc(allocSize);
|
||||
snprintf(tmp, allocSize, "%s.%s", typeName, methodName);
|
||||
krk_disassembleChunk(stdout, func, tmp);
|
||||
free(tmp);
|
||||
} else {
|
||||
|
12
src/fileio.c
12
src/fileio.c
@ -93,9 +93,10 @@ KRK_METHOD(File,__str__,{
|
||||
KrkValue modestr;
|
||||
if (!krk_tableGet(&self->inst.fields, OBJECT_VAL(S("filename")), &filename) || !IS_STRING(filename)) return krk_runtimeError(vm.exceptions->baseException, "Corrupt File");
|
||||
if (!krk_tableGet(&self->inst.fields, OBJECT_VAL(S("modestr")), &modestr) || !IS_STRING(modestr)) return krk_runtimeError(vm.exceptions->baseException, "Corrupt File");
|
||||
char * tmp = malloc(AS_STRING(filename)->length + AS_STRING(modestr)->length + 100); /* safety */
|
||||
sprintf(tmp, "<%s file '%s', mode '%s' at %p>", self->filePtr ? "open" : "closed", AS_CSTRING(filename), AS_CSTRING(modestr), (void*)self);
|
||||
KrkString * out = krk_copyString(tmp, strlen(tmp));
|
||||
size_t allocSize = AS_STRING(filename)->length + AS_STRING(modestr)->length + 100;
|
||||
char * tmp = malloc(allocSize);
|
||||
size_t len = snprintf(tmp, allocSize, "<%s file '%s', mode '%s' at %p>", self->filePtr ? "open" : "closed", AS_CSTRING(filename), AS_CSTRING(modestr), (void*)self);
|
||||
KrkString * out = krk_copyString(tmp, len);
|
||||
free(tmp);
|
||||
return OBJECT_VAL(out);
|
||||
})
|
||||
@ -455,8 +456,9 @@ KRK_METHOD(Directory,__repr__,{
|
||||
if (!krk_tableGet(&self->inst.fields, OBJECT_VAL(S("path")), &path) || !IS_STRING(path))
|
||||
return krk_runtimeError(vm.exceptions->valueError, "corrupt Directory");
|
||||
|
||||
char * tmp = malloc(AS_STRING(path)->length + 100);
|
||||
size_t len = sprintf(tmp, "<%s directory '%s' at %p>", self->dirPtr ? "open" : "closed", AS_CSTRING(path), (void*)self);
|
||||
size_t allocSize = AS_STRING(path)->length + 100;
|
||||
char * tmp = malloc(allocSize);
|
||||
size_t len = snprintf(tmp, allocSize, "<%s directory '%s' at %p>", self->dirPtr ? "open" : "closed", AS_CSTRING(path), (void*)self);
|
||||
KrkString * out = krk_copyString(tmp, len);
|
||||
free(tmp);
|
||||
return OBJECT_VAL(out);
|
||||
|
@ -166,9 +166,10 @@ static void tab_complete_func(rline_context_t * c) {
|
||||
krk_push(thisValue);
|
||||
if (IS_CLOSURE(thisValue) || IS_BOUND_METHOD(thisValue) ||
|
||||
(IS_NATIVE(thisValue) && ((KrkNative*)AS_OBJECT(thisValue))->isMethod != 2)) {
|
||||
char * tmp = malloc(s->length + 2);
|
||||
sprintf(tmp, "%s(", s->chars);
|
||||
s = krk_takeString(tmp, strlen(tmp));
|
||||
size_t allocSize = s->length + 2;
|
||||
char * tmp = malloc(allocSize);
|
||||
size_t len = snprintf(tmp, allocSize, "%s(", s->chars);
|
||||
s = krk_takeString(tmp, len);
|
||||
krk_pop();
|
||||
krk_push(OBJECT_VAL(s));
|
||||
}
|
||||
@ -300,7 +301,7 @@ static void findInterpreter(char * argv[]) {
|
||||
if (next) *next++ = '\0';
|
||||
|
||||
char tmp[4096];
|
||||
sprintf(tmp, "%s/%s", path, argv[0]);
|
||||
snprintf(tmp, 4096, "%s/%s", path, argv[0]);
|
||||
if (access(tmp, X_OK) == 0) {
|
||||
binpath = strdup(tmp);
|
||||
break;
|
||||
|
@ -36,8 +36,9 @@ static KrkValue krk_docOfClass(int argc, KrkValue argv[], int hasKw) {
|
||||
/* Class.__str__() (and Class.__repr__) */
|
||||
static KrkValue _class_to_str(int argc, KrkValue argv[], int hasKw) {
|
||||
if (!IS_CLASS(argv[0])) return krk_runtimeError(vm.exceptions->typeError, "expected class");
|
||||
char * tmp = malloc(sizeof("<type ''>") + AS_CLASS(argv[0])->name->length);
|
||||
size_t l = sprintf(tmp, "<type '%s'>", AS_CLASS(argv[0])->name->chars);
|
||||
size_t allocSize = sizeof("<type ''>") + AS_CLASS(argv[0])->name->length;
|
||||
char * tmp = malloc(allocSize);
|
||||
size_t l = snprintf(tmp, allocSize, "<type '%s'>", AS_CLASS(argv[0])->name->chars);
|
||||
KrkString * out = krk_copyString(tmp,l);
|
||||
free(tmp);
|
||||
return OBJECT_VAL(out);
|
||||
|
@ -84,7 +84,7 @@ KRK_METHOD(bytes,__repr__,{
|
||||
pushStringBuilder(&sb, '\\');
|
||||
pushStringBuilder(&sb, 'x');
|
||||
char hex[3];
|
||||
sprintf(hex,"%02x", ch);
|
||||
snprintf(hex,3,"%02x", ch);
|
||||
pushStringBuilder(&sb, hex[0]);
|
||||
pushStringBuilder(&sb, hex[1]);
|
||||
} else {
|
||||
|
@ -63,7 +63,7 @@ static KrkValue _closure_str(int argc, KrkValue argv[], int hasKw) {
|
||||
|
||||
size_t len = AS_STRING(s)->length + sizeof("<function >");
|
||||
char * tmp = malloc(len);
|
||||
sprintf(tmp, "<function %s>", AS_CSTRING(s));
|
||||
snprintf(tmp, len, "<function %s>", AS_CSTRING(s));
|
||||
s = OBJECT_VAL(krk_copyString(tmp,len-1));
|
||||
free(tmp);
|
||||
krk_pop();
|
||||
@ -79,7 +79,7 @@ static KrkValue _bound_str(int argc, KrkValue argv[], int hasKw) {
|
||||
|
||||
size_t len = AS_STRING(s)->length + sizeof("<method >") + strlen(typeName) + 1;
|
||||
char * tmp = malloc(len);
|
||||
sprintf(tmp, "<method %s.%s>", typeName, AS_CSTRING(s));
|
||||
snprintf(tmp, len, "<method %s.%s>", typeName, AS_CSTRING(s));
|
||||
s = OBJECT_VAL(krk_copyString(tmp,len-1));
|
||||
free(tmp);
|
||||
krk_pop();
|
||||
|
@ -30,7 +30,7 @@ KRK_METHOD(int,__init__,{
|
||||
|
||||
KRK_METHOD(int,__str__,{
|
||||
char tmp[100];
|
||||
size_t l = sprintf(tmp, PRIkrk_int, self);
|
||||
size_t l = snprintf(tmp, 100, PRIkrk_int, self);
|
||||
return OBJECT_VAL(krk_copyString(tmp, l));
|
||||
})
|
||||
|
||||
@ -62,7 +62,7 @@ KRK_METHOD(float,__float__,{ return argv[0]; })
|
||||
|
||||
KRK_METHOD(float,__str__,{
|
||||
char tmp[100];
|
||||
size_t l = sprintf(tmp, "%g", self);
|
||||
size_t l = snprintf(tmp, 100, "%g", self);
|
||||
return OBJECT_VAL(krk_copyString(tmp, l));
|
||||
})
|
||||
|
||||
|
@ -705,7 +705,7 @@ KRK_METHOD(str,__repr__,{
|
||||
PUSH_CHAR('\\');
|
||||
PUSH_CHAR('x');
|
||||
char hex[3];
|
||||
sprintf(hex,"%02x", (unsigned char)*c);
|
||||
snprintf(hex, 3, "%02x", (unsigned char)*c);
|
||||
PUSH_CHAR(hex[0]);
|
||||
PUSH_CHAR(hex[1]);
|
||||
} else {
|
||||
|
14
src/os.c
14
src/os.c
@ -70,9 +70,9 @@ KRK_FUNC(uname,{
|
||||
}
|
||||
|
||||
char tmp[256];
|
||||
sprintf(tmp, "%ld", versionInfo.dwBuildNumber);
|
||||
size_t len = snprintf(tmp, 256, "%ld", versionInfo.dwBuildNumber);
|
||||
|
||||
S_KEY(version, krk_copyString(tmp,strlen(tmp)));
|
||||
S_KEY(version, krk_copyString(tmp,len));
|
||||
if (sizeof(void *) == 8) {
|
||||
S_KEY(machine,S("x64"));
|
||||
} else {
|
||||
@ -94,8 +94,9 @@ KrkValue krk_os_setenviron(int argc, KrkValue argv[], int hasKw) {
|
||||
return krk_runtimeError(vm.exceptions->argumentError, "Invalid arguments to environ.__set__");
|
||||
}
|
||||
/* Set environment variable */
|
||||
char * tmp = malloc(AS_STRING(argv[1])->length + AS_STRING(argv[2])->length + 2);
|
||||
sprintf(tmp, "%s=%s", AS_CSTRING(argv[1]), AS_CSTRING(argv[2]));
|
||||
size_t len = AS_STRING(argv[1])->length + AS_STRING(argv[2])->length + 2;
|
||||
char * tmp = malloc(len);
|
||||
snprintf(tmp, len, "%s=%s", AS_CSTRING(argv[1]), AS_CSTRING(argv[2]));
|
||||
int r = putenv(tmp);
|
||||
if (r == 0) {
|
||||
/* Make super call */
|
||||
@ -116,8 +117,9 @@ KrkValue krk_os_unsetenviron(int argc, KrkValue argv[], int hasKw) {
|
||||
#ifndef _WIN32
|
||||
unsetenv(AS_CSTRING(argv[1]));
|
||||
#else
|
||||
char * tmp = malloc(AS_STRING(argv[1])->length + 2);
|
||||
sprintf(tmp, "%s=", AS_CSTRING(argv[1]));
|
||||
size_t len = AS_STRING(argv[1])->length + 2;
|
||||
char * tmp = malloc(len);
|
||||
snprintf(tmp, len, "%s=", AS_CSTRING(argv[1]));
|
||||
putenv(tmp);
|
||||
free(tmp);
|
||||
#endif
|
||||
|
@ -72,8 +72,9 @@ void rline_history_insert(char * str) {
|
||||
void rline_history_append_line(char * str) {
|
||||
if (rline_history_count) {
|
||||
char ** s = &rline_history[(rline_history_count - 1 + rline_history_offset) % RLINE_HISTORY_ENTRIES];
|
||||
char * c = malloc(strlen(*s) + strlen(str) + 2);
|
||||
sprintf(c, "%s\n%s", *s, str);
|
||||
size_t len = strlen(*s) + strlen(str) + 2;
|
||||
char * c = malloc(len);
|
||||
snprintf(c, len, "%s\n%s", *s, str);
|
||||
if (c[strlen(c)-1] == '\n') {
|
||||
c[strlen(c)-1] = '\0';
|
||||
}
|
||||
|
@ -156,7 +156,7 @@ KRK_METHOD(Lock,__repr__,{
|
||||
/* Address of lock object */
|
||||
{
|
||||
char tmp[100];
|
||||
size_t len = sprintf(tmp, "%p", (void*)self);
|
||||
size_t len = snprintf(tmp, 100, "%p", (void*)self);
|
||||
pushStringBuilderStr(&sb, tmp, len);
|
||||
}
|
||||
|
||||
|
15
src/vm.c
15
src/vm.c
@ -1234,19 +1234,22 @@ void krk_initVM(int flags) {
|
||||
if (strstr(dir,"/bin") == (dir + strlen(dir) - 4)) {
|
||||
slash = strrchr(dir,'/');
|
||||
if (slash) *slash = '\0';
|
||||
char * out = malloc(sizeof("/lib/kuroko/") + strlen(dir));
|
||||
size_t len = sprintf(out,"%s/lib/kuroko/", dir);
|
||||
size_t allocSize = sizeof("/lib/kuroko/") + strlen(dir);
|
||||
char * out = malloc(allocSize);
|
||||
size_t len = snprintf(out, allocSize, "%s/lib/kuroko/", dir);
|
||||
krk_writeValueArray(AS_LIST(module_paths), OBJECT_VAL(krk_takeString(out, len)));
|
||||
} else {
|
||||
char * out = malloc(sizeof("/modules/") + strlen(dir));
|
||||
size_t len = sprintf(out,"%s/modules/", dir);
|
||||
size_t allocSize = sizeof("/modules/") + strlen(dir);
|
||||
char * out = malloc(allocSize);
|
||||
size_t len = snprintf(out, allocSize, "%s/modules/", dir);
|
||||
krk_writeValueArray(AS_LIST(module_paths), OBJECT_VAL(krk_takeString(out, len)));
|
||||
}
|
||||
#else
|
||||
char * backslash = strrchr(dir,'\\');
|
||||
if (backslash) *backslash = '\0';
|
||||
char * out = malloc(sizeof("\\modules\\") + strlen(dir));
|
||||
size_t len = sprintf(out,"%s\\modules\\", dir);
|
||||
size_t allocSize = sizeof("\\modules\\") + strlen(dir);
|
||||
char * out = malloc(allocSize);
|
||||
size_t len = snprintf(out, allocSize, "%s\\modules\\", dir);
|
||||
krk_writeValueArray(AS_LIST(module_paths), OBJECT_VAL(krk_takeString(out,len)));
|
||||
#endif
|
||||
free(dir);
|
||||
|
@ -87,7 +87,7 @@ static void findInterpreter(char * argv[]) {
|
||||
if (next) *next++ = '\0';
|
||||
|
||||
char tmp[4096];
|
||||
sprintf(tmp, "%s/%s", path, argv[0]);
|
||||
snprintf(tmp, 4096, "%s/%s", path, argv[0]);
|
||||
if (access(tmp, X_OK) == 0) {
|
||||
binpath = strdup(tmp);
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user