[pulseaudio-discuss] [PATCH 5/5] pactl: Implement list message-handlers

Georg Chini georg at chini.tk
Sat Jan 27 17:56:57 UTC 2018


On 26.01.2018 21:14, Georg Chini wrote:
> On 26.01.2018 04:22, Tanu Kaskinen wrote:
>> On Thu, 2018-01-25 at 09:08 +0100, Georg Chini wrote:
>>> On 24.01.2018 01:40, Tanu Kaskinen wrote:
>>>> On Mon, 2018-01-22 at 16:19 +0100, Georg Chini wrote:
>>>>> On 21.01.2018 01:03, Tanu Kaskinen wrote:
>
>>
>> In this case the calling code has to initialize and use state like
>> this:
>>
>> int ret;
>> char *state = params;
>> char *value;
>> while ((ret = pa_message_params_split_list(&state, &value)) > 0) {
>>      ...
>> }
>>
>> params in the first version and state in the second version is not
>> declared const, because the function replaces the closing bracket of
>> the read value with a null byte. That will make the returned value
>> string null-terminated.
>
> This simple idea never occurred to me, first because the original
> string is a const char and second because I did not want to modify
> it. But your idea makes things much easier.
> Do you see an issue with modifying the const char like
>
>    /* Replace } with 0 */
>    *(char*) current = 0;
>
> where current is a const char* pointing to the "}"?
> This will always work because we never change the length
> of the string. Or should I allocate a new string with pa_xstrdup
> and pass that to the function? Or should I pass a char* to the
> callback function and the callback is responsible to free the
> string? Personally I would prefer modifying the const char but
> that is not a very strong opinion because I know it is a bit ugly.

I had a discussion with my son and he showed me that string literals may
be stored in the code segment. So modifying a const char is obviously no
option. I'll do it differently. Shame on me for not knowing that.


More information about the pulseaudio-discuss mailing list