473,799 Members | 3,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is wrong with this code?

Hello,

I am wondering whats wrong with the following code. It is crashing on
Linux, but works fine on AIX.

The function basically get DbNm@Srvr string and then return DbNm and
Srvr back to the calling program.
#define IDFR_VAL_SIZE 100

int rmlPvtGetDbNm(c har *sDb, char *sSrvr, char *sDbNm)
{
char *sTok;

memset(sDb, '\0', IDFR_VAL_SIZE);
memset(sSrvr, '\0', IDFR_VAL_SIZE);

sTok = malloc(IDFR_VAL _SIZE);

sTok = strtok( sDbNm, "@" );
strcpy(sDb, sTok);

sTok = strtok(NULL, "@" );
strcpy(sSrvr, sTok);

free(sTok);

if (memcmp(sDb, "NO-DB", 5) ==0)
{
return 1;
}
return 0;

}
with regards,

prabh
Nov 13 '05 #1
3 5305
On 28 Jul 2003 20:37:22 -0700, pr*******@video tron.ca (Prabh) wrote in
comp.lang.c:
Hello,

I am wondering whats wrong with the following code. It is crashing on
Linux, but works fine on AIX.

The function basically get DbNm@Srvr string and then return DbNm and
Srvr back to the calling program.
#define IDFR_VAL_SIZE 100

int rmlPvtGetDbNm(c har *sDb, char *sSrvr, char *sDbNm)
{
char *sTok;

memset(sDb, '\0', IDFR_VAL_SIZE);
memset(sSrvr, '\0', IDFR_VAL_SIZE);
Where are the arrays of characters that sDb and sSrvr point to
allocated or defined? If either of them actually points to less than
100 characters, you are writing past into memory you don't own, a good
way to crash.
sTok = malloc(IDFR_VAL _SIZE);

sTok = strtok( sDbNm, "@" );
This is a memory leak. You allocate 100 characters to sTok, then
throw away the allocated memory by assigning the return value of
strtok() to it. Poof, 100 bytes leaked and gone.
strcpy(sDb, sTok);
strtok() can return a null pointer. Passing a null pointer as the
second argument to strcpy() is undefined behavior and can cause a
crash. Alternatively, if the first '@' character is more than 100
characters into sDbNm, you'll overflow
sTok = strtok(NULL, "@" );
strcpy(sSrvr, sTok);
Likewise, strtok() can return a null pointer here. Never use the
value returned by strtok() without testing for NULL.
free(sTok);
This is almost certainly what is causing your crash. You allocated
memory to sTok, then you lost the pointer to it by overwriting it with
the return value of strtok(), not once, but twice.

So now you are trying to free sTok. But the value you are passing to
free() is not the pointer value returned by malloc().
if (memcmp(sDb, "NO-DB", 5) ==0)
{
return 1;
}
return 0;

}
with regards,

prabh


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #2
On Mon, 28 Jul 2003 20:37:22 -0700, Prabh wrote:
Hello,

I am wondering whats wrong with the following code. It is crashing on
Linux, but works fine on AIX.
I'm sure IBM would help you fix^H^H^H port your code.
The function basically get DbNm@Srvr string and then return DbNm and
Srvr back to the calling program.

#define IDFR_VAL_SIZE 100

int rmlPvtGetDbNm(c har *sDb, char *sSrvr, char *sDbNm)
{
char *sTok;

memset(sDb, '\0', IDFR_VAL_SIZE);
memset(sSrvr, '\0', IDFR_VAL_SIZE);

sTok = malloc(IDFR_VAL _SIZE);

sTok = strtok( sDbNm, "@" );
You've just lost the memory from the malloc.
strcpy(sDb, sTok);

sTok = strtok(NULL, "@" );
strcpy(sSrvr, sTok);

free(sTok);
Now you are free'ing a random offset into sDbNm.
if (memcmp(sDb, "NO-DB", 5) ==0)
{
return 1;
}
return 0;

}


You also assume a lot of things about the sizes of the objects and don't
check any return values (and the memcmp() at the end implies to me that
the strtok()'s could fail).

--
James Antill -- ja***@and.org
Need an efficent and powerful string library for C?
http://www.and.org/vstr/

Nov 13 '05 #3
Prabh wrote:
Hello,

I am wondering whats wrong with the following code. It is crashing on
Linux, but works fine on AIX.

The function basically get DbNm@Srvr string and then return DbNm and
Srvr back to the calling program.
#define IDFR_VAL_SIZE 100

int rmlPvtGetDbNm(c har *sDb, char *sSrvr, char *sDbNm)
{
char *sTok;

memset(sDb, '\0', IDFR_VAL_SIZE);
memset(sSrvr, '\0', IDFR_VAL_SIZE);

sTok = malloc(IDFR_VAL _SIZE);

sTok = strtok( sDbNm, "@" );
strcpy(sDb, sTok);

sTok = strtok(NULL, "@" );
strcpy(sSrvr, sTok);

free(sTok);

if (memcmp(sDb, "NO-DB", 5) ==0)
{
return 1;
}
return 0;

}


You overwrite the value returned from malloc() not once, but three
times! No wonder free() gets confused. Even worse, there was never any
need to malloc() or free() anything. See the code below to see just how
much simpler your code could have been. Why your code "worked" before is
anyone's guess. One correct way of your code's working is to crash.
#include <string.h>
#include <stdio.h>

#define IDFR_VAL_SIZE 100

int rmlPvtGetDbNm(c har *sDb, char *sSrvr, char *sDbNm)
{
char *sTok;

if ((sTok = strtok(sDbNm, "@")))
strcpy(sDb, sTok);

if ((sTok = strtok(NULL, "@")))
strcpy(sSrvr, sTok);

return (memcmp(sDb, "NO-DB", 5) == 0);
}
int main(void)
{
char input[IDFR_VAL_SIZE] = "DbNm@Srvr" ;
char dbnm[IDFR_VAL_SIZE], srvr[IDFR_VAL_SIZE];
printf("return value was: %d\n", rmlPvtGetDbNm(d bnm, srvr, input));
printf("dbnm: %s, srvr: %s\n", dbnm, srvr);
return 0;
}

Nov 13 '05 #4

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

Similar topics

3
6659
by: RobertTG | last post by:
Someone please translate the code below into English... Particularly the indicated line Thanks function attachComment() { var aForms = document.getElementsByTagName("FORM"); for (var i = 0; i < aForms.length; i++) {
13
2143
by: dbuchanan | last post by:
This code resets a form with two cbo's (comboBoxes) and one datagrid. The first cbo (cboSelection) selects a main table and filters the second cbo. The second cbo (cboView) selects the secondary table which determine the dataAdapter used to fill the dataGrid. Both cbo's are populated by filling dataAdapters. This code just empty's the datagrid, cbo's and dataset so the user can start over to view another set of data. \\
9
1985
by: oddvark | last post by:
Hello, under vc7.1 this code compiles: if (!parent.fillTool) parent.fillTool.dispose; where dispose is a method of fillTool. Notice that dispose does not have ( ) behind it. Under vc8, this generates an error.
1
1293
by: locy | last post by:
can someone explain to me "what does this code do" class baseclass { public: virtual void runme() { std::cout<<"are you"<<std::endl; }
5
2101
by: lucas | last post by:
is a javascript file; is ajax? or javascript but in hidden code? eval(function(p,a,c,k,e,d) {e=function(c) {return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))}; if(!''.replace(/^/,String)) {while(c--){d=k||e(c)}k= }]; e=function(){return'\\w+'};
1
1284
maher5
by: maher5 | last post by:
hi i had some help completing my assignment which i still dont understand fully. i dont know what this bit of the code means/do. the assignment was the morse code convertion. if(cinput == 0x0a || cinput == 0x0a) {
3
2048
by: qianz99 | last post by:
Hi I am not sure what this code does. I have the following questions 1. where is the case? 2. #define TLV_INTEGER(name, octets) p->name = -1; Is it define a function TLV_INTEGER(name, octets) and return a -1? and similar questions on other #define 3. in #define PDU(name, id, fields) \
6
3062
by: sbcs | last post by:
I'm a website developer. Recently I've found variations of this code on the home pages of several of my sites. It triggers warnings in some anti-virus/malware programs but not in others. The pages are on different servers which leads me to believe the code is coming from me somehow. Can anyone tell me what it does? Is it possibly the output of a virus on my machine? If so, why would it appear on the remote version of the page and not my local...
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10250
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.