473,791 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Very strange error

There is some windows specific code in this, but I believe the issue to
be something standard so...

The problem is that after attaching the name of the subdirectory I have
something like "d:\db\db-4.1.25\", which has 16 characters; but when it
arrives in the next call it looks more like "d:\db\db-4.1.25\(clubs)" ,
which has 17 characters. What is even more interesting is that the test
"if (dir[strlen(dir) - 1] != '\\')" fails when there is a clubs at the
end of the string!

Where am I screwing up?

NR - code follows

void search_director y(char *dir, char *match, void (*deal)(const
char*,const char *))
{
WIN32_FIND_DATA find_data;
HANDLE h_search;
int finished = 0;
char *search_string;

if (dir[strlen(dir) - 1] != '\\') dir[strlen(dir) - 1] = '\0';
else fprintf(stderr, "It is %c", dir[strlen(dir) - 1]);
search_string = malloc(strlen(d ir) + 1);
sprintf(search_ string, "%s*", dir);
fprintf(stderr, "%d", strlen(dir)); putchar('\n');

fprintf(stderr, search_string);

h_search = FindFirstFile(s earch_string, &find_data);
if (h_search == INVALID_HANDLE_ VALUE)
{
fprintf(stderr, "Can't do stuff\n");
exit(1);
}

while (!finished)
{
if (!(find_data.dw FileAttributes & FILE_ATTRIBUTE_ DIRECTORY))
{
int begin = strlen(find_dat a.cFileName) - strlen(match);
if (!strcmp(find_d ata.cFileName + begin, match))
{
deal(dir, find_data.cFile Name);
}

}
else if (strcmp(find_da ta.cFileName, ".") &&
strcmp(find_dat a.cFileName, ".."))
{
fprintf(stderr, "Going down...");
char *newdir = malloc(strlen(d ir) + strlen(find_dat a.cFileName));
memset(newdir, 0, strlen(dir) + strlen(find_dat a.cFileName));
sprintf(newdir, "%s%s\\", dir, find_data.cFile Name);
fprintf(stderr, "%d", strlen(newdir)) ; putchar('\n');
search_director y(newdir, match, deal);
}

if (!FindNextFile( h_search, &find_data))
{
if (GetLastError() == ERROR_NO_MORE_F ILES)
{
finished = 1;
}
else
{
fprintf(stderr, "BROKEN\n") ;
exit(1);
}
}
}
}

Output:
D:\progs>a.exe d:\ file.txt
It is \3
d:\*Going down...11
It is \11
d:\.xemacs\*Goi ng down...8
It is \8
d:\argo\*Going down...15
It is \15
d:\convert-biz\*Going down...6
It is \6
d:\db\*Going down...16
It is \17
d:\db\db-4.1.25\?*Can't do stuff

Nov 13 '05 #1
8 1594
Noah Roberts wrote:
There is some windows specific code in this, but I believe the issue to
be something standard so...

The problem is that after attaching the name of the subdirectory I have
something like "d:\db\db-4.1.25\", which has 16 characters; but when it
arrives in the next call it looks more like "d:\db\db-4.1.25\(clubs)" ,
which has 17 characters. What is even more interesting is that the test
"if (dir[strlen(dir) - 1] != '\\')" fails when there is a clubs at the
end of the string!


More on the problem....

It appears that something is happening in malloc:

fprintf(stderr, "%s is %d characters",dir , strlen(dir)); putchar('\n');
search_string = malloc(strlen(d ir) + 1);
fprintf(stderr, "after malloc %s is %d characters",dir , strlen(dir));
putchar('\n');

Result:
d:\db\db-4.1.25\ is 16 characters
after malloc d:\db\db-4.1.25\? is 17 characters

As far as I know there should be no overruns here, right?
Nov 13 '05 #2
Noah Roberts wrote:

There is some windows specific code in this, but I believe the issue to
be something standard so...
I haven't read through the whole thing, but here's
one trouble spot:
search_string = malloc(strlen(d ir) + 1);
sprintf(search_ string, "%s*", dir);


You allocate enough space for a copy of `dir' (and
forget to check for malloc() failure, by the way), and
then you try to store `dir' plus an extra '*' -- one
more character than you've alloted space for. What
happens next is anyone's guess.

--
Er*********@sun .com
Nov 13 '05 #3
Eric Sosman wrote:

You allocate enough space for a copy of `dir' (and
forget to check for malloc() failure, by the way), and
then you try to store `dir' plus an extra '*' -- one
more character than you've alloted space for. What
happens next is anyone's guess.

That was apparently the problem that resulted in quite a few very
strange behaviors.

Thanks
NR

Nov 13 '05 #4
On Thu, 13 Nov 2003 13:44:18 -0800, Noah Roberts
<nr******@donte mailme.com> wrote:
Noah Roberts wrote:
There is some windows specific code in this, but I believe the issue to
be something standard so...

The problem is that after attaching the name of the subdirectory I have
something like "d:\db\db-4.1.25\", which has 16 characters; but when it
arrives in the next call it looks more like "d:\db\db-4.1.25\(clubs)" ,
which has 17 characters. What is even more interesting is that the test
"if (dir[strlen(dir) - 1] != '\\')" fails when there is a clubs at the
end of the string!
More on the problem....

It appears that something is happening in malloc:

fprintf(stderr, "%s is %d characters",dir , strlen(dir)); putchar('\n');
search_string = malloc(strlen(d ir) + 1);
fprintf(stderr, "after malloc %s is %d characters",dir , strlen(dir));
putchar('\n' );


Errors are also likely in on the heap because you don't allocate
enough space for your string here, as well as where Eric pointed at.
char *newdir = malloc(strlen(d ir) + strlen(find_dat a.cFileName));
memset(newdir, 0, strlen(dir) + strlen(find_dat a.cFileName));
sprintf(newdir, "%s%s\\", dir, find_data.cFile Name);


The \ and terminating null require space, so you need +2 in the malloc
of newdir.

If your implementation has a maximum allowable path length, you could
avoid all of the mallocs and frees by using local character arrays for
your buffers. Just add a sanity check that you're not exceeding the
maximum length when building a sub-folder's name.

- Sev
Nov 13 '05 #5
Jim
On Thu, 13 Nov 2003 13:31:11 -0800, Noah Roberts
<nr******@donte mailme.com> wrote:

not so strange!
search_string = malloc(strlen(d ir) + 1);
sprintf(search_ string, "%s*", dir);
allocation is 1 byte too small
char *newdir = malloc(strlen(d ir) + strlen(find_dat a.cFileName));
memset(newdir, 0, strlen(dir) + strlen(find_dat a.cFileName));
sprintf(newdir, "%s%s\\", dir, find_data.cFile Name);


allocation is 2 bytes too small

strlen("abc") == 3, but it takes 4 bytes to store, since there's a
hidden trailing '\0' character.

Jim
Nov 13 '05 #6
On Thu, 13 Nov 2003 14:39:31 -0800, Noah Roberts
<nr******@donte mailme.com> wrote:

Eric Sosman wrote:

You allocate enough space for a copy of `dir' (and
forget to check for malloc() failure, by the way), and
then you try to store `dir' plus an extra '*' -- one
more character than you've alloted space for. What
happens next is anyone's guess.

That was apparently the problem that resulted in quite a few very
strange behaviors.

Nasal daemons? GIF! GIF!
--
#include <standard.discl aimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 13 '05 #7
Jim wrote:
strlen("abc") == 3, but it takes 4 bytes to store, since there's a
hidden trailing '\0' character.
Yes, I was doing things too fast apparently. strlen is 1 past the end
of the string, I just remembered that fact but forgot that it is the
true size and still need 1 more.
Jim


Nov 13 '05 #8
Noah Roberts wrote:

[snip stuff]

What is really funny is that I got it to work, and work beutifully. But
now the specs on the program have changed to the point that I highly
doubt any of it to be useful :P First it was, "Search the drive for a
file and rename it to something else." Now it is, "Get a list of
outlook identities, display some summary information (email, servers,
etc), let the user pick one and move the correct file in just that
identity. So now I am searching various places in the CURRENT_USER key
of the registry and will be doing barely any directory work at all :P

NR

Nov 13 '05 #9

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

Similar topics

3
1481
by: Shelly | last post by:
I have come across a strange problem. I am setting up a registration screen with username, password, confirm password, and a couple of other things. The submit button is named "Submit". The others are named "username", "password", and "passwordConfirm". The problem is that when I enter a username and a password of 5 characters with nothing in the passwordConfirm field, I do not get the diagnostic echo messages that I have inserted. ...
14
6353
by: Allcomp | last post by:
Hello, I have seen something really strange in VB6 If I do a Int ( (5 * 1.2)) , I receive the value 5, but I should receive 6? Is this a bug or something really "normal". I can see that if I do ? int ((5 * 1.2 + 0.0000000000000003)) I receive 6. If I add something smaller, I have 5 as a result What is strange is that If I do a ? 5*1.2, I receive 6 so (5 * 1.2)
6
8538
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
2
1788
by: TB | last post by:
I am seeing a very strange problem as follows... I have a loop where a fair amount of processing is going on and near the top of the loop I access a class that has only static helper functions to perform some calculations. After some number of iterations, randomly, I'll get an uncaught NullValueException error on one of these calls, as if the class name is being treated as an object reference and is null. Here is some psuedo-code to...
5
1697
by: cody | last post by:
I have a very funny/strange effect here. if I let the delegate do "return prop.GetGetMethod().Invoke(info.AudioHeader, null);" then I get wrong results, that is, a wrong method is called and I have no clue why. But if I store the MethodInfo in a local variable I works as expected. I do not understand why this is so, shouldn't both ways be semantically equal?
2
1506
by: Shapper | last post by:
Hello, I have this code: Dim cultureList(,) As String = {{"E", "en-GB"}, {"P", "pt-PT"}} Select Case Session("culture") Case "pt-PT" ... Dim cultureList(,) As String = {{"E", "en-GB"}, {"P", "pt-PT"}} Response.Write("1")
2
1417
by: Buddy Ackerman | last post by:
I have a web app that I have setup on numerous web servers. I've set one up for a new client at their hosting facility and cannot get it to connect to their database. I get a "SQL Server does not exist or access denied." error. Well, the strangeness is that I have a SQL Query tool installed on this server and can connect to the database fine using the exact same connection parameters that I have specified in my web app. Even more strange...
1
8190
by: Don Rixtown | last post by:
I ran into a very strange error tonight. I was working with web services and typed datasets. The web server I was using happens to be on the other end of a virtual network (Hamachi). Everything was working fine. I added one more row of data to the table and all of a sudden one web method started failing. I initially thought the web service was timing out but after a while the following exception was thrown: The CLR has been unable to...
4
1144
by: Efy | last post by:
Hi, I was debugging my JavaScript in VS2005, the script probably had an error some ware (Line 25), after I fixed the error, I am trying to run the page again I am getting the same error "Illegal argument" at that same line 25. What ever I did does not help, I have closed the program, I restarted my computer, I changed the page to contain very simple html script with no js the error is still there on line 25. I deleted all lines living...
11
1853
by: VijaKhara | last post by:
Hi all, I just write a very simple codes in C and vthere is a very strange bug which I cannot figure out why. The first loop is for v, and the second for k. There is no relationship between v and k but if I debug and watch the change of the variable after each command. When the sencond loop happends for k, the values of vs change and are set to be equal some values of k. Specifically, v is
0
9669
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
9515
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
10207
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
10155
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
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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
5431
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...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.