Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sprintf #369

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/hotspot/cpu/aarch64/vm_version_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ void VM_Version::initialize() {
if (_cpu == CPU_ARM && os::processor_count() == 1 && _model == 0xd07) _features |= CPU_A53MAC;

char buf[512];
sprintf(buf, "0x%02x:0x%x:0x%03x:%d", _cpu, _variant, _model, _revision);
if (_model2) sprintf(buf+strlen(buf), "(0x%03x)", _model2);
if (_features & CPU_ASIMD) strcat(buf, ", simd");
if (_features & CPU_CRC32) strcat(buf, ", crc");
if (_features & CPU_AES) strcat(buf, ", aes");
if (_features & CPU_SHA1) strcat(buf, ", sha1");
if (_features & CPU_SHA2) strcat(buf, ", sha256");
if (_features & CPU_LSE) strcat(buf, ", lse");
int buf_used_len = os::snprintf_checked(buf, sizeof(buf), "0x%02x:0x%x:0x%03x:%d", _cpu, _variant, _model, _revision);
if (_model2) buf_used_len += os::snprintf_checked(buf + buf_used_len, sizeof(buf) - buf_used_len, "(0x%03x)", _model2);
if (_features & CPU_ASIMD) buf_used_len += os::snprintf_checked(buf + buf_used_len, sizeof(buf) - buf_used_len, ", simd");
if (_features & CPU_CRC32) buf_used_len += os::snprintf_checked(buf + buf_used_len, sizeof(buf) - buf_used_len, ", crc");
if (_features & CPU_AES) buf_used_len += os::snprintf_checked(buf + buf_used_len, sizeof(buf) - buf_used_len, ", aes");
if (_features & CPU_SHA1) buf_used_len += os::snprintf_checked(buf + buf_used_len, sizeof(buf) - buf_used_len, ", sha1");
if (_features & CPU_SHA2) buf_used_len += os::snprintf_checked(buf + buf_used_len, sizeof(buf) - buf_used_len, ", sha256");
if (_features & CPU_LSE) buf_used_len += os::snprintf_checked(buf + buf_used_len, sizeof(buf) - buf_used_len, ", lse");

_features_string = os::strdup(buf);

Expand Down
12 changes: 6 additions & 6 deletions src/hotspot/os/bsd/attachListener_bsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ int BsdAttachListener::init() {
//
BsdAttachOperation* BsdAttachListener::read_request(int s) {
char ver_str[8];
sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER);
size_t ver_str_len = os::snprintf_checked(ver_str, sizeof(ver_str), "%d", ATTACH_PROTOCOL_VER);

// The request is a sequence of strings so we first figure out the
// expected count and the maximum possible length of the request.
Expand Down Expand Up @@ -288,11 +288,11 @@ BsdAttachOperation* BsdAttachListener::read_request(int s) {
// The first string is <ver> so check it now to
// check for protocol mis-match
if (str_count == 1) {
if ((strlen(buf) != strlen(ver_str)) ||
if ((strlen(buf) != ver_str_len) ||
(atoi(buf) != ATTACH_PROTOCOL_VER)) {
char msg[32];
sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION);
write_fully(s, msg, strlen(msg));
int msg_len = os::snprintf_checked(msg, sizeof(msg), "%d\n", ATTACH_ERROR_BADVERSION);
write_fully(s, msg, msg_len);
return NULL;
}
}
Expand Down Expand Up @@ -415,8 +415,8 @@ void BsdAttachOperation::complete(jint result, bufferedStream* st) {

// write operation result
char msg[32];
sprintf(msg, "%d\n", result);
int rc = BsdAttachListener::write_fully(this->socket(), msg, strlen(msg));
int msg_len = os::snprintf_checked(msg, sizeof(msg), "%d\n", result);
int rc = BsdAttachListener::write_fully(this->socket(), msg, msg_len);

// write any result data
if (rc == 0) {
Expand Down
25 changes: 11 additions & 14 deletions src/hotspot/os/bsd/os_bsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ void os::init_system_properties_values() {

#ifndef __APPLE__

// Buffer that fits several sprintfs.
// Buffer that fits several snprintfs.
// Note that the space for the colon and the trailing null are provided
// by the nulls included by the sizeof operator.
const size_t bufsize =
Expand Down Expand Up @@ -422,17 +422,16 @@ void os::init_system_properties_values() {
const char *v_colon = ":";
if (v == NULL) { v = ""; v_colon = ""; }
// That's +1 for the colon and +1 for the trailing '\0'.
char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char,
strlen(v) + 1 +
sizeof(SYS_EXT_DIR) + sizeof("/lib/") + strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH) + 1,
mtInternal);
sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib/%s:" DEFAULT_LIBPATH, v, v_colon, cpu_arch);
const size_t ld_library_path_size = strlen(v) + 1 + sizeof(SYS_EXT_DIR) +
sizeof("/lib/") + strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH) + 1;
char *ld_library_path = NEW_C_HEAP_ARRAY(char, ld_library_path_size, mtInternal);
os::snprintf_checked(ld_library_path, ld_library_path_size, "%s%s" SYS_EXT_DIR "/lib/%s:" DEFAULT_LIBPATH, v, v_colon, cpu_arch);
Arguments::set_library_path(ld_library_path);
FREE_C_HEAP_ARRAY(char, ld_library_path);
}

// Extensions directories.
sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
os::snprintf_checked(buf, bufsize, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
Arguments::set_ext_dirs(buf);

FREE_C_HEAP_ARRAY(char, buf);
Expand All @@ -447,7 +446,7 @@ void os::init_system_properties_values() {
size_t system_ext_size = strlen(user_home_dir) + sizeof(SYS_EXTENSIONS_DIR) +
sizeof(SYS_EXTENSIONS_DIRS);

// Buffer that fits several sprintfs.
// Buffer that fits several snprintfs.
// Note that the space for the colon and the trailing null are provided
// by the nulls included by the sizeof operator.
const size_t bufsize =
Expand Down Expand Up @@ -517,11 +516,9 @@ void os::init_system_properties_values() {
// could cause a change in behavior, but Apple's Java6 behavior
// can be achieved by putting "." at the beginning of the
// JAVA_LIBRARY_PATH environment variable.
char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char,
strlen(v) + 1 + strlen(l) + 1 +
system_ext_size + 3,
mtInternal);
sprintf(ld_library_path, "%s%s%s%s%s" SYS_EXTENSIONS_DIR ":" SYS_EXTENSIONS_DIRS ":.",
const size_t ld_library_path_size = strlen(v) + 1 + strlen(l) + 1 + system_ext_size + 3;
char *ld_library_path = NEW_C_HEAP_ARRAY(char, ld_library_path_size, mtInternal);
os::snprintf_checked(ld_library_path, ld_library_path_size, "%s%s%s%s%s" SYS_EXTENSIONS_DIR ":" SYS_EXTENSIONS_DIRS ":.",
v, v_colon, l, l_colon, user_home_dir);
Arguments::set_library_path(ld_library_path);
FREE_C_HEAP_ARRAY(char, ld_library_path);
Expand All @@ -532,7 +529,7 @@ void os::init_system_properties_values() {
// Note that the space for the colon and the trailing null are provided
// by the nulls included by the sizeof operator (so actually one byte more
// than necessary is allocated).
sprintf(buf, "%s" SYS_EXTENSIONS_DIR ":%s" EXTENSIONS_DIR ":" SYS_EXTENSIONS_DIRS,
os::snprintf_checked(buf, bufsize, "%s" SYS_EXTENSIONS_DIR ":%s" EXTENSIONS_DIR ":" SYS_EXTENSIONS_DIRS,
user_home_dir, Arguments::get_java_home());
Arguments::set_ext_dirs(buf);

Expand Down
5 changes: 5 additions & 0 deletions src/hotspot/share/adlc/adlc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,9 @@ typedef unsigned int uintptr_t;
// it everywhere it needs to be available.
extern ArchDesc* globalAD;

// Performs snprintf and asserts the result is non-negative (so there was not
// an encoding error) and that the output was not truncated.
extern int snprintf_checked(char* buf, size_t len, const char* fmt, ...);

#endif // SHARE_VM_ADLC_ADLC_HPP

25 changes: 15 additions & 10 deletions src/hotspot/share/adlc/adlparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,9 @@ void ADLParser::instr_parse(void) {
return;
}
assert(match_rules_cnt < 100," too many match rule clones");
char* buf = (char*) malloc(strlen(instr->_ident) + 4);
sprintf(buf, "%s_%d", instr->_ident, match_rules_cnt++);
const size_t buf_size = strlen(instr->_ident) + 4;
char* buf = (char*) malloc(buf_size);
snprintf_checked(buf, buf_size, "%s_%d", instr->_ident, match_rules_cnt++);
rule->_result = buf;
// Check for commutative operations with tree operands.
matchrule_clone_and_swap(rule, instr->_ident, match_rules_cnt);
Expand Down Expand Up @@ -2858,8 +2859,9 @@ void ADLParser::ins_encode_parse_block(InstructForm& inst) {
// Create a new encoding name based on the name of the instruction
// definition, which should be unique.
const char* prefix = "__ins_encode_";
char* ec_name = (char*) malloc(strlen(inst._ident) + strlen(prefix) + 1);
sprintf(ec_name, "%s%s", prefix, inst._ident);
const size_t ec_name_size = strlen(inst._ident) + strlen(prefix) + 1;
char* ec_name = (char*) malloc(ec_name_size);
snprintf_checked(ec_name, ec_name_size, "%s%s", prefix, inst._ident);

assert(_AD._encode->encClass(ec_name) == NULL, "shouldn't already exist");
EncClass* encoding = _AD._encode->add_EncClass(ec_name);
Expand Down Expand Up @@ -3329,8 +3331,9 @@ void ADLParser::constant_parse(InstructForm& inst) {
// Create a new encoding name based on the name of the instruction
// definition, which should be unique.
const char* prefix = "__constant_";
char* ec_name = (char*) malloc(strlen(inst._ident) + strlen(prefix) + 1);
sprintf(ec_name, "%s%s", prefix, inst._ident);
const size_t ec_name_size = strlen(inst._ident) + strlen(prefix) + 1;
char* ec_name = (char*) malloc(ec_name_size);
snprintf_checked(ec_name, ec_name_size, "%s%s", prefix, inst._ident);

assert(_AD._encode->encClass(ec_name) == NULL, "shouldn't already exist");
EncClass* encoding = _AD._encode->add_EncClass(ec_name);
Expand Down Expand Up @@ -4649,8 +4652,9 @@ char *ADLParser::get_ident_or_literal_constant(const char* description) {
// Grab a constant expression.
param = get_paren_expr(description);
if (param[0] != '(') {
char* buf = (char*) malloc(strlen(param) + 3);
sprintf(buf, "(%s)", param);
const size_t buf_size = strlen(param) + 3;
char* buf = (char*) malloc(buf_size);
snprintf_checked(buf, buf_size, "(%s)", param);
param = buf;
}
assert(is_literal_constant(param),
Expand Down Expand Up @@ -5257,8 +5261,9 @@ void ADLParser::next_line() {
char* ADLParser::get_line_string(int linenum) {
const char* file = _AD._ADL_file._name;
int line = linenum ? linenum : this->linenum();
char* location = (char *)malloc(strlen(file) + 100);
sprintf(location, "\n#line %d \"%s\"\n", line, file);
const size_t location_size = strlen(file) + 100;
char* location = (char *) malloc(location_size);
snprintf_checked(location, location_size, "\n#line %d \"%s\"\n", line, file);
return location;
}

Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/adlc/archDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ static const char *getRegMask(const char *reg_class_name) {
const char *mask = "_mask";
int length = (int)strlen(rc_name) + (int)strlen(mask) + 5;
char *regMask = new char[length];
sprintf(regMask,"%s%s()", rc_name, mask);
snprintf_checked(regMask, length, "%s%s()", rc_name, mask);
delete[] rc_name;
return regMask;
}
Expand Down Expand Up @@ -903,7 +903,7 @@ char *ArchDesc::stack_or_reg_mask(OperandForm &opForm) {
const char *stack_or = "STACK_OR_";
int length = (int)strlen(stack_or) + (int)strlen(reg_mask_name) + 1;
char *result = new char[length];
sprintf(result,"%s%s", stack_or, reg_mask_name);
snprintf_checked(result, length, "%s%s", stack_or, reg_mask_name);

return result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/hotspot/share/adlc/dfa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ Expr *ArchDesc::calc_cost(FILE *fp, const char *spaces, MatchList &mList, Produc
Expr *c = new Expr("0");
if (mList._lchild) { // If left child, add it in
const char* lchild_to_upper = ArchDesc::getMachOperEnum(mList._lchild);
sprintf(Expr::buffer(), "_kids[0]->_cost[%s]", lchild_to_upper);
snprintf_checked(Expr::buffer(), STRING_BUFFER_LENGTH, "_kids[0]->_cost[%s]", lchild_to_upper);
c->add(Expr::buffer());
delete[] lchild_to_upper;
}
if (mList._rchild) { // If right child, add it in
const char* rchild_to_upper = ArchDesc::getMachOperEnum(mList._rchild);
sprintf(Expr::buffer(), "_kids[1]->_cost[%s]", rchild_to_upper);
snprintf_checked(Expr::buffer(), STRING_BUFFER_LENGTH, "_kids[1]->_cost[%s]", rchild_to_upper);
c->add(Expr::buffer());
delete[] rchild_to_upper;
}
Expand Down Expand Up @@ -751,7 +751,7 @@ const char *Expr::compute_expr(const Expr *c1, const Expr *c2) {
snprintf(string_buffer, STRING_BUFFER_LENGTH, "%s", c2->_expr);
}
else {
sprintf( string_buffer, "0");
snprintf_checked(string_buffer, STRING_BUFFER_LENGTH, "0");
}
string_buffer[STRING_BUFFER_LENGTH - 1] = '\0';
char *cost = strdup(string_buffer);
Expand Down
11 changes: 7 additions & 4 deletions src/hotspot/share/adlc/formssel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// FORMS.CPP - Definitions for ADL Parser Forms Classes
#include "adlc.hpp"

#define remaining_buflen(buffer, position) (sizeof(buffer) - ((position) - (buffer)))

//==============================Instructions===================================
//------------------------------InstructForm-----------------------------------
InstructForm::InstructForm(const char *id, bool ideal_only)
Expand Down Expand Up @@ -1537,7 +1539,7 @@ Predicate *InstructForm::build_predicate() {
s += strlen(s);
}
// Add predicate to working buffer
sprintf(s,"/*%s*/(",(char*)i._key);
snprintf_checked(s, remaining_buflen(buf, s), "/*%s*/(",(char*)i._key);
s += strlen(s);
mnode->build_instr_pred(s,(char*)i._key, 0, path_bitmask, 0);
s += strlen(s);
Expand Down Expand Up @@ -3477,7 +3479,7 @@ void MatchNode::build_internalop( ) {
_rChild->_internalop : _rChild->_opType) : "";
len += (int)strlen(lstr) + (int)strlen(rstr);
subtree = (char *)malloc(len);
sprintf(subtree,"_%s_%s_%s", _opType, lstr, rstr);
snprintf_checked(subtree, len, "_%s_%s_%s", _opType, lstr, rstr);
// Hash the subtree string in _internalOps; if a name exists, use it
iop = (char *)_AD._internalOps[subtree];
// Else create a unique name, and add it to the hash table
Expand Down Expand Up @@ -3894,8 +3896,9 @@ void MatchRule::matchrule_swap_commutative_op(const char* instr_ident, int count
MatchRule* clone = new MatchRule(_AD, this);
// Swap operands of commutative operation
((MatchNode*)clone)->swap_commutative_op(true, count);
char* buf = (char*) malloc(strlen(instr_ident) + 4);
sprintf(buf, "%s_%d", instr_ident, match_rules_cnt++);
const size_t buf_size = strlen(instr_ident) + 4;
char* buf = (char*) malloc(buf_size);
snprintf_checked(buf, buf_size, "%s_%d", instr_ident, match_rules_cnt++);
clone->_result = buf;

clone->_next = this->_next;
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/adlc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ static char *base_plus_suffix(const char* base, const char *suffix)
int len = (int)strlen(base) + (int)strlen(suffix) + 1;

char* fname = new char[len];
sprintf(fname,"%s%s",base,suffix);
snprintf_checked(fname,len,"%s%s",base,suffix);
return fname;
}

Expand Down
Loading
Loading