:: Re: [DNG] int essid_alloc is causin…
Startseite
Nachricht löschen
Nachricht beantworten
Autor: Edward Bartolo
Datum:  
To: Rainer Weikusat
CC: dng
Betreff: Re: [DNG] int essid_alloc is causing valgrind to report a series of errors
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)
==5501==    by 0x403BDC: autoWirelessScanPlus (automated_scanner.c:386)
==5501==    by 0x40400D: autoWirelessScanPlus_RN (automated_scanner.c:549)
==5501==    by 0x402E2C: main (backend.c:251)
==5501==  Uninitialised value was created by a stack allocation
==5501==    at 0x4034BB: getRadiatingWifiList (automated_scanner.c:155)



The code portion is this:
                                tmp_wifi_quality =
calloc(sizeof(wifi_quality), 1);


Here follows testing of return value from calloc, but I am not quoting it.

                                active_wifi_list[*active_wifis] =
tmp_wifi_quality;

                
                char* substr = strstr((char *) scan_buffer, "Signal level=");
                substr = strstr(substr, "=");
                char* endstr = strstr(substr + 1, " ");
                char tmpstr[MAX_ESSID_LENGTH];
                strncpy(tmpstr, substr + 1, endstr - substr - 1);
                tmpstr[endstr - substr + 1] = '\0';

                
                tmp_wifi_quality->quality = strtod(tmpstr, NULL);



Needless to state, the above works, but valgrind complains.

Edward

On 14/10/2015, Rainer Weikusat <rainerweikusat@???> wrote:
> Edward Bartolo <edbarx@???> writes:
>> The problem according to my logic seems to be result is used in a
>> branch control expression before it is initialised. However, I am
>> emailing this to have other opinions.
>>
>>
>> int essid_alloc(
>>     size_t length,
>>     char ** result
>> ) {
>>     char * tmp;

>>
>>     if(length==0 || !result)
>>         return EINVAL;

>>
>>     tmp = (char *) calloc(length, 1);

>>
>>     if(!tmp)
>>         return ENOMEM;

>>
>>     *result = tmp;

>>     
>>     return 0;
>> }

>
> result can't be uinitialized because it's an argument the caller must
> provide. *result may be uninitialized but this doesn't really matter
> unless the caller uses it regardless of the function return value. It
> would be helpful to know what valgrind actually prints.
>
> OTOH, the function above is equivalent to just doing a
>
> char *essid;
>
> essid = calloc(length, 1);
> if (!essid) /* ENOMEM */
>
> in the caller and IMHO, the whole function should be removed. There's
> also no point in casting the result of any memory allocation routine _in
> C_ as a void * will automatically be converted to whatever type was
> asked for on assignment (and there are rare cases where this may mask an
> actual error[*]).
>
> [*] In particular, using calloc without a prototype in scope will cause
>     the compiler to assume that it returns and int. This will work on a
>     32-bit system because the sizeof of a pointer is == sizeof(int) but
>     will fail on 64-bit (I had to mispleasure to debug this once when
>     moving some 'working' 32-bit Linux code to Solaris 8 on Sparc ...).
> _______________________________________________
> Dng mailing list
> Dng@???
> https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng

>