• best solution fo local buffer of rather nknown size?

    From fir@3:633/10 to All on Fri Sep 11 14:28:18 2026
    say yo may this case - its a printf like wrapper who just renders text
    on screen bitmap (though i could use it to render also on some ram
    bitmaps - so the length of text used here is limited to size of bitmap,
    say i rarelu use bitmaps larger than say 8k and rarely fonts
    smaller than 10 pixel wide so the 8k/10 = 800 length at most) -

    i know its under those mentioned assumptions okbut how to do it
    properly?




    static BMFont* tahoma = NULL;
    int tahoma_size = 23;

    void text_xy_colors_tahoma(int x, int y, unsigned* colors, char*
    format, ...)
    {
    if(!tahoma) tahoma = AllocFont("Tahoma", tahoma_size, 500);

    static char text[1000];

    va_list args;
    va_start(args, format);
    vsprintf(text, format, args);
    va_end(args);

    DrawText_BM_colors(tahoma, x,y, text, colors);
    }

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Fri Sep 11 14:49:51 2026
    On 11/09/2026 14:28, fir wrote:
    say yo may this case - its a printf like wrapper who just renders text
    on screen bitmap (though i could use it to render also on some ram
    bitmaps - so the length of text used here is limited to size of bitmap,
    say i rarelu use bitmaps larger than say 8k and rarely fonts
    smaller than 10 pixel wide so the 8k/10 = 800 length at most) -

    i know its under those mentioned assumptions okbut how to do it
    properly?




    ÿstatic BMFont* tahomaÿ = NULL;
    ÿint tahoma_sizeÿ = 23;

    ÿvoid text_xy_colors_tahoma(int x, int y, unsigned* colors, char*
    format, ...)
    ÿ{
    ÿÿ if(!tahoma)ÿ tahomaÿ = AllocFont("Tahoma", tahoma_size, 500);

    ÿÿ static char text[1000];

    ÿÿ va_list args;
    ÿÿ va_start(args, format);
    ÿÿ vsprintf(text, format, args);
    ÿÿ va_end(args);

    ÿÿ DrawText_BM_colors(tahoma, x,y,ÿ text, colors);
    ÿ}

    IMHO, it is fine to have a fixed buffer size like this. But you really
    should use "vsnprintf(text, sizeof(text), format, args);". Then if your
    code accidentally uses too long a message, it gets truncated instead of stomping over random memory space.

    Also note that with a "static" buffer, you'll get a mess (and UB) if you
    call the function from two threads at the same time. That may or may
    not matter to you.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From fir@3:633/10 to All on Fri Sep 11 15:29:34 2026
    David Brown pisze:
    On 11/09/2026 14:28, fir wrote:
    say yo may this case - its a printf like wrapper who just renders text
    on screen bitmap (though i could use it to render also on some ram
    bitmaps - so the length of text used here is limited to size of bitmap,
    say i rarelu use bitmaps larger than say 8k and rarely fonts
    smaller than 10 pixel wide so the 8k/10 = 800 length at most) -

    i know its under those mentioned assumptions okbut how to do it
    properly?




    ÿÿstatic BMFont* tahomaÿ = NULL;
    ÿÿint tahoma_sizeÿ = 23;

    ÿÿvoid text_xy_colors_tahoma(int x, int y, unsigned* colors, char*
    format, ...)
    ÿÿ{
    ÿÿÿ if(!tahoma)ÿ tahomaÿ = AllocFont("Tahoma", tahoma_size, 500);

    ÿÿÿ static char text[1000];

    ÿÿÿ va_list args;
    ÿÿÿ va_start(args, format);
    ÿÿÿ vsprintf(text, format, args);
    ÿÿÿ va_end(args);

    ÿÿÿ DrawText_BM_colors(tahoma, x,y,ÿ text, colors);
    ÿÿ}

    IMHO, it is fine to have a fixed buffer size like this.ÿ But you really should use "vsnprintf(text, sizeof(text), format, args);".ÿ Then if your code accidentally uses too long a message, it gets truncated instead of stomping over random memory space.


    ye you right here, im not used to vsprintf yet and i sometimes
    prototyping codes (some could say - prototyping codes is normal
    (by prototyping i mean writing it in not optimal state taking edge cases
    and so on just to move faster)




    Also note that with a "static" buffer, you'll get a mess (and UB) if you call the function from two threads at the same time.ÿ That may or may
    not matter to you.


    thsi ttotally no problem i write 1 core


    as to this static buffer it has his barbaric style but im not sure its strictly proper..probably stack (VLA) based solution would be more
    proper but there is a problem

    sometimes i code c in c++ compiler mode (i dont care co even check as
    its close and i compile sometimes c mode sometimes c++) and its said c++
    not support VLA - this is also bad becouse the best would be the subset
    od c/c++ to compile in both


    i distaste c++ much though i began to think if to even use references as
    c should have references in fact, allows codes be shorter

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From fir@3:633/10 to All on Fri Sep 11 18:46:45 2026
    fir pisze:
    David Brown pisze:
    On 11/09/2026 14:28, fir wrote:
    say yo may this case - its a printf like wrapper who just renders
    text on screen bitmap (though i could use it to render also on some
    ram bitmaps - so the length of text used here is limited to size of
    bitmap,
    say i rarelu use bitmaps larger than say 8k and rarely fonts
    smaller than 10 pixel wide so the 8k/10 = 800 length at most) -

    i know its under those mentioned assumptions okbut how to do it
    properly?




    ÿÿstatic BMFont* tahomaÿ = NULL;
    ÿÿint tahoma_sizeÿ = 23;

    ÿÿvoid text_xy_colors_tahoma(int x, int y, unsigned* colors, char*
    format, ...)
    ÿÿ{
    ÿÿÿ if(!tahoma)ÿ tahomaÿ = AllocFont("Tahoma", tahoma_size, 500);

    ÿÿÿ static char text[1000];

    ÿÿÿ va_list args;
    ÿÿÿ va_start(args, format);
    ÿÿÿ vsprintf(text, format, args);
    ÿÿÿ va_end(args);

    ÿÿÿ DrawText_BM_colors(tahoma, x,y,ÿ text, colors);
    ÿÿ}

    IMHO, it is fine to have a fixed buffer size like this.ÿ But you
    really should use "vsnprintf(text, sizeof(text), format, args);".
    Then if your code accidentally uses too long a message, it gets
    truncated instead of stomping over random memory space.


    ye you right here, im not used to vsprintf yet and i sometimes
    prototyping codes (some could say - prototyping codes is normal
    (by prototyping i mean writing it in not optimal state taking edge cases
    and so on just to move faster)




    Also note that with a "static" buffer, you'll get a mess (and UB) if
    you call the function from two threads at the same time.ÿ That may or
    may not matter to you.


    thsi ttotally no problem i write 1 core


    as to this static buffer it has his barbaric style but im not sure its strictly proper..probably stack (VLA) based solution would be more
    proper but there is a problem

    sometimes i code c in c++ compiler mode (i dont care co even check as
    its close and i compile sometimes c mode sometimes c++) and its said c++
    not support VLA - this is also bad becouse the best would be the subset
    od c/c++ to compile in both


    i distaste c++ much though i began to think if to even use references as
    c should have references in fact, allows codes be shorter

    as to this VLA's it seems that there should be some mechanism that allow
    to client function to use it as its kinda infinite

    char vtab[0];

    vla_vsprintf(vtab, format, args);

    the client code vould need probbaly to call some function or use
    keyword like

    vla_set_size(vtab, 200); something like that..

    it is not present as far as i know

    im not proably a fan as i once said that normal callstack be holding
    local variables probably there should be few separate stacks

    im not sure hevever how many separate like callstack should only hold retvalues where im not sure if srhuments, static size locals, and
    thise vlas should have 3 separate or 2 or 1







    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From fir@3:633/10 to All on Fri Sep 11 18:59:43 2026
    fir pisze:
    fir pisze:
    David Brown pisze:
    On 11/09/2026 14:28, fir wrote:
    say yo may this case - its a printf like wrapper who just renders
    text on screen bitmap (though i could use it to render also on some
    ram bitmaps - so the length of text used here is limited to size of
    bitmap,
    say i rarelu use bitmaps larger than say 8k and rarely fonts
    smaller than 10 pixel wide so the 8k/10 = 800 length at most) -

    i know its under those mentioned assumptions okbut how to do it
    properly?




    ÿÿstatic BMFont* tahomaÿ = NULL;
    ÿÿint tahoma_sizeÿ = 23;

    ÿÿvoid text_xy_colors_tahoma(int x, int y, unsigned* colors, char*
    format, ...)
    ÿÿ{
    ÿÿÿ if(!tahoma)ÿ tahomaÿ = AllocFont("Tahoma", tahoma_size, 500);

    ÿÿÿ static char text[1000];

    ÿÿÿ va_list args;
    ÿÿÿ va_start(args, format);
    ÿÿÿ vsprintf(text, format, args);
    ÿÿÿ va_end(args);

    ÿÿÿ DrawText_BM_colors(tahoma, x,y,ÿ text, colors);
    ÿÿ}

    IMHO, it is fine to have a fixed buffer size like this.ÿ But you
    really should use "vsnprintf(text, sizeof(text), format, args);".
    Then if your code accidentally uses too long a message, it gets
    truncated instead of stomping over random memory space.


    ye you right here, im not used to vsprintf yet and i sometimes
    prototyping codes (some could say - prototyping codes is normal
    (by prototyping i mean writing it in not optimal state taking edge
    cases and so on just to move faster)




    Also note that with a "static" buffer, you'll get a mess (and UB) if
    you call the function from two threads at the same time.ÿ That may or
    may not matter to you.


    thsi ttotally no problem i write 1 core


    as to this static buffer it has his barbaric style but im not sure its
    strictly proper..probably stack (VLA) based solution would be more
    proper but there is a problem

    sometimes i code c in c++ compiler mode (i dont care co even check as
    its close and i compile sometimes c mode sometimes c++) and its said
    c++ not support VLA - this is also bad becouse the best would be the
    subset
    od c/c++ to compile in both


    i distaste c++ much though i began to think if to even use references as
    c should have references in fact, allows codes be shorter

    as to this VLA's it seems that there should be some mechanism that allow
    to client function to use it as its kinda infinite

    char vtab[0];

    vla_vsprintf(vtab, format, args);

    the client codeÿ vould need probbaly to call some function or use
    keyword like

    vla_set_size(vtab, 200); something like that..

    it is not present as far as i know

    im not proably a fan as i once saidÿ that normal callstack be holding
    local variables probably there should be few separate stacks

    im not sure hevever how many separate like callstack should only hold retvalues where im not sure if srhuments, static size locals, and
    thise vlas should have 3 separate or 2 or 1



    in morern times i would say im not a fan of ststic buffer of
    such type as in this code i would consider even malloc

    char* text = (char*)malloc(10000);

    free(text);

    probably better but i would afraid that it has notable overhead some
    and such calls of this kind of functions could be often maybe


    so maybe thinkig of it its better to have one pice of trash ram for all
    such temporary usage?

    char temp_ram[10*1024*1024];

    void text_xy_colors_tahoma(int x, int y, unsigned* colors, char*
    format, ...)
    {
    if(!tahoma) tahoma = AllocFont("Tahoma", tahoma_size, 500);

    va_list args;
    va_start(args, format);
    vsprintf(temp_ram, format, args);
    va_end(args);

    DrawText_BM_colors(tahoma, x,y, temp_ram, colors);
    }


    im close to starting using this..in a way this makes codes simpler
    (les names less instances)




    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From fir@3:633/10 to All on Fri Sep 11 19:31:23 2026
    fir pisze:


    in morern times i would say im not a fan of ststic buffer of
    such type as in this code i would consider even malloc

    char* text = (char*)malloc(10000);

    free(text);

    probably better but i would afraid that it has notable overhead some
    and such calls of this kind of functions could be often maybe


    so maybe thinkig of it its better to have one pice of trash ram for all
    such temporary usage?

    char temp_ram[10*1024*1024];

    void text_xy_colors_tahoma(int x, int y, unsigned* colors, char*
    ÿformat, ...)
    ÿÿ {
    ÿÿÿÿ if(!tahoma)ÿ tahomaÿ = AllocFont("Tahoma", tahoma_size, 500);

    ÿÿÿ va_list args;
    ÿÿÿÿ va_start(args, format);
    ÿÿÿÿ vsprintf(temp_ram, format, args);
    ÿÿÿÿ va_end(args);

    ÿÿÿÿ DrawText_BM_colors(tahoma, x,y,ÿ temp_ram, colors);
    ÿÿ }


    im close to starting using this..in a way this makes codes simpler
    (les names less instances)




    i will probbaly try it (maybe i use some other name like temp_buffer
    maybe better some shorter but unique?)

    i think koncet of usinh it when using widelly could be quite more
    usefull, becouse api calls could communicate thru this and it make
    avoiding some names typing

    this in back times could be considered bad design but when using with a
    head maybe it could be good..at least its better than local static buffers


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From fir@3:633/10 to All on Fri Sep 11 19:38:22 2026
    fir pisze:
    fir pisze:


    in morern times i would say im not a fan of ststic buffer of
    such type as in this code i would consider even malloc

    char* text = (char*)malloc(10000);

    free(text);

    probably better but i would afraid that it has notable overhead some
    and such calls of this kind of functions could be often maybe


    so maybe thinkig of it its better to have one pice of trash ram for
    all such temporary usage?

    char temp_ram[10*1024*1024];

    void text_xy_colors_tahoma(int x, int y, unsigned* colors, char*
    ÿÿformat, ...)
    ÿÿÿ {
    ÿÿÿÿÿ if(!tahoma)ÿ tahomaÿ = AllocFont("Tahoma", tahoma_size, 500);

    ÿÿÿÿ va_list args;
    ÿÿÿÿÿ va_start(args, format);
    ÿÿÿÿÿ vsprintf(temp_ram, format, args);
    ÿÿÿÿÿ va_end(args);

    ÿÿÿÿÿ DrawText_BM_colors(tahoma, x,y,ÿ temp_ram, colors);
    ÿÿÿ }


    im close to starting using this..in a way this makes codes simpler
    (les names less instances)




    i will probbaly try it (maybe i use some other name like temp_buffer
    maybe better some shorter but unique?)

    i think koncet of usinh it when using widelly could be quite more
    usefull, becouse api calls could communicate thru this and it make
    avoiding some names typing

    this in back times could be considered bad design but when using with a
    head maybe it could be good..at least its better than local static buffers


    maybe some name propositions?

    ai give me such jury notes

    Ja bym je oceni?a tak:

    tmp ? ????? ? kr¢tkie, klasyczne, ?atwe do rozpoznania
    tbuf ? ???? ? od razu sugeruje temporary buffer
    rtemp ? ???? ? runtime temporary / raw temporary, ale znaczenie r nie
    jest oczywiste
    temp ? ???? ? czytelne, ale nie m¢wi, ?e to bufor
    temp_buf ? ??? ? najbardziej jednoznaczne, ale troch? d?ugie
    tmpbuf ? ???? ? bardzo jednoznaczne, tylko 6 znak¢w
    tmp jako char tmp[10*1024*1024] ? moim zdaniem ca?kiem eleganckie.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Keith Thompson@3:633/10 to All on Fri Sep 11 11:03:36 2026
    David Brown <david.brown@hesbynett.no> writes:
    [...]
    IMHO, it is fine to have a fixed buffer size like this. But you
    really should use "vsnprintf(text, sizeof(text), format, args);".
    Then if your code accidentally uses too long a message, it gets
    truncated instead of stomping over random memory space.
    [...]

    Think carefully about what will happen if the string is truncated.
    If you're writing a human-readable log, for example, quietly
    truncating the string might be acceptable, though it might also be
    good to have an indication that the data was truncated (perhaps by
    appending "..." or "?" to the truncated message).

    If the potentially truncated string is going to be executed
    as a command, quiet truncation can be deadly. Consider what
    happens if you quietly truncate "rm -rf /home/username/tmpdir" to
    "rm -rf /home/username".

    Fortunately vsnprintf and related functions let you detect this.
    The return value is the number of characters that would have been
    if enough space had been available. If that's greater than or
    equal to the value you passed as the size argument, it was truncated.
    But it's (too) easy to forget to check.

    Some implementations provide non-standard asprintf() and vasprintf()
    functions that write to an allocated string.

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Fri Sep 11 23:23:43 2026
    On 11/09/2026 20:03, Keith Thompson wrote:
    David Brown <david.brown@hesbynett.no> writes:
    [...]
    IMHO, it is fine to have a fixed buffer size like this. But you
    really should use "vsnprintf(text, sizeof(text), format, args);".
    Then if your code accidentally uses too long a message, it gets
    truncated instead of stomping over random memory space.
    [...]

    Think carefully about what will happen if the string is truncated.
    If you're writing a human-readable log, for example, quietly
    truncating the string might be acceptable, though it might also be
    good to have an indication that the data was truncated (perhaps by
    appending "..." or "?" to the truncated message).

    If the potentially truncated string is going to be executed
    as a command, quiet truncation can be deadly. Consider what
    happens if you quietly truncate "rm -rf /home/username/tmpdir" to
    "rm -rf /home/username".

    Fortunately vsnprintf and related functions let you detect this.
    The return value is the number of characters that would have been
    if enough space had been available. If that's greater than or
    equal to the value you passed as the size argument, it was truncated.
    But it's (too) easy to forget to check.

    Some implementations provide non-standard asprintf() and vasprintf() functions that write to an allocated string.


    My common use-case (and I use this kind of thing a lot) is for debug log messages that only developers and testers will ever see, so truncation
    is fine for that purpose. But you make good points for other possible use-cases - truncation is not necessarily a harmless operation!



    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Scott Lurndal@3:633/10 to All on Fri Sep 11 22:37:14 2026
    David Brown <david.brown@hesbynett.no> writes:
    On 11/09/2026 20:03, Keith Thompson wrote:
    David Brown <david.brown@hesbynett.no> writes:
    [...]
    IMHO, it is fine to have a fixed buffer size like this. But you
    really should use "vsnprintf(text, sizeof(text), format, args);".
    Then if your code accidentally uses too long a message, it gets
    truncated instead of stomping over random memory space.
    [...]

    Think carefully about what will happen if the string is truncated.
    If you're writing a human-readable log, for example, quietly
    truncating the string might be acceptable, though it might also be
    good to have an indication that the data was truncated (perhaps by
    appending "..." or "?" to the truncated message).

    If the potentially truncated string is going to be executed
    as a command, quiet truncation can be deadly. Consider what
    happens if you quietly truncate "rm -rf /home/username/tmpdir" to
    "rm -rf /home/username".

    Fortunately vsnprintf and related functions let you detect this.
    The return value is the number of characters that would have been
    if enough space had been available. If that's greater than or
    equal to the value you passed as the size argument, it was truncated.
    But it's (too) easy to forget to check.

    Some implementations provide non-standard asprintf() and vasprintf()
    functions that write to an allocated string.


    My common use-case (and I use this kind of thing a lot) is for debug log >messages that only developers and testers will ever see, so truncation
    is fine for that purpose. But you make good points for other possible >use-cases - truncation is not necessarily a harmless operation!


    I've been known to use snprintf in place of strcat; in that usage it
    is necessary to check the return from snprintf (when the format
    string isn't constant width and programmer knows that space is
    available in the target buffer) to see if the buffer was exhausted.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)