Git Product home page Git Product logo

Comments (7)

orangeduck avatar orangeduck commented on August 19, 2024

Thanks a lot for the error report. I'll check it out as soon as I get time.

from mpc.

hellerve avatar hellerve commented on August 19, 2024

Just dropping by to say that using gcc 4.9 on OS X 10.10, I can not reproduce the bug.

from mpc.

orangeduck avatar orangeduck commented on August 19, 2024

Hey,

Can you verify if this is fixed in 9481c65?

Definitely one of the more perculiar bugs I've come across.

I managed to reproduce something similar in Ubuntu using gcc. It seems that the call to calloc in mpcf_escape_new was getting optimised away into something odd and making accessing the pointer cause some kind of address violation.

First I tried switching calloc to malloc and zeroing the memory manually. This appeared to fix the issue on my side. I then tried just switching the variable declaration order so that calloc was called last - which also appeared to fix the issue.

It seems very weird that switching the variable declaration order would fix this problem (it shouldn't matter), so it may be an obscure bug in the compiler - because I couldn't spot anything obviously wrong with my code.

Thanks,

Dan

from mpc.

swhalen avatar swhalen commented on August 19, 2024

Hi Dan,

I'm afraid it doesn't seem to be fixed. However, I can't reproduce the bug with gcc, and valgrind doesn't find anything (regardless of whether I use gcc or clang) so it's quite possible this is a false positive on the part of clang's address sanitizer.

That said, I did a bit of primitive printf tracing and have found that the problem (if indeed it is a problem) occurs in a call to vsprintf in mpc_err_string_cat, to be specific:

void mpc_err_string_cat(char *buffer, int *pos, int *max, char const *fmt, ...) {
  /* TODO: Error Checking on Length */
  int left = ((*max) - (*pos));
  va_list va;
  va_start(va, fmt);
  if (left < 0) { left = 0;}
  (*pos) += vsprintf(buffer + (*pos), fmt, va);  // Clang detects problem here
  va_end(va);
}

At this point in the program, the argument fmt has been produced by mpc_err_char_unescape. Adding a null-terminator to this string prevents the address sanitizer from complaining:

static char char_unescape_buffer[4];  // Used to be [3]

static const char *mpc_err_char_unescape(char c) {

  char_unescape_buffer[0] = '\'';
  char_unescape_buffer[1] = ' ';
  char_unescape_buffer[2] = '\'';
  char_unescape_buffer[3] = '\0';  // Line added

  switch (c) {

    case '\a': return "bell";
    case '\b': return "backspace";
    case '\f': return "formfeed";
    case '\r': return "carriage return";
    case '\v': return "vertical tab";
    case '\0': return "end of input";
    case '\n': return "newline";
    case '\t': return "tab";
    case ' ' : return "space";
    default:
      char_unescape_buffer[1] = c;
      return char_unescape_buffer;
  }

}

I'm sorry that I have no idea whether this change breaks any other functionality in the library!

Cheers,
Simon

from mpc.

orangeduck avatar orangeduck commented on August 19, 2024

Oh that looks like a very real bug in the mpc_err_char_unescape function! Nice catch.

I'll make the update and merge it in. My suspicion is that gcc has some different vsprintf implementation that doesn't rely on a null terminated format string but that maybe was screwing with some memory elsewhere due to no null terminator and causing the weird calloc bug. I'll test it and see.

from mpc.

orangeduck avatar orangeduck commented on August 19, 2024

Please verify if this is fixed in d6347af and thanks again.

from mpc.

swhalen avatar swhalen commented on August 19, 2024

I can confirm that this is fixed in d6347af. Thanks, this discussion has been educational for me!

from mpc.

Related Issues (20)

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.