r/badcode • • Jul 01 '20

c++ Found this in someone's project

Post image
710 Upvotes

43 comments sorted by

95

u/mustaineTheDave Jul 01 '20 edited Jul 01 '20

Shouldn't i start at filename.size() - 1?

And is the reason it breaks on windows the fact that he accesses an index out of bounds? Why doesn't it break on other platforms too?

C/C++ noob here, genuinely want to know.

EDIT: I'm dumb, windows uses \

97

u/Restioson Jul 01 '20

Probably because the Windows path separator is \

1

u/[deleted] Jul 02 '20

With WSL and cygwin that's basically the way it happens lol

38

u/Mr2-1782Man Jul 01 '20

Windows directory separator is \, Linux (Unix-like in general) is /. Although I would point out it'll break on Linux too. A separator isn't required as part of a path, hello.txt is a valid path in Linux. It needs a length check.

10

u/lestofante Jul 01 '20 edited Jul 02 '20

It has lenght check, you would get a i == -1,if substr does not break and return an empty dir string/null, could be fine (to see the rest of code)

3

u/Mr2-1782Man Jul 02 '20

I missed that, its a weird way to process directories.

1

u/eXl5eQ Jul 02 '20

string::substr(0, -1) should return the whole string, not an empty string

0

u/[deleted] Jul 02 '20

[deleted]

3

u/0x564A00 Jul 02 '20

It is a perfectly valid relative path.

6

u/Farull Jul 02 '20

No, in C++11, filename[filename.size()] is guaranteed to be ’\0’, and this was already true in many previous implementations.

4

u/real_fff Jul 01 '20

You're right, you would think this would crash with a Segmentation Fault, but it doesn't because more memory is allocated than you think (and C/C++ doesn't check if you're accessing a valid index when using subscript [] operator). If you're using [], a program won't crash until some time after the last index depending on how much memory you have allocated and system config.

96

u/MathSciElec Jul 01 '20

Terrible code indeed. They forgot the spaces between variables/constants and operators!

38

u/Mr2-1782Man Jul 01 '20

Even worse, the spacing is inconsistent.

4

u/VaginalMatrix Jul 02 '20

Welcome to a competitive programmer's life

23

u/The_Northern_Light Jul 02 '20

The days before std::filesystem were dark indeed.

62

u/Noobnugget19 Jul 01 '20

I'm more upset about the bad spacing with = and super huge indents

6

u/[deleted] Jul 01 '20

who needs good code anyways

4

u/[deleted] Jul 01 '20

If it mildly works, it's good enough

4

u/machine3lf Jul 02 '20

I thing that's in Pragmatic Programmer somewhere.

1

u/[deleted] Jul 02 '20
 // load bearing code, do not touch

9

u/BlazingThunder30 Jul 01 '20

What's wrong with tabspace = 8?

9

u/Mr2-1782Man Jul 01 '20

Some of use like to have two windows side by side.

-4

u/lestofante Jul 01 '20

Wich is exactly why 8 space tab is enforced in Linux, if you exceed the 80char limit like this, you probably affected by arrow code syndrome. If course general rule, not a universal law.

9

u/[deleted] Jul 02 '20

Yep, Torvalds said once that if you need more than 3 indents, your code is probably flawed (ofc it's C, so no function definitions inside classes or namespaces). But they changed the 80 char hard limit to a soft limit last kernel update and the new hardlimit is now 100 chars per line.

3

u/ezylot Jul 02 '20

The only true indentation length for tabs is 0 /s

8

u/0x3fff0000 Jul 02 '20

Whew, thought that was mine for second.

4

u/KCGD_r Jul 02 '20

What is this even supposed to do?

15

u/GOKOP Jul 02 '20

When you have /some/path/to/some/file it returns /some/path/to/some/

3

u/[deleted] Jul 02 '20

Someone needs to familiarize themselves with the standard library.

3

u/[deleted] Jul 02 '20

maybe use preprocessor #if and etc.? am i correct?

C++ noob here

2

u/TheSoundDude Jul 02 '20

For cross platform? Yeah, that would work. Using the proper filesystem library would be much better though, of course.

7

u/[deleted] Jul 01 '20

This whole function is equivalent to

return filename.substr(0, filename.lastIndexOf('/'));

Edit: if youre writing in java don't know about cpp tho.

17

u/Breadfish64 Jul 01 '20

std::filesystem::path{filename}.parent_path();

2

u/[deleted] Jul 01 '20

What’s the implementation of lastIndexOf?

11

u/Breadfish64 Jul 02 '20

If you want the exact equivalent of the Java that would be
return filename.substr(0, filename.rfind('/'));

1

u/eXl5eQ Jul 02 '20

This Java version throws OOB Exception if filename doesn't contain any '/'. (BTW in Java this method is called substring, not substr). The C++ version returns the whole string

1

u/[deleted] Jul 02 '20

Shit your right. All you would have to add is a condition checking if substring returns -1.

1

u/necheffa Jul 02 '20

So basically a shitty version of dirname.

1

u/Yaroster Jul 02 '20

this is actually hilarious

1

u/Urtehnoes Jul 02 '20

God bless all languages that come with some form of Environment/OS/Application.PathSeparator

I ain't got time to figure out how these things are separated in a directory!!

1

u/notYuriy Jul 02 '20

oh, terrible code, because they use using namespace std. Yeah, I agree

0

u/[deleted] Jul 02 '20

[deleted]

7

u/wickermoon Jul 02 '20

should've stopped after the third word. :)

0

u/recycle4science Jul 02 '20

For sure, nobody uses it.