Skip to content
Permalink
Browse files

Fix strncat/cpy bounding issues (fix #1425)

  • Loading branch information
gfwilliams committed May 21, 2018
1 parent a0d7f43 commit 0a7619875bf79877907205f6bee08465b89ff10b
Showing with 14 additions and 16 deletions.
  1. +1 −0 ChangeLog
  2. +3 −8 src/jslex.c
  3. +4 −4 src/jspin.c
  4. +4 −3 src/jsutils.c
  5. +2 −1 src/jsvar.c
@@ -2,6 +2,7 @@
nRF5x: Make FlashWrite cope with flash writes > 4k
Increase max size of native strings on platforms that support it - 16 bit to 32 bit (#1432)
Fix stack size detection on Linux (fix #1427)
Fix strncat/cpy bounding issues (fix #1425)

1v98 : Allow Crypto SHA1 without SHA256/512 (for ESP8266 where flash is scarce)
Add better docs for the form of Wifi callback functions
@@ -814,19 +814,14 @@ void jslTokenAsString(int token, char *str, size_t len) {
}

assert(len>=10);
strncpy(str, "?[",len);
itostr(token, &str[2], 10);
strncat(str, "]",len);
espruino_snprintf(str, len, "?[%d]", token);
}

void jslGetTokenString(char *str, size_t len) {
if (lex->tk == LEX_ID) {
strncpy(str, "ID:", len);
strncat(str, jslGetTokenValueAsString(), len);
espruino_snprintf(str, len, "ID:%s", jslGetTokenValueAsString());
} else if (lex->tk == LEX_STR) {
strncpy(str, "String:'", len);
strncat(str, jslGetTokenValueAsString(), len);
strncat(str, "'", len);
espruino_snprintf(str, len, "String:'%s'", jslGetTokenValueAsString());
} else
jslTokenAsString(lex->tk, str, len);
}
@@ -179,7 +179,7 @@ void jshGetPinString(char *result, Pin pin) {
#endif
#endif
} else {
strncpy(result, "undefined", 10);
strcpy(result, "undefined");
}
}

@@ -379,10 +379,10 @@ void jshPinFunctionToString(JshPinFunction pinFunc, JshPinFunctionToStringFlags
jsiConsolePrintf("Couldn't convert pin function %d\n", pinFunc);
return;
}
if (flags & JSPFTS_DEVICE) strncat(buf, devStr, bufSize);
if (flags & JSPFTS_DEVICE) strncat(buf, devStr, bufSize-1);
if (flags & JSPFTS_DEVICE_NUMBER) itostr(devIdx, &buf[strlen(buf)], 10);
if (flags & JSPFTS_SPACE) strncat(buf, " ", bufSize);
if (infoStr && (flags & JSPFTS_TYPE)) strncat(buf, infoStr, bufSize);
if (flags & JSPFTS_SPACE) strncat(buf, " ", bufSize-(strlen(buf)+1));
if (infoStr && (flags & JSPFTS_TYPE)) strncat(buf, infoStr, bufSize-(strlen(buf)+1));
}

/** Prints a list of capable pins, eg:
@@ -604,11 +604,12 @@ void itostr_extra(JsVarInt vals,char *str,bool signedVal, unsigned int base) {
}

void ftoa_bounded_extra(JsVarFloat val,char *str, size_t len, int radix, int fractionalDigits) {
assert(len>9); // in case if strcpy
const JsVarFloat stopAtError = 0.0000001;
if (isnan(val)) strncpy(str,"NaN",len);
if (isnan(val)) strcpy(str,"NaN");
else if (!isfinite(val)) {
if (val<0) strncpy(str,"-Infinity",len);
else strncpy(str,"Infinity",len);
if (val<0) strcpy(str,"-Infinity");
else strcpy(str,"Infinity");
} else {
if (val<0) {
if (--len <= 0) { *str=0; return; } // bounds check
@@ -1186,6 +1186,7 @@ size_t jsvGetString(const JsVar *v, char *str, size_t len) {
const char *s = jsvGetConstString(v);
if (s) {
strncpy(str, s, len);
str[len-1] = 0;
return strlen(s);
} else if (jsvIsInt(v)) {
itostr(v->varData.integer, str, 10);
@@ -1218,7 +1219,7 @@ size_t jsvGetString(const JsVar *v, char *str, size_t len) {
jsvUnLock(stringVar);
return l;
} else {
strncpy(str, "", len);
str[0] = 0;
jsExceptionHere(JSET_INTERNALERROR, "Variable type cannot be converted to string");
return 0;
}

0 comments on commit 0a76198

Please sign in to comment.
You can’t perform that action at this time.