-
Notifications
You must be signed in to change notification settings - Fork 736
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
LogError with null as a string (%s) #2587
Comments
Probably should have a new func in strings.c that returns a "NULL" string if the parameter is NULL. |
Hi Eric, Thanks for the quick reply. I have a quick question.
How is this different/better than using existing macro |
Because one would return a null value and the other is a string. That is why there are quotes around it. |
#define MU_P_OR_NULL(p) (((p)!=NULL)?(p):"NULL") Am I missing something? |
Thanks for the explanation. My original comment might be confusing too, let me rephrase it: I was talking about using MU_P_OR_NULL to encapsulate calls to STRING_c_str, which would result in something like this: Your suggestion, as I understand it, is to create a new mockable function such that:
And then replace calls to STRING_c_str with calls to this new function This obviously would work. But I can see that it might have a downside. The original STRING_c_str returns a null value which is easy to detect. Now with the new function you have to use strcmp to determine whether the return value of the function represents a null value or not. Moreover, this new function might cause confusion in the edge case where the string handle's string is "NULL" to begin with. |
With a new STRING_c_str you can be more explicate what is NULL. Something like this
This would more be opt-in than opt-out, so you should know the use case, which should be limited to logging. |
I see, if this function is for logging only then it makes sense to do that and it would solve the problem we have with However, there are some other cases where doing this does not solve the problem. One somewhat frequent problem is that existing code tries to print a null char* instead of a string handle. There are few examples of this, such as https://github.com/Azure/azure-iot-sdk-c/blob/main/serializer/src/schema.c#L2446, https://github.com/Azure/azure-iot-sdk-c/blob/main/serializer/src/iotdevice.c#L305, and https://github.com/Azure/azure-iot-sdk-c/blob/main/iothub_client/src/iothub_client_core.c#L2385 Since these cases print a char* instead of a string handle, |
@jianyan-li-qnx please validate this branch. |
@ericwol-msft Thanks for the response. The changes you proposed works on my end. However I have to also change void char_ptr_ToString(char* string, size_t bufferSize, const char* val)
{
(void)snprintf(string, bufferSize, "%s", MU_P_OR_NULL(val));
}
void wchar_ptr_ToString(char* string, size_t bufferSize, const wchar_t* val)
{
(void)snprintf(string, bufferSize, "%ls", MU_WP_OR_NULL(val));
} |
What is the call stack? We can't change anything in the deps/azure-ctest repo. |
|
Fixed in PR #2594 |
Hi team!
My name is Jianyan and I've been working on porting azure-iot-sdk-c LTS_08_2023 to QNX.
I noticed this problem where some test programs would seg fault. Upon further investigation, this is what I think happened:
Take
Transport_Fully_Reconnects_After_5_AMQP_device_errors
iniothub_client/tests/iothubtransport_amqp_common_ut/iothubtransport_amqp_common_ut.c
, in LTS_08_2023, as an example:STRING_c_str
without specifying return value of the mock function, atiothubtransport_amqp_common_ut.c:4465
IoTHubTransport_AMQP_Common_DoWork
atiothubtransport_amqp_common_ut.c:4469
IoTHubTransport_AMQP_Common_Device_DoWork
is called atiothubtransport_amqp_common.c:1470
LogError
is called atiothubtransport_amqp_common.c:1071
with the argumentSTRING_c_str(registered_device->device_id)
as a%s
STRING_c_str
returnsNULL
, so LogError is called withNULL
as the%s
LogError
invokesconsolelogger_log
, which invokesvprintf
withNULL
as a%s
.Please see the full stacktrace below:
Please confirm, if possible, that my conclusion is correct.
I am not sure what is the most idiomatic way of solving this issue. I am thinking of sanitizing the string with
MU_P_OR_NULL
before each call toLogError
.The text was updated successfully, but these errors were encountered: