Skip to content

Commit 3082939

Browse files
authored
Merge pull request #7497 from mppf/issue-7352
Fix bug where string assignment did not always add terminating zero byte The string assignment function (really in reinitString) wasn't setting the null terminator at the end of a string in certain cases. The included new test demonstrates the previous bug and now passes. Closes #7352. Reviewed by @daviditen - thanks! - [x] full local testing - [x] valgrind testing of test/types/string - [x] tested test/types/string with GASNet
2 parents 071cc73 + e322ea9 commit 3082939

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

modules/internal/String.chpl

+2-1
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,20 @@ module String {
262262
const allocSize = chpl_here_good_alloc_size(s_len+1);
263263
this.buff = chpl_here_alloc(allocSize,
264264
CHPL_RT_MD_STR_COPY_DATA):bufferType;
265-
this.buff[s_len] = 0;
266265
this._size = allocSize;
267266
// We just allocated a buffer, make sure to free it later
268267
this.owned = true;
269268
}
270269
c_memmove(this.buff, buf, s_len);
270+
this.buff[s_len] = 0;
271271
} else {
272272
if this.owned && !this.isEmptyString() then
273273
chpl_here_free(this.buff);
274274
this.buff = buf;
275275
this._size = size;
276276
}
277277
} else {
278+
// s_len == 0 so the source string is empty
278279
// free the old buffer
279280
if this.owned && !this.isEmptyString() then chpl_here_free(this.buff);
280281
this.buff = nil;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern proc printf(fmt:c_string, arg:c_string);
2+
3+
var s1 = "0" * 10;
4+
var s2 = "1" * 3;
5+
6+
var test = s1;
7+
test = s2;
8+
9+
printf("%s\n", test.c_str());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
111

0 commit comments

Comments
 (0)