On 2018/09/21 07:36, Tom Murphy wrote:
> On Thu, Sep 20, 2018 at 10:42:50AM +0100, Stuart Henderson wrote:
> > If you fix the memory leak you introduced I'll commit it :)
> 
> Ah. Yes. Here's a new diff. :)
> 
> -Tom
> 
> Index: Makefile
> ===================================================================
> RCS file: /cvs/ports/sysutils/conky/Makefile,v
> retrieving revision 1.54
> diff -u -p -u -p -r1.54 Makefile
> --- Makefile  1 Jun 2018 14:24:55 -0000       1.54
> +++ Makefile  21 Sep 2018 06:33:32 -0000
> @@ -7,7 +7,7 @@ COMMENT=      light-weight system monitor
>  DISTNAME=    conky-1.9.0
>  CATEGORIES=  sysutils
>  HOMEPAGE=    http://conky.sourceforge.net/
> -REVISION=    15
> +REVISION=    16
>  
>  MASTER_SITES=        ${MASTER_SITE_SOURCEFORGE:=conky/}
>  
> Index: patches/patch-src_conky_c
> ===================================================================
> RCS file: /cvs/ports/sysutils/conky/patches/patch-src_conky_c,v
> retrieving revision 1.6
> diff -u -p -u -p -r1.6 patch-src_conky_c
> --- patches/patch-src_conky_c 31 Dec 2012 09:36:38 -0000      1.6
> +++ patches/patch-src_conky_c 21 Sep 2018 06:33:32 -0000
> @@ -1,6 +1,11 @@
>  $OpenBSD: patch-src_conky_c,v 1.6 2012/12/31 09:36:38 chrisz Exp $
> ---- src/conky.c.orig Thu May  3 23:22:21 2012
> -+++ src/conky.c      Tue Jul 24 18:10:39 2012
> +
> +Correctly change update_interval based on whether AC is plugged in
> +or on battery for OpenBSD.
> +
> +Index: src/conky.c
> +--- src/conky.c.orig
> ++++ src/conky.c
>  @@ -125,7 +125,7 @@
>   
>   /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
> @@ -39,17 +44,32 @@ $OpenBSD: patch-src_conky_c,v 1.6 2012/1
>                       OBJ(apm_adapter) {
>                               char *msg;
>   
> -@@ -3496,12 +3492,14 @@ static void main_loop(void)
> +@@ -3494,14 +3490,28 @@ static void main_loop(void)
> +     info.looped = 0;
> +     while (terminate == 0 && (total_run_times == 0 || info.looped < 
> total_run_times)) {
>               if(update_interval_bat != NOBATTERY && update_interval_bat != 
> update_interval_old) {
> -                     char buf[max_user_text];
> +-                    char buf[max_user_text];
>   
>  +#ifndef __OpenBSD__
> ++                    char buf[max_user_text];
>                       get_battery_short_status(buf, max_user_text, "BAT0");
>                       if(buf[0] == 'D') {
>                               update_interval = update_interval_bat;
>                       } else {
>                               update_interval = update_interval_old;
>                       }
> ++#else
> ++                    char *apm_status;
> ++                    apm_status = (char *) calloc(16, sizeof(char));
> ++                    if (apm_status != 0) {
> ++                            apm_status = get_apm_adapter();
> ++                if (strcmp(apm_status,"off-line") == 0) {
> ++                              update_interval = update_interval_bat;
> ++                    } else {
> ++                              update_interval = update_interval_old;
> ++                    }
> ++                            free(apm_status);
> ++            }

Same problem exists, get_apm_adapter() allocates itself and returns
a pointer to that memory, so now you're allocating yourself, throwing
away the address, freeing the memory allocated by get_apm_adapter(),
and leaking the memory from your calloc.

Try something along these lines:

char *apm_status;
apm_status = get_apm_adapter();
if (apm_status != NULL) {
        if (strcmp(apm_status,"off-line") == 0) {
                update_interval = update_interval_bat;
        else
                update_interval = update_interval_old;
        free(apm_status);
}




>  +#endif
>               }
>               info.looped++;

Reply via email to