Changeset 30379f4 in serd


Ignore:
Timestamp:
07/30/17 05:16:57 (4 weeks ago)
Author:
David Robillard <d@…>
Branches:
serd1
Children:
1379291
Parents:
1cd3218
git-author:
David Robillard <d@…> (03/15/16 20:21:34)
git-committer:
David Robillard <d@…> (07/30/17 05:16:57)
Message:

Use SerdBuffer? for mutable buffers

This avoids const violations from abusing SerdChunk? as a mutable buffer
for string sinks.

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • NEWS

    r1cd3218 r30379f4  
     1serd (1.0.0) unstable; 
     2 
     3  * Use SerdBuffer for mutable buffers instead of abusing SerdChunk 
     4  * Add serd_node_new_relative_uri() 
     5  * Fix construction and comparison of URIs with UTF-8 characters 
     6  * Report I/O errors with message and return appropriate status code 
     7  * Report missing statement separator errors 
     8  * Fix potential out of bounds read 
     9  * Fix unaligned memory access, undefined behaviour which breaks on ARM 
     10  * Fix documentation generation 
     11  * Update serdi man page 
     12 
     13 -- David Robillard <d@drobilla.net>  Mon, 10 Jul 2017 11:28:59 +0200 
     14 
    115serd (0.29.1) unstable; 
    216 
  • serd/serd.h

    r4d535bb r30379f4  
    232232 
    233233/** 
     234   A mutable buffer in memory. 
     235*/ 
     236typedef struct { 
     237    void*  buf;  /**< Buffer */ 
     238    size_t len;  /**< Size of buffer in bytes */ 
     239} SerdBuffer; 
     240 
     241/** 
    234242   An error description. 
    235243*/ 
     
    951959   A convenience sink function for writing to a string. 
    952960 
    953    This function can be used as a SerdSink to write to a SerdChunk which is 
     961   This function can be used as a SerdSink to write to a SerdBuffer which is 
    954962   resized as necessary with realloc().  The `stream` parameter must point to 
    955    an initialized SerdChunk.  When the write is finished, the string should be 
    956    retrieved with serd_chunk_sink_finish(). 
     963   an initialized SerdBuffer.  When the write is finished, the string should be 
     964   retrieved with serd_buffer_sink_finish(). 
    957965*/ 
    958966SERD_API 
    959967size_t 
    960 serd_chunk_sink(const void* buf, size_t len, void* stream); 
    961  
    962 /** 
    963    Finish a serialisation to a chunk with serd_chunk_sink(). 
     968serd_buffer_sink(const void* buf, size_t len, void* stream); 
     969 
     970/** 
     971   Finish a serialisation to a chunk with serd_buffer_sink(). 
    964972 
    965973   The returned string is the result of the serialisation, which is NULL 
     
    968976SERD_API 
    969977uint8_t* 
    970 serd_chunk_sink_finish(SerdChunk* stream); 
     978serd_buffer_sink_finish(SerdBuffer* stream); 
    971979 
    972980/** 
  • src/node.c

    r4d535bb r30379f4  
    184184    } 
    185185 
    186     SerdChunk chunk = { uri, uri_len }; 
     186    SerdBuffer buffer = { uri, uri_len }; 
    187187    for (size_t i = 0; i < path_len; ++i) { 
    188188        if (evil && path[i] == '\\') { 
    189             serd_chunk_sink("/", 1, &chunk); 
     189            serd_buffer_sink("/", 1, &buffer); 
    190190        } else if (path[i] == '%') { 
    191             serd_chunk_sink("%%", 2, &chunk); 
     191            serd_buffer_sink("%%", 2, &buffer); 
    192192        } else if (!escape || is_uri_path_char(path[i])) { 
    193             serd_chunk_sink(path + i, 1, &chunk); 
     193            serd_buffer_sink(path + i, 1, &buffer); 
    194194        } else { 
    195195            char escape_str[4] = { '%', 0, 0, 0 }; 
    196196            snprintf(escape_str + 1, sizeof(escape_str) - 1, "%X", path[i]); 
    197             serd_chunk_sink(escape_str, 3, &chunk); 
     197            serd_buffer_sink(escape_str, 3, &buffer); 
    198198        } 
    199199    } 
    200     serd_chunk_sink_finish(&chunk); 
     200    serd_buffer_sink_finish(&buffer); 
    201201 
    202202    if (out) { 
    203         serd_uri_parse(chunk.buf, out); 
    204     } 
    205  
    206     return serd_node_from_substring(SERD_URI, chunk.buf, chunk.len); 
     203        serd_uri_parse(buffer.buf, out); 
     204    } 
     205 
     206    return serd_node_from_substring(SERD_URI, buffer.buf, buffer.len); 
    207207} 
    208208 
  • src/uri.c

    r0af4d3e r30379f4  
    7373    } 
    7474 
    75     SerdChunk chunk = { NULL, 0 }; 
     75    SerdBuffer buffer = { NULL, 0 }; 
    7676    for (const uint8_t* s = path; *s; ++s) { 
    7777        if (*s == '%') { 
    7878            if (*(s + 1) == '%') { 
    79                 serd_chunk_sink("%", 1, &chunk); 
     79                serd_buffer_sink("%", 1, &buffer); 
    8080                ++s; 
    8181            } else if (is_hexdig(*(s + 1)) && is_hexdig(*(s + 2))) { 
     
    8484                sscanf((const char*)code, "%X", &num); 
    8585                const uint8_t c = num; 
    86                 serd_chunk_sink(&c, 1, &chunk); 
     86                serd_buffer_sink(&c, 1, &buffer); 
    8787                s += 2; 
    8888            } else { 
     
    9090            } 
    9191        } else { 
    92             serd_chunk_sink(s, 1, &chunk); 
    93         } 
    94     } 
    95     return serd_chunk_sink_finish(&chunk); 
     92            serd_buffer_sink(s, 1, &buffer); 
     93        } 
     94    } 
     95    return serd_buffer_sink_finish(&buffer); 
    9696} 
    9797 
  • src/writer.c

    rf15d475 r30379f4  
    989989SERD_API 
    990990size_t 
    991 serd_chunk_sink(const void* buf, size_t len, void* stream) 
    992 { 
    993     SerdChunk* chunk = (SerdChunk*)stream; 
    994     chunk->buf = (uint8_t*)realloc((uint8_t*)chunk->buf, chunk->len + len); 
    995     memcpy((uint8_t*)chunk->buf + chunk->len, buf, len); 
    996     chunk->len += len; 
     991serd_buffer_sink(const void* buf, size_t len, void* stream) 
     992{ 
     993    SerdBuffer* buffer = (SerdBuffer*)stream; 
     994    buffer->buf = (char*)realloc(buffer->buf, buffer->len + len); 
     995    memcpy((uint8_t*)buffer->buf + buffer->len, buf, len); 
     996    buffer->len += len; 
    997997    return len; 
    998998} 
     
    10001000SERD_API 
    10011001uint8_t* 
    1002 serd_chunk_sink_finish(SerdChunk* stream) 
    1003 { 
    1004     serd_chunk_sink("", 1, stream); 
     1002serd_buffer_sink_finish(SerdBuffer* stream) 
     1003{ 
     1004    serd_buffer_sink("", 1, stream); 
    10051005    return (uint8_t*)stream->buf; 
    10061006} 
  • tests/serd_test.c

    rd292cd3 r30379f4  
    583583    serd_writer_free(writer); 
    584584 
    585     // Test chunk sink 
    586     SerdChunk chunk = { NULL, 0 }; 
     585    // Test buffer sink 
     586    SerdBuffer buffer = { NULL, 0 }; 
    587587    writer = serd_writer_new( 
    588         SERD_TURTLE, (SerdStyle)0, env, NULL, serd_chunk_sink, &chunk); 
     588        SERD_TURTLE, (SerdStyle)0, env, NULL, serd_buffer_sink, &buffer); 
    589589 
    590590    o = serd_node_from_string(SERD_URI, USTR("http://example.org/base")); 
     
    594594 
    595595    serd_writer_free(writer); 
    596     uint8_t* out = serd_chunk_sink_finish(&chunk); 
     596    uint8_t* out = serd_buffer_sink_finish(&buffer); 
    597597 
    598598    if (strcmp((const char*)out, "@base <http://example.org/base> .\n")) { 
    599         return failure("Incorrect chunk output:\n%s\n", chunk.buf); 
     599        return failure("Incorrect buffer output:\n%s\n", buffer.buf); 
    600600    } 
    601601 
  • wscript

    r1cd3218 r30379f4  
    1212# minor increment <=> compatible changes (additions) 
    1313# micro increment <=> no interface changes 
    14 SERD_VERSION       = '0.29.1' 
    15 SERD_MAJOR_VERSION = '0' 
     14SERD_VERSION       = '1.0.0' 
     15SERD_MAJOR_VERSION = '1' 
    1616 
    1717# Mandatory waf variables 
Note: See TracChangeset for help on using the changeset viewer.