Debugging C - Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cjava
    New Member
    • Feb 2008
    • 3

    Debugging C - Question

    Hi ,

    I was once asked to debug the following code. The code is attached as an image file:

    I have found the following Bugs but I would like some other thoughts too :

    1 ) Should Bool be a defined type then , succeeded should be initialized to False

    2 ) Line 28 : if size_t is defined as long in a 64 bit system then its passsed to uint32 which will make it lose higher 32 bits

    3 ) Malloc(0) is a possibility

    4) malloc may return error but its not checked or accounted

    5) String has to be NULL terminated

    6) *s is assigned the allocated and assigned the memory by s is passed..

    I hope there are lots more in this. Please post your thoughts
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by cjava
    Hi ,

    I was once asked to debug the following code. The code is attached as an image file:

    I have found the following Bugs but I would like some other thoughts too :

    1 ) Should Bool be a defined type then , succeeded should be initialized to False

    2 ) Line 28 : if size_t is defined as long in a 64 bit system then its passsed to uint32 which will make it lose higher 32 bits

    3 ) Malloc(0) is a possibility

    4) malloc may return error but its not checked or accounted

    5) String has to be NULL terminated

    6) *s is assigned the allocated and assigned the memory by s is passed..

    I hope there are lots more in this. Please post your thoughts

    1 ) Should Bool be a defined type then , succeeded should be initialized to False
    ===>Yes, The variable succeeded is not initialized.So in error cases it may return an undefined value
    3 ) Malloc(0) is a possibility
    ===>Yes.
    4) malloc may return error but its not checked or accounted
    ===>So after malloc check for null

    Are you expecting something else?

    Raghuram

    Comment

    • cjava
      New Member
      • Feb 2008
      • 3

      #3
      Hi raghu ,

      Thanks for replying. I was just curios if there are any other blatant error .

      This is one of the questions I was interviewed last year. I was told by the interviewer that there are around lots error. Thats why , I was curious if there are anyother errors.

      Comment

      Working...