Thread

  1. Avoid use scoped block variable

    Ranier Vilela <ranier.vf@gmail.com> — 2025-12-09T16:06:35Z

    Hi.
    
    I noticed a possible violation of C rules.
    Some functions rely on local block variables,
    but this are a mistake.
    Once that block exits, the memory of the variable is released.
    
    Fix by moving the declaration variables.
    
    best regards,
    Ranier Vilela
    
  2. Re: Avoid use scoped block variable

    Andres Freund <andres@anarazel.de> — 2025-12-09T16:19:06Z

    Hi,
    
    On 2025-12-09 13:06:35 -0300, Ranier Vilela wrote:
    > I noticed a possible violation of C rules.
    > Some functions rely on local block variables,
    > but this are a mistake.
    > Once that block exits, the memory of the variable is released.
    
    CStringGetTextDatum() copies its input to a fresh allocation. So there's no
    longer-lived references to the local memory, unless I miss something?
    
    Greetings,
    
    Andres Freund
    
    
    
    
  3. Re: Avoid use scoped block variable

    Tomas Vondra <tomas@vondra.me> — 2025-12-09T16:20:48Z

    On 12/9/25 17:06, Ranier Vilela wrote:
    > Hi.
    > 
    > I noticed a possible violation of C rules.
    > Some functions rely on local block variables, 
    > but this are a mistake.
    > Once that block exits, the memory of the variable is released.
    > 
    > Fix by moving the declaration variables.
    > 
    
    When you say "possible violation", did you check the issue is real?
    
    All these places call CStringGetTextDatum, which calls cstring_to_text,
    which allocates a new varlena copy of the string. So why is this an
    issue, exactly?
    
    
    regards
    
    -- 
    Tomas Vondra
    
    
    
    
    
  4. Re: Avoid use scoped block variable

    Ranier Vilela <ranier.vf@gmail.com> — 2025-12-09T16:24:14Z

    Em ter., 9 de dez. de 2025 às 13:19, Andres Freund <andres@anarazel.de>
    escreveu:
    
    > Hi,
    >
    > On 2025-12-09 13:06:35 -0300, Ranier Vilela wrote:
    > > I noticed a possible violation of C rules.
    > > Some functions rely on local block variables,
    > > but this are a mistake.
    > > Once that block exits, the memory of the variable is released.
    >
    > CStringGetTextDatum() copies its input to a fresh allocation. So there's no
    > longer-lived references to the local memory, unless I miss something?
    >
    Yeah. My bad.
    cstring_to_text use palloc.
    
    Sorry for the noise.
    
    best regards,
    Ranier Vilela