Suppose this is a string in which we are interested:
The value of Pi is 3.142 approximately.
The index of the space preceding Pi is: 18 - 1 = 17
The index of the space after Pi is: 24 - 1 = 23
The length of the decimal number is: 5 characters,
which is also: 23 - 17 - 1 = 5
If we were to copy the string representing the decimal number as I
actually did in my code, the null char would have been at: 5
My mistake was to add 1 instead of subtracting 1, ie, my mistake was
endptr - substr + 1
On 15/10/2015, Rainer Weikusat <rainerweikusat@???> wrote:
> Peter Olson <peabo@???> writes:
>>> On October 14, 2015 at 3:20 PM Edward Bartolo <edbarx@???> wrote:
>>>
>>>
>>> This is another part of the backend code where valgrind is saying:
>>>
>>> ==5501== 5 errors in context 1 of 3:
>>> ==5501== Use of uninitialised value of size 8
>>> ==5501== at 0x5172AFC: ____strtod_l_internal (strtod_l.c:889)
>>> ==5501== by 0x403856: getRadiatingWifiList (automated_scanner.c:265)
>
> [...]
>
>> This diagnostic bothers me:
>>
>>> ==5501== Uninitialised value was created by a stack allocation
>>> ==5501== at 0x4034BB: getRadiatingWifiList (automated_scanner.c:155)
>>
>> This is hundreds of lines away from
>>
>>> ==5501== by 0x403856: getRadiatingWifiList
>>> (automated_scanner.c:265)
>
> "ELARGEFUNCTION"
>
>> which is presumably
>>
>>> tmp_wifi_quality->quality = strtod(tmpstr, NULL);
>>
>> You should probably investigate the area around line 155.
>
> Since the explanation may be useful: 'Stack allocations' usually happen
> at the beginning of a function, regardless of the point of a variable
> declaration. Eg, when running the following test/ example program:
>
> ------
> /* 1 */ #include <stdlib.h>
> /* 2 */ #include <string.h>
> /* 3 */ #include <stdio.h>
> /* 4 */
> /* 5 */ char const scan_buffer[] = "yadda Signal level=5.9 fff";
> /* 6 */
> /* 7 */ int main(void)
> /* 8 */ {
> /* 9 */ double d;
> /* 10 */
> /* 11 */ char* substr = strstr((char *) scan_buffer, "Signal
> level=");
> /* 12 */ substr = strstr(substr, "=");
> /* 13 */ char* endstr = strstr(substr + 1, " ");
> /* 14 */ char tmpstr[8];
> /* 15 */ strncpy(tmpstr, substr + 1, endstr - substr - 1);
> /* 16 */ tmpstr[endstr - substr + 1] = '\0';
> /* 17 */
> /* 18 */ d = strtod(tmpstr, NULL);
> /* 19 */ printf("%f\n", d);
> /* 20 */
> /* 21 */ return 0;
> /* 22 */ }
> ------
>
> via
>
> $valgrind --track-origins=yes ./a.out
>
> one gets the following (partial) output:
>
> ==27072== Conditional jump or move depends on uninitialised value(s)
> ==27072== at 0x4E63430: ____strtod_l_internal (strtod_l.c:803)
> ==27072== by 0x40066B: main (aa.c:18)
> ==27072== Uninitialised value was created by a stack allocation
> ==27072== at 0x4005D4: main (aa.c:8)
> ==27072==
> ==27072== Use of uninitialised value of size 8
> ==27072== at 0x4E6343E: ____strtod_l_internal (strtod_l.c:818)
> ==27072== by 0x40066B: main (aa.c:18)
> ==27072== Uninitialised value was created by a stack allocation
> ==27072== at 0x4005D4: main (aa.c:8)
>
> The uninitialized values is used on line 18 but reported as allocated on
> line 8 which is the start of the function.
> _______________________________________________
> Dng mailing list
> Dng@???
> https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
>