Ub sdk problem with sprintf


    Jul 04 2006 | 10:21 am
    Hi,
    I have crash problem when I use sprintf in external. I have installed the UB SDK, XCODE 2.3, max beta 4.6, mac OSX 10.4.5 in a mac mini duo.
    Here is the first line of the crash log :
    Host Name: ordinateur-de-dominique-ehret Date/Time: 2006-07-04 01:51:56.341 +0200 OS Version: 10.4.5 (Build 8H1619) Report Version: 4
    Command: MaxMSP Path: /Applications/MaxMSP 4.6/MaxMSP.app/Contents/MacOS/MaxMSP Parent: WindowServer [76]
    Version: ??? (4.6b11)
    PID: 1320 Thread: 0
    Exception: EXC_BAD_ACCESS (0x0001) Codes: KERN_PROTECTION_FAILURE (0x0002) at 0x14b5e874
    Thread 0 Crashed: 0 <> 0xffff07c7 __memcpy + 39 (cpu_capabilities.h:186) 1 libSystem.B.dylib 0x9000b639 __sfvwrite + 409 2 libSystem.B.dylib 0x9001070d __vfprintf + 19692 3 libSystem.B.dylib 0x9000ba10 sprintf + 480 4 com.cycling74.fenlist 0x14b4ef0d fenlist_handle_message_column(_fenlist*, symbol*, short, atom*) + 653 (fenlist.c:1453)
    and line of source code
    void fenlist_handle_message_column(t_fenlist *x, t_symbol *s, short argc, t_atom *argv) { fenlist_rDebug("start fenlist_handle_message_column"); fenlist_stripArgs(argc, argv); fenlist_dumpSymbolAndArgs(s, argc, argv, FENLIST_PROXY_GETINLET(x));
    char *colName; int colId; bool visible; char *colIdentificator; char *colType; int colWidth;
    if (FENLIST_PROXY_GETINLET(x) == 0) // left inlet { if ( (argc < 2) ) { error("misformated message"); goto bail; } //depending on the nb of args this could be read from a file. if (argc == 2) // in a patcher { fenlist_figureOutColumnId(x, &argc, argv, &colId);
    if (colId == -1) // no columnid used { if ((argv[0].a_type != A_LONG) || (argv[1].a_type != A_SYM) ) { error("misformated message"); goto bail; }
    colId = argv[0].a_w.w_long; colName = argv[1].a_w.w_sym->s_name; } else { if ( (argv[0].a_type != A_SYM) ) { error("misformated message"); goto bail; } colName = argv[0].a_w.w_sym->s_name; }
    tapi_changeColumName(colId, colName, x->m_table); dbapi_changeColumnNameInDataBrowser(colId, colName, x->m_browser); } else // from a file { fenlist_rDebug("read from a file"); colId = argv[0].a_w.w_long; colName = argv[1].a_w.w_sym->s_name; visible = (0==strcmp("visible", argv[2].a_w.w_sym->s_name)); colType = argv[3].a_w.w_sym->s_name; colWidth = argv[4].a_w.w_long; sprintf(tempstr, "%ld %d %s %d", colId, visible, colType, colWidth); fenlist_rDebug(tempstr);
    colIdentificator = "DefcolidXXX"; sprintf(colIdentificator, "Defcolid%ld", colId); fenlist_rDebug(colIdentificator); -------------------------- max crash at line sprintf(colIdentificator, "Defcolid%ld", colId);
    memcpy problem ?
    when I use char [] = "DefcolidXXX"; sprintf(colIdentificator, "Defcolid%ld", colId); I have no crash but with old framework and Xcode 2.2 crashing code with char * is working.
    Can I have help ? I have very big trouble with porting my code to UB. Sorry for my bad english ?
    Thanks
    dominique ehret

    • Jul 04 2006 | 10:40 am
      On 4 Jul 2006, at 12:21, dominique ehret wrote:
      > colIdentificator = "DefcolidXXX"; > sprintf(colIdentificator, "Defcolid%ld", colId);
      This is deeply suspect: you're trying to use a constant string as a writable buffer. You should declare the buffer as a char[N] where N is big enough that you're never ever ever going to overrun it. Ever. Or use snprintf() if it's available.
      -- N.
      nick rothwell -- composition, systems, performance -- http:// www.cassiel.com
    • Jul 04 2006 | 11:45 am
      hi,
      the 'colId' variable is being used twice. I'd think that the first call to sprintf deallocates the data inside the 'colId' member. have u tried using memcpy for assignin 'argv[0].a_w.w_long' to 'colId' ? or create two members 'colId1' an 'colId2'... if that solves your problem, the data is being deallocated by the sprintf call...
      greetz david
      On 7/4/06, dominique ehret wrote: > > > Hi, > > I have crash problem when I use sprintf in external. I have installed the > UB SDK, XCODE 2.3, max beta 4.6, mac OSX 10.4.5 in a mac mini duo. > > Here is the first line of the crash log : > > Host Name: ordinateur-de-dominique-ehret > Date/Time: 2006-07-04 01:51:56.341 +0200 > OS Version: 10.4.5 (Build 8H1619) > Report Version: 4 > > Command: MaxMSP > Path: /Applications/MaxMSP 4.6/MaxMSP.app/Contents/MacOS/MaxMSP > Parent: WindowServer [76] > > Version: ??? (4.6b11) > > PID: 1320 > Thread: 0 > > Exception: EXC_BAD_ACCESS (0x0001) > Codes: KERN_PROTECTION_FAILURE (0x0002) at 0x14b5e874 > > Thread 0 Crashed: > 0 <> 0xffff07c7 __memcpy + 39 (cpu_capabilities.h:186) > 1 libSystem.B.dylib 0x9000b639 __sfvwrite + 409 > 2 libSystem.B.dylib 0x9001070d __vfprintf + 19692 > 3 libSystem.B.dylib 0x9000ba10 sprintf + 480 > 4 com.cycling74.fenlist 0x14b4ef0d > fenlist_handle_message_column(_fenlist*, symbol*, short, atom*) + 653 ( > fenlist.c:1453) > > > and line of source code > > void fenlist_handle_message_column(t_fenlist *x, t_symbol *s, short argc, > t_atom *argv) > { > fenlist_rDebug("start fenlist_handle_message_column"); > fenlist_stripArgs(argc, argv); > fenlist_dumpSymbolAndArgs(s, argc, argv, FENLIST_PROXY_GETINLET(x)); > > char *colName; > int colId; > bool visible; > char *colIdentificator; > char *colType; > int colWidth; > > if (FENLIST_PROXY_GETINLET(x) == 0) // left inlet > { > if ( (argc < 2) ) > { > error("misformated message"); > goto bail; > } > //depending on the nb of args this could be read from a file. > if (argc == 2) // in a patcher > { > fenlist_figureOutColumnId(x, &argc, argv, &colId); > > if (colId == -1) // no columnid used > { > if ((argv[0].a_type != A_LONG) || > (argv[1].a_type != A_SYM) ) > { > error("misformated message"); > goto bail; > } > > colId = argv[0].a_w.w_long; > colName = argv[1].a_w.w_sym->s_name; > } > else > { > if ( (argv[0].a_type != A_SYM) ) > { > error("misformated message"); > goto bail; > } > colName = argv[0].a_w.w_sym->s_name; > } > > tapi_changeColumName(colId, colName, x->m_table); > dbapi_changeColumnNameInDataBrowser(colId, colName, > x->m_browser); > } > else // from a file > { > fenlist_rDebug("read from a file"); > colId = argv[0].a_w.w_long; > colName = argv[1].a_w.w_sym->s_name; > visible = (0==strcmp("visible", argv[2].a_w.w_sym->s_name)); > colType = argv[3].a_w.w_sym->s_name; > colWidth = argv[4].a_w.w_long; > sprintf(tempstr, "%ld %d %s %d", colId, visible, colType, > colWidth); > fenlist_rDebug(tempstr); > > colIdentificator = "DefcolidXXX"; > sprintf(colIdentificator, "Defcolid%ld", colId); > fenlist_rDebug(colIdentificator); > -------------------------- > max crash at line sprintf(colIdentificator, "Defcolid%ld", colId); > > memcpy problem ? > > when I use char [] = "DefcolidXXX"; > sprintf(colIdentificator, "Defcolid%ld", colId); I have no crash but with > old framework and Xcode 2.2 crashing code with char * is working. > > Can I have help ? I have very big trouble with porting my code to UB. > Sorry for my bad english ? > > Thanks > > dominique ehret > > > > > >
    • Jul 04 2006 | 12:36 pm
      Hi Dominique,
      On 4 juil. 06, at 12:21, dominique ehret wrote:
      > sprintf(tempstr, "%ld %d %s %d", colId, visible, colType, > colWidth); > fenlist_rDebug(tempstr);
      You don't seem to allocate some memory for tempstr (I guess it is global), make sure it's big enough
      > colIdentificator = "DefcolidXXX"; > sprintf(colIdentificator, "Defcolid%ld", colId);
      same here, are you sure colIdentificator is big enough, I think a %ld may take more than 3 char. You could define colIdentificator as char[256].
      _____________________________ Patrick Delges
      Centre de Recherches et de Formation Musicales de Wallonie asbl http://users.skynet.be/crfmw/max
    • Jul 04 2006 | 3:39 pm
      On 4 Jul 2006, at 14:36, Patrick Delges wrote:
      >> colIdentificator = "DefcolidXXX"; >> sprintf(colIdentificator, "Defcolid%ld", colId); > > same here, are you sure colIdentificator is big enough, I think a % > ld may take more than 3 char. > You could define colIdentificator as char[256].
      You're right, but a more fundamental problem is that the constant string is not writable. If this has worked in the past, it's by fluke, and I would expect most compilers to reject it. Ah, except it's C... so the correct behaviour is to do something unpredictable, which is what indeed appears to be happening.
      nick rothwell -- composition, systems, performance -- http:// www.cassiel.com
    • Jul 04 2006 | 3:59 pm
    • Jul 05 2006 | 10:17 pm
      Thanks for quickly reply.
      I use char char colIdentificator[32] and code is running. Yes, tempstr is global and the size is big [1024]. I use a table API in C++ and it is not easy for me to share data of table with max code, temptr do this job in max code because the size of data is unpredictable. Making C code is not my primary job and I am very happy when I have help. Thanks for reply an the best for dev member forum.
      Dominique Ehret