[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

problem with strncpy


we have a weird problem with the strncpy function from glibc.
First, is this the right list for this question ?


void table_set_param(Table *table, char *name, int count, long index) {
  int i;
  for(i = 0; i < TABLE_NAME_LENGTH; i++) {
    table->name[i] = '\0';
  strncpy(table->name, name, TABLE_NAME_LENGTH-1);
  table->columnCount  = count;
  table->columnIndex  = index;
  table->ebene3Index  = 0;
  table->ebene3Count  = 0;

This function is called many times in our software. After
processing some gigabytes of data the strncpy fails reproducable
exactly one time. All following calls are working.
Whats wrong? 
It looks to me strncpy starts 8 bytes after the source adress
to copy data.


Breakpoint 3, table_set_param (table=0x60000ffffffedec0, name=0x4000000000022808 "TSTICHWORT", count=2, index=6917546619827052592) at suchmaschine/table.c:21
21        strncpy(table->name, name, TABLE_NAME_LENGTH-1);
(gdb) print name
$1 = 0x4000000000022808 "TSTICHWORT"
(gdb) print table->name
$6 = '\0' <repeats 254 times>
(gdb) next
22        table->columnCount  = count;
(gdb) print table->name
$2 = "RT", '\0' <repeats 252 times>               <=== PROBLEM !!!!

if i do the copy by hand in gdb after that again, everything works:
(gdb) print strncpy(table->name, name, 20)
$3 = -74048
(gdb) print table->name
$4 = "TSTICHWORT", '\0' <repeats 244 times>

Has anybody heard of such a problem, 
or am I completely wrong ???

Mit freundlichen Grüßen

Markus Bernhardt

Software Consulting Markus Bernhardt GmbH
Spieljochstr. 34  Phone: +49-89-420903-14
81825 München     Fax:   +49-89-420903-20

Reply to: