freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Git][freetype/freetype][master] [base] Avoid UB with memcpy


From: Ben Wagner (@bungeman)
Subject: [Git][freetype/freetype][master] [base] Avoid UB with memcpy
Date: Thu, 17 Aug 2023 19:56:23 +0000

Ben Wagner pushed to branch master at FreeType / FreeType

Commits:

  • a9793fea
    by Ben Wagner at 2023-08-17T13:25:31-04:00
    [base] Avoid UB with memcpy
    
    `FT_NEW_ARRAY(p, 0)` sets `p` to `NULL`. `FT_Stream_ReadAt` with a
    memory based stream uses `FT_MEM_COPY` which is `memcpy` which specifies
    that it is undefined behavior for either the `src` or `dst` to be
    `NULL`. Instead of forcing all callers work around calling
    `FT_Stream_Read` when `buffer == NULL && count == 0` do the check in
    `FT_StreamRead`. This allows any call with `count == 0` to succesfully
    read zero bytes without UB.
    
    * src/base/ftstream.c (FT_Stream_ReadAt): skip `FT_MEM_COPY` when
    `count == 0`. (FT_Stream_TryRead): ditto
    
    Fixes: #1250
    

1 changed file:

Changes:

  • src/base/ftstream.c
    ... ... @@ -141,7 +141,9 @@
    141 141
           if ( read_bytes > count )
    
    142 142
             read_bytes = count;
    
    143 143
     
    
    144
    -      FT_MEM_COPY( buffer, stream->base + pos, read_bytes );
    
    144
    +      /* Allow "reading" zero bytes without UB even if buffer is NULL */
    
    145
    +      if ( count )
    
    146
    +        FT_MEM_COPY( buffer, stream->base + pos, read_bytes );
    
    145 147
         }
    
    146 148
     
    
    147 149
         stream->pos = pos + read_bytes;
    
    ... ... @@ -178,7 +180,9 @@
    178 180
           if ( read_bytes > count )
    
    179 181
             read_bytes = count;
    
    180 182
     
    
    181
    -      FT_MEM_COPY( buffer, stream->base + stream->pos, read_bytes );
    
    183
    +      /* Allow "reading" zero bytes without UB even if buffer is NULL */
    
    184
    +      if ( count )
    
    185
    +        FT_MEM_COPY( buffer, stream->base + stream->pos, read_bytes );
    
    182 186
         }
    
    183 187
     
    
    184 188
         stream->pos += read_bytes;
    


  • reply via email to

    [Prev in Thread] Current Thread [Next in Thread]