diff options
author | Adam Domurad <[email protected]> | 2012-05-25 11:23:47 -0400 |
---|---|---|
committer | Adam Domurad <[email protected]> | 2012-05-25 11:23:47 -0400 |
commit | ef79a2eab042f66aa10a2356d9a6f80f5b8d544f (patch) | |
tree | 0f92b13bf74dc49b19607174e37bb176d555678a | |
parent | cfc204f1f531f9c867534ea2eca5eca216c99c04 (diff) |
Introduced constant NUM_STR_BUFFER_SIZE to replace magic constants.
Clarified some uses of snprintf.
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | plugin/icedteanp/IcedTeaNPPlugin.cc | 6 | ||||
-rw-r--r-- | plugin/icedteanp/IcedTeaPluginUtils.cc | 106 | ||||
-rw-r--r-- | plugin/icedteanp/IcedTeaPluginUtils.h | 4 |
4 files changed, 71 insertions, 58 deletions
@@ -1,3 +1,16 @@ +2012-05-23 Adam Domurad <[email protected]> + Removed instances of snprintf where buffer size was not known. Added + buffer size constant for allocating buffers for numeric conversions. + * plugin/icedteanp/IcedTeaNPPlugin.cc: Removed usage of snprintf for + simple blanking of strings. Buffer size was misguided previously. + Used NUM_STR_BUFFER_SIZE constant to replace magic numbers/ + * plugin/icedteanp/IcedTeaPluginUtils.cc: Made + NPVariantToString(NPVariant variant, std::string* result) use two + space indentation. Used NUM_STR_BUFFER_SIZE constant to replace magic + numbers. + * plugin/icedteanp/IcedTeaPluginUtils.h: Added constant, + NUM_STR_BUFFER_SIZE. + 2012-05-24 Danesh Dadachanji <[email protected]> Fix use of src dir instead of build dir when whitelisting. diff --git a/plugin/icedteanp/IcedTeaNPPlugin.cc b/plugin/icedteanp/IcedTeaNPPlugin.cc index a60d2e6..ea15ba3 100644 --- a/plugin/icedteanp/IcedTeaNPPlugin.cc +++ b/plugin/icedteanp/IcedTeaNPPlugin.cc @@ -1227,9 +1227,9 @@ void consume_message(gchar* message) { { // clear the "instance X status" parts - snprintf(parts[0], sizeof(""), ""); - snprintf(parts[1], sizeof(""), ""); - snprintf(parts[2], sizeof(""), ""); + strcpy(parts[0], ""); + strcpy(parts[1], ""); + strcpy(parts[2], ""); // join the rest gchar* status_message = g_strjoinv(" ", parts); diff --git a/plugin/icedteanp/IcedTeaPluginUtils.cc b/plugin/icedteanp/IcedTeaPluginUtils.cc index 3d69e9e..3210b1d 100644 --- a/plugin/icedteanp/IcedTeaPluginUtils.cc +++ b/plugin/icedteanp/IcedTeaPluginUtils.cc @@ -146,16 +146,15 @@ IcedTeaPluginUtilities::constructMessagePrefix(int context, int reference, void IcedTeaPluginUtilities::JSIDToString(void* id, std::string* result) { - - char id_str[20]; // max = long long = 8446744073709551615 == 19 chars + char id_str[NUM_STR_BUFFER_SIZE]; if (sizeof(void*) == sizeof(long long)) { - snprintf(id_str, sizeof(id_str), "%llu", id); + snprintf(id_str, NUM_STR_BUFFER_SIZE, "%llu", id); } else { - snprintf(id_str, sizeof(id_str), "%lu", id); // else use long + snprintf(id_str, NUM_STR_BUFFER_SIZE, "%lu", id); // else use long } result->append(id_str); @@ -256,9 +255,8 @@ IcedTeaPluginUtilities::releaseReference() void IcedTeaPluginUtilities::itoa(int i, std::string* result) { - // largest possible integer is 10 digits long - char int_str[11]; - snprintf(int_str, sizeof(int_str), "%d", i); + char int_str[NUM_STR_BUFFER_SIZE]; + snprintf(int_str, NUM_STR_BUFFER_SIZE, "%d", i); result->append(int_str); } @@ -368,12 +366,11 @@ IcedTeaPluginUtilities::convertStringToUTF8(std::string* str, std::string* utf_s ostream << length; - // UTF-8 characters are 4-bytes max + space + '\0' - char hex_value[10]; + char hex_value[NUM_STR_BUFFER_SIZE]; for (int i = 0; i < str->length(); i++) { - snprintf(hex_value, sizeof(hex_value)," %hx", str->at(i)); + snprintf(hex_value, NUM_STR_BUFFER_SIZE," %hx", str->at(i)); ostream << hex_value; } @@ -679,54 +676,53 @@ IcedTeaPluginUtilities::printNPVariant(NPVariant variant) void IcedTeaPluginUtilities::NPVariantToString(NPVariant variant, std::string* result) { - char str[32]; // enough for everything except string - char* largestr = NULL; - - if (NPVARIANT_IS_VOID(variant)) - { - snprintf(str, sizeof(str), "%p", variant); - } - else if (NPVARIANT_IS_NULL(variant)) - { - snprintf(str, sizeof(str), "NULL"); - } - else if (NPVARIANT_IS_BOOLEAN(variant)) - { - if (NPVARIANT_TO_BOOLEAN(variant)) - snprintf(str, sizeof(str), "true"); - else - snprintf(str, sizeof(str), "false"); - } - else if (NPVARIANT_IS_INT32(variant)) - { - snprintf(str, sizeof(str), "%d", NPVARIANT_TO_INT32(variant)); - } - else if (NPVARIANT_IS_DOUBLE(variant)) - { - snprintf(str, sizeof(str), "%f", NPVARIANT_TO_DOUBLE(variant));; - } - else if (NPVARIANT_IS_STRING(variant)) - { + char str[NUM_STR_BUFFER_SIZE]; // enough for everything except string + char* largestr = NULL; + if (NPVARIANT_IS_VOID(variant)) + { + snprintf(str, NUM_STR_BUFFER_SIZE, "%p", variant); + } + else if (NPVARIANT_IS_NULL(variant)) + { + snprintf(str, NUM_STR_BUFFER_SIZE, "NULL"); + } + else if (NPVARIANT_IS_BOOLEAN(variant)) + { + if (NPVARIANT_TO_BOOLEAN(variant)) + snprintf(str, NUM_STR_BUFFER_SIZE, "true"); + else + snprintf(str, NUM_STR_BUFFER_SIZE, "false"); + } + else if (NPVARIANT_IS_INT32(variant)) + { + snprintf(str, NUM_STR_BUFFER_SIZE, "%d", NPVARIANT_TO_INT32(variant)); + } + else if (NPVARIANT_IS_DOUBLE(variant)) + { + snprintf(str, NUM_STR_BUFFER_SIZE, "%f", NPVARIANT_TO_DOUBLE(variant)); + } + else if (NPVARIANT_IS_STRING(variant)) + { #if MOZILLA_VERSION_COLLAPSED < 1090200 - size_t buffersize = sizeof(char)*NPVARIANT_TO_STRING(variant).utf8length; - largestr = (char*) malloc(buffersize); - snprintf(str, buffersize, "%s", NPVARIANT_TO_STRING(variant).utf8characters); + size_t buffersize = sizeof(char)*NPVARIANT_TO_STRING(variant).utf8length; + largestr = (char*) malloc(buffersize); + snprintf(str, buffersize, "%s", NPVARIANT_TO_STRING(variant).utf8characters); #else - size_t buffersize = sizeof(char)*NPVARIANT_TO_STRING(variant).UTF8Length; - largestr = (char*) malloc(buffersize); - snprintf(str, buffersize, "%s", NPVARIANT_TO_STRING(variant).UTF8Characters); + size_t buffersize = sizeof(char)*NPVARIANT_TO_STRING(variant).UTF8Length; + largestr = (char*) malloc(buffersize); + snprintf(str, buffersize, "%s", NPVARIANT_TO_STRING(variant).UTF8Characters); #endif - } - else - { - snprintf(str, sizeof(str), "[Object %p]", variant); - } - if (largestr != NULL){ - result->append(largestr); - free(largestr); - } else { - result->append(str); - } + } + else + { + snprintf(str, NUM_STR_BUFFER_SIZE, "[Object %p]", variant); + } + if (largestr != NULL){ + result->append(largestr); + free(largestr); + } else { + result->append(str); + } } bool diff --git a/plugin/icedteanp/IcedTeaPluginUtils.h b/plugin/icedteanp/IcedTeaPluginUtils.h index 6a168da..9382a94 100644 --- a/plugin/icedteanp/IcedTeaPluginUtils.h +++ b/plugin/icedteanp/IcedTeaPluginUtils.h @@ -96,6 +96,10 @@ exception statement from your version. */ (*c >= 'a' && *c <= 'f') || \ (*c >= 'A' && *c <= 'F')) +//long long max ~ 19 chars + terminator +//leave some room for converting strings like "<var> = %d" +const size_t NUM_STR_BUFFER_SIZE = 32; + /* * This struct holds data specific to a Java operation requested by the plugin */ |