-
Notifications
You must be signed in to change notification settings - Fork 2k
Avoid keeping pointer to temporary strings #4016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
substr() creates a temporary string which will be deallocated, leaving the c_str() pointer pointing to garbage.
pyu10055
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 0 of 1 approvals obtained (waiting on @estyrke)
tfjs-node/binding/tfjs_backend.cc, line 1142 at r1 (raw file):
input_tensor_index = 0; } else { input_tensor_index = atoi(input_op_index.c_str());
Is the concern that the temporary string was not disposed? If so here the temporary string is created?
Not exactly. The problem is on this line: const char *input_op_index = name.substr(index + 1).c_str();The expression In general, it is risky to hold on to the value returned by c_str, since it is an unmanged C pointer which will be undefined if used after its backing std::string has been deallocated - It is better to only call it "temporarily" when it is needed, such as within the atoi call here. |
|
VC2019 prints a warning to this effect, unfortunately I don't have a windows computer at hand now so I can't quote it. |
pyu10055
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! 1 of 1 approvals obtained (waiting on @estyrke)
tfjs-node/binding/tfjs_backend.cc, line 1142 at r1 (raw file):
Previously, estyrke (Emil Styrke) wrote…
Not exactly. The problem is on this line:
const char *input_op_index = name.substr(index + 1).c_str();The expression
name.substr(index + 1)creates a temporary variable whose scope ends at the end of the "full-expression" (essentially the end of this line). Thec_str()call then takes the address of the string pointed to by this temporary variable. Thus, after this line, the pointer ininput_op_indexpoints to an already deallocated memory area. When the pointer is then used on the line you pointed out, the result is undefined. In most cases, the memory contents may still be there and no problem is observed. But if the deallocated memory area has already been cleared or reused, the atoi (or the strlen before it) will fail. This is what we observed on our compiler (VS 2019) - the strlen returns 0 and the output_tensor_index (in our case) will always be zero no matter which output we try to select.In general, it is risky to hold on to the value returned by c_str, since it is an unmanged C pointer which will be undefined if used after its backing std::string has been deallocated - It is better to only call it "temporarily" when it is needed, such as within the atoi call here.
got it, thanks for the explanation.
BUG substr() creates a temporary string which will be deallocated, leaving the c_str() pointer pointing to garbage. Co-authored-by: Ping Yu <4018+pyu10055@users.noreply.github.com>
substr() creates a temporary string which will be deallocated, leaving the c_str() pointer pointing to garbage.
To see the logs from the Cloud Build CI, please join either our discussion or announcement mailing list.
This change is