Skip to content

Commit

Permalink
Fixed 2 memory bugs related to #207. The Joys of C. sigh.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottransom committed Jan 18, 2025
1 parent e3abd53 commit 726e0ab
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ dist/
.mypy_cache/
.vscode/
build/
examplescripts/ppdot_*.png
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project('PRESTO', 'c',
version: '5.0.2.dev27',
version: '5.0.2.dev28',
license: 'GPL-2.0',
default_options: [
'buildtype=release',
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ requires = ['meson-python', 'numpy']

[project]
name = 'presto'
version = '5.0.2.dev27'
version = '5.0.2.dev28'
description = 'PulsaR Exploration and Search TOolkit'
requires-python = '>=3.9'
dependencies = ['numpy', 'scipy', 'astropy', 'matplotlib']
Expand Down
8 changes: 4 additions & 4 deletions src/misc_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ int split_root_suffix(char *input, char **root, char **suffix)
sptr = strrchr(input, '.');
if (sptr == NULL) {
*root = (char *) calloc(len + 1, sizeof(char));
strncpy(*root, input, len + 1);
strncpy(*root, input, len);
// ensure null-terminated
*root[len] = '\0';
(*root)[len] = '\0';
return 0;
} else {
rootlen = sptr - input;
*root = (char *) calloc(rootlen + 1, sizeof(char));
strncpy(*root, input, rootlen + 1);
strncpy(*root, input, rootlen);
// ensure null-terminated
(*root)[rootlen] = '\0';
suffixlen = len - rootlen - 1;
*suffix = (char *) calloc(suffixlen + 1, sizeof(char));
strncpy(*suffix, sptr + 1, suffixlen + 1);
strncpy(*suffix, sptr + 1, suffixlen);
// ensure null-terminated
(*suffix)[suffixlen] = '\0';
return 1;
Expand Down
12 changes: 7 additions & 5 deletions src/rednoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ int main(int argc, char *argv[])
cmd->argv[0]);
exit(0);
}
// The "+11" makes space for '_red' (4 bytes), a suffix if needed (4 bytes),
// a "./" prefix if needed (2 bytes), plus 1 for null termination
outname = (char *) calloc(strlen(rootfilenm) + 11, sizeof(char));
}

Expand Down Expand Up @@ -85,7 +87,7 @@ int main(int argc, char *argv[])

split_path_file(idata.name, &infdir, &filenm);

newinf = (char *) calloc(strlen(idata.name) + 5, sizeof(char));
newinf = (char *) calloc(strlen(idata.name) + strlen("_red") + 1, sizeof(char));

/* Uncomment for debugging
printf("idata.name: %s\n", idata.name);
Expand All @@ -103,8 +105,8 @@ int main(int argc, char *argv[])
sprintf(outname, "%s_red.fft", rootfilenm);
sprintf(idata.name, "%s", newinf);

fftfullpath = (char *) calloc(strlen(outname), sizeof(char));
inffullpath = (char *) calloc(strlen(outname), sizeof(char));
fftfullpath = (char *) calloc(strlen(outname) + 1, sizeof(char));
inffullpath = (char *) calloc(strlen(outname) + 1, sizeof(char));
sprintf(fftfullpath, "./%s_red.fft", rootfilenm);
sprintf(inffullpath, "./%s_red.inf", rootfilenm);

Expand All @@ -119,8 +121,8 @@ int main(int argc, char *argv[])
sprintf(idata.name, "%s", newinf);
sprintf(outname, "%s.fft", newinf);

fftfullpath = (char *) calloc(strlen(outname), sizeof(char));
inffullpath = (char *) calloc(strlen(outname), sizeof(char));
fftfullpath = (char *) calloc(strlen(outname) + 1, sizeof(char));
inffullpath = (char *) calloc(strlen(outname) + 1, sizeof(char));
sprintf(fftfullpath, "%s.fft", idata.name);
sprintf(inffullpath, "%s.inf", idata.name);

Expand Down

0 comments on commit 726e0ab

Please sign in to comment.