473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Invalid read of size 1 [valgrind warning]

Hello, All!

I already posted my question and received valuable feedbacks, I changed my
code as was proposed here but still receive the same error of valgrind. SO,
the code is:

#define DB_IS_FLD_EMPTY(x) (((x) && *(x)) ? 0 : 1)
int db_is_fld_empty(char *fld)
{
return DB_IS_FLD_EMPTY(fld);
}

static int cli_auth2()
{
...
/* the value of 'raw' is fetched from DB */
...
if ( db_is_fld_empty(row[2+idx]) )
...
}

Valgrind's error is:

1 errors in context 1 of 10:
Invalid read of size 1
at 0x804A896: db_is_fld_empty (db.c:147)
by 0x804B94F: cli_auth2 (daemon.c:130)
by 0x804C002: process (daemon.c:336)
by 0x804CADA: main (daemon.c:567)

The value of 'raw' is definitely not NULL, otherwise code would never jump
to the specified loop. Is it possible 'valgrind' makes wrong assumption and
warns me?

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Dec 12 '05 #1
5 16129
Roman Mashak wrote:
Hello, All!

I already posted my question and received valuable feedbacks, I changed my
code as was proposed here but still receive the same error of valgrind. SO,
the code is:

#define DB_IS_FLD_EMPTY(x) (((x) && *(x)) ? 0 : 1)
int db_is_fld_empty(char *fld)
{
return DB_IS_FLD_EMPTY(fld);
}

static int cli_auth2()
{
...
/* the value of 'raw' is fetched from DB */
...
if ( db_is_fld_empty(row[2+idx]) )
...
}
<snip>
The value of 'raw' is definitely not NULL, otherwise code would never jump
to the specified loop. Is it possible 'valgrind' makes wrong assumption and
warns me?


That's possible, it's also possibly that you go off the end of the block
of memory. We don't know because you have not provided a complete
compilable program together with sample input exhibiting the problem.
Until you do that all anyone can do is guess. So I suggest changing line
42 of your source to:
a = 6 * 9;
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 12 '05 #2
Roman Mashak wrote:
Hello, All!

I already posted my question and received valuable feedbacks, I changed my
code as was proposed here but still receive the same error of valgrind. SO,
the code is:

#define DB_IS_FLD_EMPTY(x) (((x) && *(x)) ? 0 : 1)


<snip to EOF>

Why do you use the conditional operator? IMO the following would be
much easier to read:

#define DB_IS_FLD_EMPTY(x) (!((x) && *(x)))

Dec 12 '05 #3
Roman Mashak wrote:
Hello, All!

I already posted my question and received valuable feedbacks, I changed my
code as was proposed here but still receive the same error of valgrind. SO,
the code is:

#define DB_IS_FLD_EMPTY(x) (((x) && *(x)) ? 0 : 1)
int db_is_fld_empty(char *fld)
{
return DB_IS_FLD_EMPTY(fld);
}

static int cli_auth2()
{
...
/* the value of 'raw' is fetched from DB */
...
if ( db_is_fld_empty(row[2+idx]) )
...
}

Valgrind's error is:

1 errors in context 1 of 10:
Invalid read of size 1
at 0x804A896: db_is_fld_empty (db.c:147)
by 0x804B94F: cli_auth2 (daemon.c:130)
by 0x804C002: process (daemon.c:336)
by 0x804CADA: main (daemon.c:567)


You need to provide the smallest but *complete* piece of code that will
compile properly and demonstrate your issue. What you posted isn't
complete, for example, what is row? If row is an array of char then
try db_is_fld_empty(&row[2+idx]).

Robert Gamble

Dec 12 '05 #4
Hello, Robert!
You wrote on 12 Dec 2005 05:27:45 -0800:

RG> You need to provide the smallest but *complete* piece of code that will
RG> compile properly and demonstrate your issue. What you posted isn't
I should apologize in advance for possible inconvenience, because compilable
piece of code demonstrating 'valgrind' warnings is pretty large and may be
offtopic here (because it uses MySQL library which has nothing with standard
C), anyway I'll take a chance.

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mysql/mysql.h>

/* responses */
#define NS_UPDATE_PASS 3
#define NS_DELETE_PASS 4
#define NS_REGISTER_PASS 5

#define FAIL 11
#define NS_REGISTER_FAIL 16
#define DB_NO_DATA 17
#define AUTH_FAIL_1 18

#define BUFFLEN 1024

/* message type */
#define MSG_REQ 0x01
#define MSG_REPLY 0x02

/* client message */
typedef struct msg_s {
unsigned char msg_flag; /* 0-register, 1-modify, 2-delete */
unsigned char msg_type; /* message type: MSG_REQ, MSG_REPLY */
unsigned char msg_rc; /* reply code */
char msg_id[BUFFLEN];
char msg_passwd[BUFFLEN];
char msg_fqdn[BUFFLEN];
char msg_ipaddr[BUFFLEN];
char msg_serialno[BUFFLEN];
} msg_t;

/* DB connection properties */
typedef struct db_connector_s {
char db_host[BUFFLEN];
char db_user[BUFFLEN];
char db_passwd[BUFFLEN];
char db_name[BUFFLEN];
} db_connector_t;

db_connector_t db_conn = {"127.0.0.1", "root", "core2003", "Cdisk2"};

char *db_query_str[] = {
"SELECT UserId,PassWord,FqDomainName1,FqDomainName2 from diskInfo WHERE
SerialNumber=\"%s\";",
"UPDATE diskInfo SET IpAddress=\"%s\",FqDomainName%d=\"%s\" WHERE
SerialNumber=\"%s\";",
"UPDATE diskInfo SET IpAddress=\"%s\",FqDomainName%d=\"\" WHERE
SerialNumber=\"%s\";",
"INSERT into diskInfo
(UserId,PassWord,FqDomainName%d,IpAddress,SerialNu mber,Date) VALUES (\"%s\",
\"%s\", \"%s\", \"%s\", \"%s\", \"%s\");",
"DELETE from diskInfo WHERE SerialNumber=\"%s\";"
};

/* db_init() - initialize DB object and establish connection */
int db_init(MYSQL *Conn, db_connector_t *c)
{
/* initialize the connection object here */
if ( mysql_init(Conn) == NULL )
return -1;

/* connect main database here */
if( !mysql_real_connect(Conn, c->db_host, c->db_user, c->db_passwd,
c->db_name, 0, NULL, 0) )
return -1;

/* select database */
if( mysql_select_db(Conn, c->db_name) )
return -1;

return 0;
}

/* db_close() - close previously opened connection */
void db_close(MYSQL *Conn)
{
mysql_close(Conn);
}

/* db_search() - run query and return pointer to result set */
MYSQL_RES* db_search(MYSQL *Conn, char szQuery[])
{
MYSQL_RES *res, *tmp_res;

res = malloc(sizeof(*res));
tmp_res = malloc(sizeof(*tmp_res));
if ( res == NULL || tmp_res == NULL )
return NULL;

if ( mysql_query(Conn, szQuery) ) {
free(res);
free(tmp_res);
return NULL;
}

tmp_res = mysql_store_result(Conn);
memcpy(res, tmp_res, sizeof(MYSQL_RES));
if ( !tmp_res ) {
/* must free allocated memory */
mysql_free_result(tmp_res);
free(res);
free(tmp_res);
return NULL;
}
else {
mysql_free_result(tmp_res);
return res;
}
}

#define DB_IS_FLD_EMPTY(x) (((x) && *(x)) ? 0 : 1)

/* db_is_field_empty() - return 1 if 'fld' is empty, otherwise 0 */
int db_is_fld_empty(char *fld)
{
return DB_IS_FLD_EMPTY(fld);
}

/* xstrstr() - search substring in string: 1 if found, 0 otherwise */
static int xstrstr(const char *s1, const char *s2)
{
return (strstr(s1, s2) == NULL ? 0 : 1);
}

/* authenticate client */
static int cli_auth2(MYSQL_RES *res, msg_t *m)
{
int fld_idx;
MYSQL_ROW row;

fld_idx = xstrstr(m->msg_fqdn, ".cdisk.co.kr");
if ( res ) {
if ( res->row_count == 0 ) {
return ( m->msg_flag == 0 ? _ERR_NO_ROWS : DB_NO_DATA );
}
else {
row = mysql_fetch_row(res);
if ( !strcmp(m->msg_id, row[0]) && !strcmp(m->msg_passwd, row[1]) ) {
/* ID-PASSWD pair match */
switch ( m->msg_flag ) {
case 0:
/* assume there are no both FQDNi empty fields */
if ( !db_is_fld_empty(row[3-fld_idx]) &&
!db_is_fld_empty(row[2+fld_idx]))
return NS_REGISTER_FAIL;
else
if ( !db_is_fld_empty(row[2]) && fld_idx == 1 )
return NS_REGISTER_FAIL;
else
if ( !db_is_fld_empty(row[3]) && fld_idx == 0 )
return NS_REGISTER_FAIL;
else
/*return NS_UPDATE_PASS;*/
return NS_REGISTER_PASS;
break;
default:
break;
}
}
else
return AUTH_FAIL_1;
} /* else */
}
else /* res is NULL */
return FAIL;

return FAIL;
}

/* process() - daemon's main tube, listen for connections and spawn */
int main(void)
{
msg_t *msg;
int r;

MYSQL conn;
MYSQL_RES *mysql_res;
char szQuery[BUFFLEN] = { 0 };

/* endless loop: get request and handle it */
if ( !(msg = malloc(sizeof(msg_t))) ) {
exit(EXIT_FAILURE);
}

msg->msg_flag = 0;
msg->msg_flag = MSG_REQ;
msg->msg_flag = 0;
strcpy(msg->msg_id, "us");
strcpy(msg->msg_passwd, "us");
strcpy(msg->msg_fqdn, "mrv.cdisk.co.kr");
strcpy(msg->msg_ipaddr, "192.168.1.1");
strcpy(msg->msg_serialno, "1234");

if (db_init(&conn, &db_conn) < 0) {
goto cleanup;
}

if ( (mysql_res = malloc(sizeof(MYSQL_RES))) == NULL ) {
goto cleanup;
}

memset(mysql_res, 0, sizeof(MYSQL_RES));

/* build query */
snprintf(szQuery, BUFFLEN, db_query_str[0], msg->msg_serialno);

mysql_res = db_search(&conn, szQuery);

switch ( r = cli_auth2(mysql_res, msg) ) {
case NS_REGISTER_PASS:
break;
case _ERR_NO_ROWS:
break;
case NS_UPDATE_PASS:
break;
case NS_DELETE_PASS:
break;
default:
break;
}

cleanup:
db_close(&conn);
free(msg);
free(mysql_res);
exit(EXIT_SUCCESS); /* for test only */

return 0;
}

Actually, the upper first error indicated by 'valgrind' seems to be related
to MySQL specific functions, so I suppose fixing of these errors may fix
others:

==13580== Invalid read of size 4
==13580== at 0x4069700: mysql_fetch_row (in
/usr/lib/libmysqlclient.so.15.0.0)
==13580== by 0x8048A5B: cli_auth2 (main.c:110)
==13580== by 0x8048CF7: main (main.c:181)
==13580== Address 0x4330B0C is 20 bytes inside a block of size 8,164 free'd
==13580== at 0x401B12C: free (vg_replace_malloc.c:235)
==13580== by 0x4044472: my_no_flags_free (in
/usr/lib/libmysqlclient.so.15.0.0)
==13580== by 0x40476BF: free_root (in /usr/lib/libmysqlclient.so.15.0.0)
==13580== by 0x4066560: free_rows (in /usr/lib/libmysqlclient.so.15.0.0)
==13580== by 0x40669CA: mysql_free_result (in
/usr/lib/libmysqlclient.so.15.0.0)
==13580== by 0x8048992: db_search (main.c:79)
==13580== by 0x8048CDD: main (main.c:179)

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Dec 13 '05 #5
Roman Mashak wrote:
Hello, Robert!
You wrote on 12 Dec 2005 05:27:45 -0800:

RG> You need to provide the smallest but *complete* piece of code that will
RG> compile properly and demonstrate your issue. What you posted isn't
I should apologize in advance for possible inconvenience, because compilable
piece of code demonstrating 'valgrind' warnings is pretty large and may be
offtopic here (because it uses MySQL library which has nothing with standard
C), anyway I'll take a chance.


[snip code and valgrind output]

These errors are different from the ones you originally asked about and
valgrind and MySQL are both off-topic here. At this point you
definitely need to find a group that deals with MySQL API issues.
Before you do though, you should try to isolate the code causing the
error to the smallest possible complete program, you should be able to
due this in about 20 lines of code or less. If you do this you will be
in a better position to figure out the problem for yourself and someone
else will be more likely to assist you if you still need help. You
should also explain exactly how your program misbehaves so that you are
not limiting yourself to answers from people who have experience with
valgrind.

Robert Gamble

Dec 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: AC Slater | last post by:
In regards to the following code: char tmp; myifstread.read(tmp,5); Does tmp = '\0' by definition? E.g. does .read put the null terminator? If not, why would it be that for months the...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
5
by: Janning Vygen | last post by:
Hi, tonight my database got corruppted. before it worked fine. since two days i do the following tasks every night psql -c 'CLUSTER;' $DBNAME psql -c 'VACUUM FULL ANALYZE;' $DBNAME ...
9
by: Roman Mashak | last post by:
Hello, All! I assume my post isn't offtopic here. I used 'valgrind' utility (guess plenty if you know and use it) for my application and I got the message "Invalid read of size 1" regarding...
36
by: gert | last post by:
Any comments why char **page doesn't reallocate #include <stdlib.h> void add(char **page,char *line,int n) { char **temp; if(temp=realloc(page,sizeof(char *)*(n+1))) {
5
by: deepakNagpal | last post by:
hi friends i am trying to create a contact feedback form. but when i am running the code it showing some warning: Please if anyone can help me. PHP Warning: mail() : SMTP server response:...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
6
by: webinfinite | last post by:
Hi, How to find a STL class size, for example: string. I would like to know what the size of the implementation of this class, I am not interested in the size of its object but the class itself....
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.