Skip to content

Commit

Permalink
fix bc mcxlab/pmcxcl memory error,fangq/mcx#191,fangq/mcx#192
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Oct 3, 2023
1 parent aa5da8c commit fa8dbb2
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/mcx_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void mcx_initcfg(Config* cfg) {
cfg->debuglevel = 0;
cfg->gpuid = 0;

memset(cfg->bc, 0, 12);
memset(cfg->bc, 0, 13);
memset(&cfg->his, 0, sizeof(History));
memcpy(cfg->his.magic, "MCXH", 4);
cfg->his.version = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/mcx_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ typedef struct MCXConfig {
int parentid; /**<flag for testing if mcx is executed inside matlab*/
uint optlevel; /**<OpenCL JIT compilation optimization level*/
uint mediabyte; /**< how many bytes per media index, mcx supports 1, 2 and 4, 4 is the default*/
char bc[12]; /**<boundary condition flag for [-x,-y,-z,+x,+y,+z, det(-x,-y,-z,+x,+y,+z)] */
char bc[13]; /**<boundary condition flag for [-x,-y,-z,+x,+y,+z, det(-x,-y,-z,+x,+y,+z)], last element is always NULL for string termination */
} Config;

#ifdef __cplusplus
Expand Down
3 changes: 1 addition & 2 deletions src/mcxlabcl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,8 +1034,7 @@ void mcx_set_field(const mxArray* root, const mxArray* item, int idx, Config* cf
mexErrMsgTxt("the 'bc' field must be a non-empty string");
}

mxGetString(item, cfg->bc, len + 1);
cfg->bc[len] = '\0';
mxGetString(item, cfg->bc, len + 1); // copy the string, plus the ending NULL, max 13 char
printf("mcx.bc='%s';\n", cfg->bc);
} else if (strcmp(name, "detphotons") == 0) {
arraydim = mxGetDimensions(item);
Expand Down
5 changes: 2 additions & 3 deletions src/pmcxcl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,11 +758,10 @@ void parse_config(const py::dict& user_cfg, Config& mcx_config) {
std::string bc_string = py::str(user_cfg["bc"]);

if (bc_string.empty() || bc_string.size() > 12) {
throw py::value_error("the 'bc' field must be a non-empty string / have less than 12 characters.");
throw py::value_error("the 'bc' field must be a non-empty string / have no less than 12 characters.");
}

strncpy(mcx_config.bc, bc_string.c_str(), bc_string.size() + 1);
mcx_config.bc[bc_string.size()] = '\0';
strncpy(mcx_config.bc, bc_string.c_str(), bc_string.size() + 1); // copy the string, plus the ending NULL, max 13 char
}

if (user_cfg.contains("detphotons")) {
Expand Down

0 comments on commit fa8dbb2

Please sign in to comment.