read int from char * question

Started by
39 comments, last by ApochPiQ 6 years, 10 months ago

inline int read_int_from_stream(int count, int & index, char * src)
{
char * psrc = &src[index];
int dst;
memcpy(&dst, psrc, (size_t)count);
index = index + count;
psrc = 0;

return dst;
}

is this code above correct, i need that to know because this is the one of two functions i use and i need to find out whoch one is causing problems

Advertisement

have you tried calling the function and see if the correct value is returned?

that being said, if you are trying to convert "10" to 10, then no, this wont work.

Look into build in functions like http://www.cplusplus.com/reference/cstdlib/strtol/

Write a couple of unit tests to check it works. It's some work, but it saves you asking at forums, and getting responses / discussion.

The "psrc = 0" is quite usless at least, as the variable goes out of scope 2 line further.

Hard to be certain without context. This appears to be for deserialization? I think it has a happy path that could work as expected.

For example:

int main()
{
   int x = 42;
   char buffer[sizeof(x)];
   memcpy(buffer, &x, sizeof(int));
 
   int index = 0;
   int result = read_int_from_stream(sizeof(int), index, buffer);
   printf("Result: %d\n", result);
}

That said, there are a few things you could change about the code.

A likely issue is if the caller passes a value of "count" that is not equal to "sizeof(int)". If "count" is bigger, then you have undefined behaviour (likely manifesting as overwriting adjacent memory to "dst"). If "count" is smaller, then you have undefined behaviour as some part of "dst" remains uninitialised, likely manifesting as unexpected return values. It would be simpler to remove the parameter and use sizeof(int) rather than accept a parameter that could be potentially incorrect.

Other possible issues include:

  • Caller passes a negative "count"
  • Caller passes an invalid "src" buffer pointer (e.g. uninitialised, null)
  • Buffer pointer is valid but points to uninitialised data
  • Buffer data was never generated from the bit pattern of an int
  • Buffer data comes from another system with different endianess or sizeof(int)
  • "index" is or becomes out of range with respect to the buffer length
  • Also, it would be nice if "src" were const.

    it seems ftp client along with http receives some garbage data, that cannot be decoded.\

    and yes i did the conversion it works.


    inline int read_int_from_stream(int count, int & index, char * src)
    {
    char * psrc = &src[index]; // you use address of a very pointer, why not char * psrc=(src+index*4) ..considerin' if int is 4 chars
    int dst;
    memcpy(&dst, psrc, (size_t)count);
    index = index + count;
    psrc = 0;

    return dst;
    }

    I am writting in this separate post, that you should stop making code in C++, at least temporarily, to stress it out. For your own wellness and security. Unless this code you posted is for a very educational purpose of your own. Now to your mad code...

    inline int read_int_from_stream(int count, int & index, char * src)

    you use index variable of int type as a parameter to be only red from, yet you pass it as a reference, mind blowing
    {
    char * psrc = &src[index];

    you have a pointer char* src to bytes, but you use address of this pointer, and use index variable reference for indexing !
    int dst;
    memcpy(&dst, psrc, (size_t)count);

    you copy from some random space in memory arbitrary size of bytes into a local variable of int type!
    index = index + count;

    you add to a memory reference an integer
    psrc = 0;

    return dst;
    }

    So, you realy need to learn something about memory unmanaged languages and approach them much more carefully.

    JohnnyCode, not really sure you know what you are talking about.

    you use index variable of int type as a parameter to be only red from, yet you pass it as a reference, mind blowing

    it is actually NOT read from only as evident by the index = index + count line of the code

    char * psrc = &src[index]; you have a pointer char* src to bytes, but you use address of this pointer, and use index variable reference for indexing !

    This gets a pointer to a specific location within the src buffer. What is wrong with that? He could have just did char* psrc = src + index, but either way same thing and I see nothing wrong with it.

    int dst; memcpy(&dst, psrc, (size_t)count); you copy from some random space in memory arbitrary size of bytes into a local variable of int type!

    He is not copying from "some random space in memory". It is from the space he specifically calculated as being in the src buffer passed in. It is somewhat "arbitrary" the number of bytes as it is a passed in parameter. That would be better to use sizeof(int) if he is indeed reading it into an int. And yes he is reading it into a local variable....but he is returning that value at the end. What is wrong with that?

    index = index + count; you add to a memory reference an integer

    index is an int. count is an int. how is that a memory reference adding to an integer? he is using index to guess what, index in his array!!! How else would you index other locations in an array or buffer without adding to the index?

    So, you realy need to learn something about memory unmanaged languages and approach them much more carefully.

    pot. kettle. black.

    JohnnyCode, not really sure you know what you are talking about.

    you use index variable of int type as a parameter to be only red from, yet you pass it as a reference, mind blowing

    it is actually NOT read from only as evident by the index = index + count line of the code

    You are right on this one, but both, count and index, are number values that exist on the parameter scope- out of the function, so it is at least strange for someone to pass an int (the index)- and, without knowing it, earn it altered.

    char * psrc = &src[index]; you have a pointer char* src to bytes, but you use address of this pointer, and use index variable reference for indexing !

    This gets a pointer to a specific location within the src buffer. What is wrong with that? He could have just did char* psrc = src + index, but either way same thing and I see nothing wrong with it.

    No, it uses address of a 32bit/64bit pointer, then adds index to it, what is an address of some int

    int dst; memcpy(&dst, psrc, (size_t)count); you copy from some random space in memory arbitrary size of bytes into a local variable of int type!

    He is not copying from "some random space in memory". It is from the space he specifically calculated as being in the src buffer passed in. It is somewhat "arbitrary" the number of bytes as it is a passed in parameter. That would be better to use sizeof(int) if he is indeed reading it into an int. And yes he is reading it into a local variable....but he is returning that value at the end. What is wrong with that?

    No, he specificaly calculated a random point in memory literaly, becouse of what I mentioned upper, and if count is a bigger number, code will attempt to put more bytes to int dst variable.

    index = index + count; you add to a memory reference an integer

    index is an int. count is an int. how is that a memory reference adding to an integer? he is using index to guess what, index in his array!!! How else would you index other locations in an array or buffer without adding to the index?

    index is a reference, an address to memory where the passed parameter is, count is an int, all integer numbers, thus can be added.

    So, you realy need to learn something about memory unmanaged languages and approach them much more carefully.

    pot. kettle. black.

    You should, or deal with uncorrectable memory leaks.

    This topic is closed to new replies.

    Advertisement