RE: [PATCH 2/6] mgmt and concat_buf: added concat_buf api to util.h

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

 



> From: FUJITA Tomonori [fujita.tomonori@xxxxxxxxxxxxx]
> Sent: Thursday, January 12, 2012 2:04 AM

    > +struct concat_buf {
    > +     FILE *streamf;
    > +     int err;
    > +     int used;
    > +     int *size;
    > +};

>  What are 'used' and '*size' for?


    > +static inline void concat_buf_init(struct concat_buf *b, char **buf, int *size)
    > +{
    > +     b->streamf = open_memstream(buf, (size_t *)size);
    > +     b->err = b->streamf ? 0 : errno;
    > +     b->used = 0;
    > +     b->size = size;
    > +}


The user passes **buf and *size pointers, to be passed on to open_memstream().
Subsequent writes to FILE* will lead to allocation and possibly to re-allocations of
a memory buffer over which we have no control. We obtain the buffer only after fclose()
(or after fflush() which is irrelevant in our setting). The buffer is returned in the descriptor
referenced by **buf and *size.
The problem is that the value stored in *size does not account for the trailing NULL character.
Thus i store *size in concat_buf and increment the size once the file is closed and
the buffer descriptor gets finalized. It's done in concat_buf_finish(), below:


    > +static inline int concat_buf_finish(struct concat_buf *b)
    > +{
    > +     if (b->streamf) {
    > +             fclose(b->streamf);
    > +             b->streamf = NULL;
    > +             (*b->size) ++; /* account for trailing NULL char */


--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux SCSI]     [Linux RAID]     [Linux Clusters]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]

  Powered by Linux