Giter Site home page Giter Site logo

stlc's People

Contributors

joshiayush avatar thechampagne avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

stlc's Issues

Refactor to a more compact version of function `StringStreamWriteFile()`

Currently function StringStreamWriteFile() is not compact properly which you can clearly see in the below lines of code:

https://github.com/joshiayush/cjson/blob/269a939c6301c137c3516b878e0d4bee7be801c2/cjson/data/sstream/fileio.c#L59-L73

What we want to achieve is a more compact version of this function by replacing the if ladder into a single if clause:

void StringStreamWriteFile(StringStream* const sstream, FILE* const file, 
                            size_t begin, size_t end) { 
  if ((sstream == NULL || file == NULL) || (begin >= sstream->length) || !end ||
      (end < begin))
    return;
  if (end > sstream->length) 
    end = sstream->length; 
  fwrite(sstream->data + begin, sizeof(char), end - begin, file); 
 } 

`stringstream` - Broken resize routine

Description

Pointed out a serious bug when testing stringstream_alloc() function which takes in an argument of const size_t i.e., the number of char sized blocks to allocate in the free store.

stringstream instance has a length and a capacity instance in it which tells the number of characters a stringstream instance currently holds plus the number of characters it can hold respectively.

The way the capacity is calculated is broken, at least in here, as the spectrum the length is in is (0, +18446744073709551615) but the capacity will break as the length exceeds by 2147483648 (which is the decimal value of binary 10000000000000000000000000000000) which means the length will increase but at a certain point the following code will wipe out the entire memory the capacity instance has.

while (capacity < length)
  capacity <<= 1;

Possible Solutions

Leaving a simple Python list resize routine in case you want to start contributing to cjson.

typedef struct {
    PyObject_HEAD
    Py_ssize_t ob_size;

    /* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */
    PyObject **ob_item;

    /* ob_item contains space for 'allocated' elements.  The number
     * currently in use is ob_size.
     * Invariants:
     *     0 <= ob_size <= allocated
     *     len(list) == ob_size
     *     ob_item == NULL implies ob_size == allocated == 0
     */
    Py_ssize_t allocated;
} PyListObject;

As you can see PyListObject has a corresponding attribute allocated to capacity which is later calculated the following way:

new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
new_allocated += newsize;

where the newsize is the requested size for stringstream instance and the new_allocated is the new capacity for the stringstream instance.

Contributions are welcome!

`JSON_{LIST|OBJECT}_ADD_VAL` doesn't add the given data to the given `JSON` list

Using macro JSON_LIST_ADD_VAL() doesn't add the given data to the given JSON list instance which is clearly visible in the test CJSONTest.JSONStringifiyTestWithAListAndAObjectInstance.

After debugging it many times what I observed is, steps upto the following macro are properly doing their job,

https://github.com/joshiayush/cjson/blob/8f588d8e6c0e169847ccf54bbbbc173f1022bd26/cjson/modifiers.c#L44-L48

But when the control flow returns from the following function, everything just seems to be wiped out.

https://github.com/joshiayush/cjson/blob/8f588d8e6c0e169847ccf54bbbbc173f1022bd26/cjson/modifiers.c#L40-L42

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.