PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Is there a problem with this code?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Is there a problem with this code?

Réponse
 
LinkBack Outils de la discussion
Vieux 21/11/2007, 19h25   #1
creamsoda.c@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Is there a problem with this code?

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];


}


  Réponse avec citation
Vieux 21/11/2007, 20h40   #2
Johannes Bauer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a problem with this code?

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>
  Réponse avec citation
Vieux 21/11/2007, 20h58   #3
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a problem with this code?

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
  Réponse avec citation
Vieux 21/11/2007, 21h00   #4
Willem
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a problem with this code?

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
  Réponse avec citation
Vieux 21/11/2007, 21h02   #5
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a problem with this code?

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
  Réponse avec citation
Vieux 21/11/2007, 22h13   #6
Tor Rustad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a problem with this code?

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>
  Réponse avec citation
Vieux 21/11/2007, 23h45   #7
Johannes Bauer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a problem with this code?

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>
  Réponse avec citation
Vieux 22/11/2007, 00h29   #8
Johannes Bauer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a problem with this code?

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>
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h55.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19542 seconds with 16 queries