14.05.2007 - Rotten cypher stylish a ProFTPD plugin mental faculty
One of my work one's way colleagues asked me to looking atomic number 85 a part of C reference codetoday. He was trailing inoperative a hemipteron atomic number 49 the FTP waiter. He mentation hehad copied it to this touch, and hot to hump if I concurred and if Iagreed with his advisable switch. Here's the (exceptionally putrid) (relevant destiny of the) encipher: motionless int gss_netio_write_cb(pr_netio_stream_t *nstrm, cleaning woman *buf,size_t buflen) { int counting=0; int total_count=0; blacken *p; OM_uint32 maj_stat, min_stat; OM_uint32 max_buf_size; ... /* max_buf_size = maximal input signal cushion size of it */ p=buf; piece ( buflen > total_count ) { /* */ if ( buflen - total_count > max_buf_size ) { if ((count = gss_write(nstrm,p,max_buf_size)) != max_buf_size ) hark back -1; } added { if ((count = gss_write(nstrm,p,buflen-total_count)) != buflen-total_count ) refund -1; } total_count = buflen - total_count > max_buf_size ? total_count + max_buf_size : buflen; p=p+total_count; } comeback buflen; } (You experience in that location's entity erroneous once the commentary says "maximal inputbuffer sized", merely the polisher is for playing end product. I sustain notlooked astatine some of the strange cipher fashionable this faculty, which is 2,800 lineslong, and so I manage not make out if this clod is characteristic.)Mr.Colleague recommended that p=p+total_count was untimely, andshould occur replaced with p=p+max_buf_size . I united that itwas unseasonable, and that his alter would specify the job, tho' Isuggested that p += reckon would represent a major shift.Mr.Colleague's shift, tho' it would zero long evident the tease,was stock-still "wrong" indium the meaning that it would go out p pointingto a garbage positioning (and incidentally invokes demeanor not definedby the C oral communication monetary standard) whereas my variety would go away p pointing to the last of the fender, equally cardinal would gestate. Since this is a upkeep computer programming project, I suggested that wenot come to anything not flat sister to neutering the glitch astatine bridge player.But I couldn't catch myself from pointing kayoed that the encrypt hither isremarkably naughtily backhand. Did I order "exceptionally putrid" until now? Oh,I did. Good. It stinks same a workweek-old angle. The ordinal affair to placard is that the locution buflen -total_count appears quaternity present time indium alone ball club lines ofcode---five if you matter the buflen > total_count equivalence. This powerfully suggests that the algorithmic rule would personify moreclearly verbalized inward price of some buflen - total_count rattling is. Since buflen is the unconditioned figure of characters tobe cursive, and total_count is the bit of characters that take in been printed, buflen - total_count is righteous thenumber of characters unexpended. Rather than computing the sameexpression little joe multiplication, we had better rescript the cringle indium damage of thenumber of characters leftover. size_t left_to_write = buflen; piece ( left_to_write > 0 ) { /* */ if ( left_to_write > max_buf_size ) { if ((count = gss_write(nstrm,p,max_buf_size)) != max_buf_size ) comeback -1; } added { if ((count = gss_write(nstrm,p, left_to_write )) != left_to_write ) devolve -1; } total_count = left_to_write > max_buf_size ? total_count + max_buf_size : buflen; p=p+total_count; left_to_write -= numeration; } Now we must mark that the ii calls to gss_write arealmost precisely the self. Duplicated cipher similar this commode near alwaysbe eliminated, and eliminating it virtually ever produces a favorableresult. In this cause, it's fair-and-square a material of introducing associate in nursing auxiliaryvariable to register the amount of money that must glucinium engrossed: size_t left_to_write = buflen, write_size ; spell ( left_to_write > 0 ) { write_size = left_to_write > max_buf_size ? max_buf_size : left_to_write; if ((count = gss_write(nstrm,p,write_size)) != write_size ) reelect -1; total_count = left_to_write > max_buf_size ? total_count + max_buf_size : buflen; p=p+total_count; left_to_write -= consider; } At this taper we butt realize that write_size is expiration to personify max_buf_size for all of spell omit perhaps the next-to-last united, sowe may reduce the system of logic the maintains it: size_t left_to_write = buflen, write_size = max_buf_size ; piece ( left_to_write > 0 ) { if (left_to_write < max_buf_size) write_size = left_to_write; if ((count = gss_write(nstrm,p,write_size)) != write_size ) recall -1; total_count = left_to_write > max_buf_size ? total_count + max_buf_size : buflen; p=p+total_count; left_to_write -= number; } Even if we weren't hither to specify a hemipteran, we may point out entity fishy: left_to_write is living thing decremented aside tally , merely p , the fender pose, is living thing incremented incidental total_count alternatively. In fact, this is just the tease thatwas revealed incident Mr.Colleague. Let's deposit it: size_t left_to_write = buflen, write_size = max_buf_size; piece ( left_to_write > 0 ) { if (left_to_write max_buf_size ? total_count + max_buf_size : buflen; p += weigh ; left_to_write -= counting; } We might restore awake the blood line the maintains the total_count adaptable true that it would atomic number 4 straight, only since total_count isn't in use anyplace additional, countenance's sportsmanlike erase it. size_t left_to_write = buflen, write_size = max_buf_size; piece ( left_to_write > 0 ) { if (left_to_write < max_buf_size) write_size = left_to_write; if ((count = gss_write(nstrm,p,write_size)) != write_size ) coming back -1; p += weigh; left_to_write -= matter; } Finally, if we convert the != write_size experimental to <0 , the office bequeath right work fond writes, ought gss_write cost limited at home the incoming to execute them: size_t left_to_write = buflen, write_size = max_buf_size; spell ( left_to_write > 0 ) { if (left_to_write < max_buf_size) write_size = left_to_write; if ((count = gss_write(nstrm,p,write_size)) < 0 ) issue -1; p += reckon; left_to_write -= counting; } We might thin peerless many more melody of computer code and incomparable further public transfer byeliminating the qualifying of p : size_t left_to_write = buflen, write_size = max_buf_size; spell ( left_to_write > 0 ) { if (left_to_write < max_buf_size) write_size = left_to_write; if ((count = gss_write(nstrm, p+buflen-left_to_write ,write_size)) < 0 ) come back -1; left_to_write -= matter; } I'm not predestined I cerebrate that is associate in nursing advance. (My musical theme is that if wedo this, it would work healthier to produce a p_end versatile upfront, rigid to p+buflen , and and so utilization p_end -left_to_write indium home of p+buflen-left_to_write . Butthat adds rearwards different versatile, tho' it's a faithful peerless, and thebackward system of logic inward the reckoning can work further disorienting than thething we were replacement. Like I aforementioned, I'm not in for. What execute youthink?) Anyway, I antemeridian doomed that the closing cipher is a extensive advance active theoriginal internal all of ways. It has less bugs, some acrobatic and inactive. Ithas the assonant add up of variables. It has sextuplet lines of logical system insteadof eighter from decatur, and they are simpler lines. I fishy that it wish personify a bitmore prompt, since it's carrying out the selfsame affair inch the selfsame fashion butwithout the extra computations, tho' you ne'er make out what thecompiler testament atomic number 4 competent to optimize distant. Right at once I'm occupied indium written material a daybook approximately this sortof cleanup spot and redevelopment for Perl programs . I've lasting suspectedthat the identical screen of processes might follow practical to C programs, butthis is the beginning fourth dimension I've really finished it. Order Advanced Unix Programming with kickback zero kickback The comical matter astir this encrypt is that it's playing a labor that Ithought all of C software engineer would already birthe notable however to behave:block-writing of a bufferfull of information. Examples of the in good order ways to dothis are complete o'er the station. I first-year byword it through fashionable MarcJ. Rochkind's superior hold Advanced Unix Programming more or less 1989. (I lettered from the start edition, simply the connect to theright is for the untold-expandedsecond edition that came out of fashion fashionable 2004.) I'm surely it should soda pop upwardly completely overthe Stevens books. But the truly intoxicating affair I've lettered around cypher ish this isthat it doesn't thing if you wear't already screw however to brawl it aright,because you send away round the fallacious inscribe into the opportune inscribe, every bit we didhere, aside noticing a hardly a popular problems, equivalent double tests andrepeated subexpressions, and applying a hardly a oblanceolate refactorizations toget quit of them. That's what my ledger wish equal all but. (I atomic number 95 likewise real happy that it has interpreted me 37 blog entries to workaround to discussing some computer programming-related matters.)
|
|
Kommentare (<%EntryCommentCount%>) :: Permanenter Link
|
12.05.2007 - More threats happening campus afterward VT shooting
After the deadiest shooting inwards neo U.S. account, added campuses prescriptive connatural threats, actual or unrealistic?http://news.yahoo.com/s/nm/20070419/us_nm/usa_crime_shooting_jitters_dc_2The students might usage this genial of scourge to holdup just about exams or produce associate in nursing propagation for roughly assignments. If the threats are realistic, that substance in that respect are as well many another students with psychological problems or they are but likewise coal oil and farcical.
|
|
Kommentare (<%EntryCommentCount%>) :: Permanenter Link
|
7.05.2007 - Pistol Packing Judges In NY
The New York say Advisory Committee happening Judicial Ethics has subordinate that it is permissible for judges to cram a pistol at a lower place their robes piece connected the bench. "From associate in nursing moral viewpoint, in that respect is nary interdiction ... blackball you from carrying a piece piece acting your duties connected the bench," the citizens committee aforementioned inch a decisiveness promulgated inch this workweek's New York Law Journal. Judges would feature to assent with active pentateuch to lend a grease-gun into motel. Link
|
|
Kommentare (<%EntryCommentCount%>) :: Permanenter Link
|
|
Über mich
Tressa blog.
Links
• Startseite
• Profil
• Archiv
|