|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I am wondering if the following code snippet has an issue, can
somebody tell me if the parameter passed to getPacket() is done the correct way? I expect packet to be populated with the contents. Is this valid? I am new to C. For now please assume that the size of the buffer passed (55) is sufficient. extern int getPacket(unsigned char* packet); static bool func1(message* msg) { unsigned char *cmd = NULL; short cmd_len; unsigned char packet[55]; if (getPacket(packet) == REJECTED) { printf("FAIL: getPacket returns REJECTED\n"); return; /* no packet to read */ } cmd = (unsigned char*) packet; cmd_len = packet[1]; } |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
creamsoda.c@gmail.com schrieb:
> static bool func1(message* msg) > { > unsigned char *cmd = NULL; > short cmd_len; > unsigned char packet[55]; > > if (getPacket(packet) == REJECTED) "packet" is uninitialized. Greetings, Johannes -- "Viele der Theorien der Mathematiker sind falsch und klar Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka HJP in de.sci.mathematik <4740ad67$0$3811$5402220f@news.sunrise.ch> |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
In article <jmhe15xctp.ln2@joeserver.homelan.net>,
Johannes Bauer <dfnsonfsduifb@gmx.de> wrote: >creamsoda.c@gmail.com schrieb: >> static bool func1(message* msg) >> { >> unsigned char *cmd = NULL; >> short cmd_len; >> unsigned char packet[55]; >> >> if (getPacket(packet) == REJECTED) > >"packet" is uninitialized. Not necessarily. The bare reference to packet devolves to a pointer to the first element of packet, which is just what you would want if getPacket() is passed the address of a buffer that it fills in. -- "Okay, buzzwords only. Two syllables, tops." -- Laurie Anderson |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Johannes wrote:
) creamsoda.c@gmail.com schrieb: )> static bool func1(message* msg) )> { )> unsigned char *cmd = NULL; )> short cmd_len; )> unsigned char packet[55]; )> )> if (getPacket(packet) == REJECTED) ) ) "packet" is uninitialized. From the looks of it, it's probably meant to be initialized by the function getPacket. SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something.. No I'm not paranoid. You all think I'm paranoid, don't you ! #EOT |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Johannes Bauer <dfnsonfsdu...@gmx.de> wrote:
> creamsod...@gmail.com schrieb: [ extern int getPacket(unsigned char* packet); ] > > static bool func1(message* msg) > > { > > unsigned char *cmd = NULL; > > short cmd_len; > > unsigned char packet[55]; The magic constant is a bit of a worry. How does getPacket() know how much space it has? > > > > if (getPacket(packet) == REJECTED) > > "packet" is uninitialized. Neither is cmd_len, but like packet, there's no indication it should be. Presumably getPacket() will fill packet. Of course, I'm guessing since there's no context, but note that the pointer parameter does not point to a constant type. -- Peter |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
creamsoda.c@gmail.com wrote:
> I am wondering if the following code snippet has an issue, can > somebody tell me if the parameter passed to getPacket() is done the Without seeing the getPacket() code, who knows? > correct way? I expect packet to be populated with the contents. Is > this valid? I am new to C. For now please assume that the size of the > buffer passed (55) is sufficient. IIRC, Ethernet header has 16 octets, IPv4 require 20 octets, or it could be IPv6, or perhaps with a TCP header.. etc etc > extern int getPacket(unsigned char* packet); > > static bool func1(message* msg) > { > unsigned char *cmd = NULL; > short cmd_len; > unsigned char packet[55]; > > if (getPacket(packet) == REJECTED) I prefer if ( getPacket(packet, sizeof packet) ) or rc = getPacket(packet, sizeof packet) if ( REJECTED == rc ) > { > printf("FAIL: getPacket returns REJECTED\n"); > return; /* no packet to read */ return false; > } > > cmd = (unsigned char*) packet; why use cast here > cmd_len = packet[1]; and not here? Have you snipped away code? 'msg' unused, and no return true -- Tor <bwzcab@wvtqvm.vw | tr i-za-h a-z> |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Walter Roberson schrieb:
> In article <jmhe15xctp.ln2@joeserver.homelan.net>, > Johannes Bauer <dfnsonfsduifb@gmx.de> wrote: >> creamsoda.c@gmail.com schrieb: >>> static bool func1(message* msg) >>> { >>> unsigned char *cmd = NULL; >>> short cmd_len; >>> unsigned char packet[55]; >>> >>> if (getPacket(packet) == REJECTED) >> "packet" is uninitialized. > > Not necessarily. The bare reference to packet devolves to > a pointer to the first element of packet, which is just > what you would want if getPacket() is passed the address > of a buffer that it fills in. You (and all others) are of course right. I thought it was void getPacket(unsigned char*) Mea culpa! :-) Greetings, Johannes -- "Viele der Theorien der Mathematiker sind falsch und klar Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka HJP in de.sci.mathematik <4740ad67$0$3811$5402220f@news.sunrise.ch> |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Johannes Bauer schrieb:
> You (and all others) are of course right. I thought it was > > void getPacket(unsigned char*) const! const! const! Oh man. Enough making a fool out of myself for today ;-) Greetings, Johannes -- "Viele der Theorien der Mathematiker sind falsch und klar Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka HJP in de.sci.mathematik <4740ad67$0$3811$5402220f@news.sunrise.ch> |
|
![]() |
| Outils de la discussion | |
|
|