Skip to content

Commit

Permalink
replace strdup with strcpy due to matlab issues (#243)
Browse files Browse the repository at this point in the history
* replace strdup with strcpy due to matlab issues

* other minor fixes to logging
  • Loading branch information
bodono committed Jan 1, 2024
1 parent 831e781 commit 9d5c392
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/cones.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ void SCS(finish_cone)(ScsConeWork *c) {
}

char *SCS(get_cone_header)(const ScsCone *k) {
char *tmp = (char *)scs_malloc(sizeof(char) * 512);
char *tmp = (char *)scs_malloc(512); /* sizeof(char) = 1 */
scs_int i, soc_vars, sd_vars;
sprintf(tmp, "cones: ");
if (k->z) {
Expand Down
2 changes: 0 additions & 2 deletions src/rw.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ void SCS(write_data)(const ScsData *d, const ScsCone *k,
uint32_t scs_float_sz = (uint32_t)sizeof(scs_float);
const char *scs_version = SCS_VERSION;
uint32_t scs_version_sz = (uint32_t)strlen(scs_version);
scs_printf("Writing raw problem data to %s\n", stgs->write_data_filename);
fwrite(&(scs_int_sz), sizeof(uint32_t), 1, fout);
fwrite(&(scs_float_sz), sizeof(uint32_t), 1, fout);
fwrite(&(scs_version_sz), sizeof(uint32_t), 1, fout);
Expand Down Expand Up @@ -247,7 +246,6 @@ void SCS(log_data_to_csv)(const ScsCone *k, const ScsSettings *stgs,
ScsSolution *sol = w->xys_orig;
ScsSolution *sol_n = w->xys_normalized;
/* if iter 0 open to write, else open to append */
scs_printf("Logging run data to %s\n", stgs->log_csv_filename);
FILE *fout = fopen(stgs->log_csv_filename, iter == 0 ? "w" : "a");
if (!fout) {
scs_printf("Error: Could not open %s for writing\n",
Expand Down
5 changes: 5 additions & 0 deletions src/scs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1217,8 +1217,13 @@ ScsWork *scs_init(const ScsData *d, const ScsCone *k, const ScsSettings *stgs) {
#endif
SCS(tic)(&init_timer);
if (stgs->write_data_filename) {
scs_printf("Writing raw problem data to %s\n", stgs->write_data_filename);
SCS(write_data)(d, k, stgs);
}
if (stgs->log_csv_filename) {
scs_printf("Logging run data to %s\n", stgs->log_csv_filename);
/* logging done every iteration */
}
w = init_work(d, k, stgs);
if (w) {
w->setup_time = SCS(tocq)(&init_timer);
Expand Down
13 changes: 11 additions & 2 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,22 @@ void SCS(deep_copy_data)(ScsData *dest, const ScsData *src) {

void SCS(deep_copy_stgs)(ScsSettings *dest, const ScsSettings *src) {
memcpy(dest, src, sizeof(ScsSettings));
/* MATLAB does something weird with strdup, so use strcpy instead */
char *tmp;
if (src->write_data_filename) {
dest->write_data_filename = strdup(src->write_data_filename);
/* sizeof(char) = 1 */
tmp = (char *)scs_malloc(strlen(src->write_data_filename) + 1);
strcpy(tmp, src->write_data_filename);
dest->write_data_filename = tmp;
} else {
dest->write_data_filename = SCS_NULL;
}
/* MATLAB does something weird with strdup, so use strcpy instead */
if (src->log_csv_filename) {
dest->log_csv_filename = strdup(src->log_csv_filename);
/* sizeof(char) = 1 */
tmp = (char *)scs_malloc(strlen(src->log_csv_filename) + 1);
strcpy(tmp, src->log_csv_filename);
dest->log_csv_filename = tmp;
} else {
dest->log_csv_filename = SCS_NULL;
}
Expand Down

0 comments on commit 9d5c392

Please sign in to comment.