Skip to content

Commit

Permalink
Being a bit more careful about memory.
Browse files Browse the repository at this point in the history
This avoids some valgrind errors, and should hopefully help reduce memory corruption (Issue #150)
  • Loading branch information
eholk committed Apr 28, 2015
1 parent 7fe9c66 commit ccf9e97
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 3 additions & 3 deletions rt/builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ int hscanfu64(FILE *f, uint64_t *i) {
// This will leak memory (#149)
char* hgets(FILE *f) {
char buffer[1024];
// this is totally insecure!
fscanf(f, "%s", buffer);
fscanf(f, "%1024s", buffer);

int len = strnlen(buffer, sizeof(buffer));

char *new_str = (char *)malloc(len + 1);
strncpy(new_str, buffer, sizeof(buffer));
memset(new_str, 0, len + 1);
strncpy(new_str, buffer, len);

return new_str;
}
Expand Down
9 changes: 8 additions & 1 deletion rt/cl++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ device_list::device_list(cl_device_type type)
// Allocate memory, gather information about all the devices.
devices = new cl_device_id[num_ids];

status = clGetDeviceIDs(platforms[platform], type, n_dev,
status = clGetDeviceIDs(platforms[platform], type, min(num_ids, n_dev),
devices, &n_dev);
//cerr << status << ", " << n_dev << endl;
CL_CHECK_MSG(status, "clGetDeviceIDs");
Expand Down Expand Up @@ -134,7 +134,9 @@ context::context(device_list &devices)

context::~context()
{
//cerr << "Destroying context" << endl;
clReleaseContext(ctx);
//cerr << "Context destroyed" << endl;
}

void context::sLogError(const char *errinfo,
Expand All @@ -159,7 +161,9 @@ kernel::kernel(cl_kernel k)

kernel::~kernel()
{
//cerr << "Destroying kernel" << endl;
clReleaseKernel(k);
//cerr << "Kernel destroyed" << endl;
}

size_t kernel::maxWorkGroupSize(cl_device_id device)
Expand All @@ -184,7 +188,9 @@ program::program(cl_program prog)

program::~program()
{
//cerr << "Destroying program" << endl;
clReleaseProgram(prog);
//cerr << "Program destroyed" << endl;
}

string escape_path(const char *s) {
Expand Down Expand Up @@ -229,6 +235,7 @@ void program::build()

kernel program::createKernel(string name)
{
//cerr << "Creating kernel" << endl;
return kernel(clCreateKernel(prog, name.c_str(), NULL));
}

Expand Down

0 comments on commit ccf9e97

Please sign in to comment.