8000 Switch from strdup to memcpy to faithfully copy all bytes and prevent a crash in some cases. by tylerchurch · Pull Request #966 · nodegit/nodegit · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@tylerchurch
Copy link
Contributor

This is a pull request to fix the issue I created over here: #965

To summarize over here why I'm making these changes:

  • strdup expects a C-style null-terminated string, but sometimes we're using nodejs strings to pass in strings that contain nulls in the middle, thus failing to copy everything at best, and causing crashes from reading invalid memory locations at worst.
  • Thus I changed to using malloc, memcpy and memset to prevent these issues.

To explain my changes a little bit:

  • I make the malloc'ed string one byte longer so I can add a terminating null character, since it looks like these templates are used in a number of different places, and for things like a repo path, where we do want things to end in a null character.
  • I used memset instead of the more standard array-style access because most of the usages of these templates are const char *.
  • The funky casts are because some places have void * instead of const char *, so to get the pointer arithmetic right, I had to specify the char *.

C code isn't what I normally write, so let me apologize in advance if these changes aren't quite what they should be 😄

I'm happy to make any modifications necessary.

The limited testing I've done seems to show that everything is working perfectly well with these changes.

@tylerchurch
Copy link
Contributor Author

Okay, not sure what I did wrong to fail the Travis CI build, this is the only error I see:

> nodegit@0.11.9 coveralls /home/travis/build/nodegit/nodegit
> cat ./test/coverage/merged.lcov | coveralls


/home/travis/build/nodegit/nodegit/node_modules/coveralls/bin/coveralls.js:18
        throw err;
              ^
Bad response: 422 {"message":"Couldn't find a repository matching this job.","error":true}

Did I do that? Or is there some other error I'm missing? Or is there just some intermittent CI failure?

@johnhaley81
Copy link
Collaborator

@tylerchurch it's an intermittent CI failure. I restarted the test. I was seeing the same on #958

@johnhaley81
Copy link
Collaborator

Ok, that took me a moment to get my head around. Would you mind doing a summary of the comments above in the code? I know that later I'm going to look at that and go "WTF??"

@johnhaley81
Copy link
Collaborator

Thanks @tylerchurch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0