Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In the project, I found a file, math.c, with a big GPL header and ...

//------------------------------------------------------------------------------
/// Returns the minimum value between two integers.
/// \param a  First integer to compare.
/// \param b  Second integer to compare.
//------------------------------------------------------------------------------
unsigned int min(unsigned int a, unsigned int b)
{
    if (a < b) {
        return a;
    }
    else {
        return b;
    }
}

OK, cool so I need to get min value and ... this file!? So I need to open the whole project because of it? Or do I need to reinvent mathematics?

I don't believe it's just insane, so the question is: when we can just remove the GPL header?

Must I be a weirdo and do it?:

unsigned int min(             unsigned int
JEIOfuihFHIYEFHyigHUEFGEGEJEIOFJOIGHE,
unsigned int hyrthrtRERG            ) {  if
(JEIOfuihFHIYEFHyigHUEFGEGEJEIOFJOIGHE
< hyrthrtRERG            ) {  return JEIOfuihFHIYEFHyigHUEFGEGEJEIOFJOIGHE;  }
else {return hyrthrtRERG            ;    } }

Seriously, do they want me to write code like the above?

share|improve this question
17  
Well, in the recent case of Google vs Oracle the jury did find that one nine-line function that Google acknowledged copying was infringing (from ArsTechnica). –  Zenon May 12 '12 at 5:11
3  
Aww come on, seriously, min? –  Mircea Chirea May 12 '12 at 7:34
22  
I'd say the very existence of this question proves a bad thing - The job of a programmer has become less programming, more legal clutter and bureaucracy –  K.Steff May 12 '12 at 14:53
3  
Copy it. A trivial function like min is not covered by copyright. See my answer for details. –  Tony the Pony May 12 '12 at 15:59
5  
@Loki, you're missing it by a mile. Algorithms are covered by patents, not by copyright. Copyright expires 70 years after the death of the author, so works copyrighted in the 60's are still quite fresh. Finally, the question is about copying THIS code, not about the algorithm. –  alexis May 12 '12 at 22:22
show 5 more comments

15 Answers

up vote 49 down vote accepted

Unlike many of the user here, I would simply suggest: Copy it!

Make sure the formatting of the code fits your coding standard and also you should probably remove or rewrite the comment. No one will ever know you copied it - when a piece of code is this simple, you might as well have written it from scratch. If your coding standard somehow requires the function to look exactly as it does in the snippet, so be it - as long as it looks as it would look if you had written it from scratch.

Think about it, this is hardly(!) the first time this exact piece has been written - when something is this trivial, there is little reason not to copy it, if you do not feel like writing it yourself.

Even having this discussion seems a little superfluous to me - we need to be pragmatic if we are to get any real work done!

share|improve this answer
5  
the real questions is where is the barrier between trivial and non-trivial and is there way to be absolutely sure that people will not point my code with something alike "LOOK IT'S MY GPL MIN REALIZATION!"... I really afraid those people >_< –  Heather May 12 '12 at 8:24
17  
Well, there is the concept of threshold of originality in copyright. And such a simple method should hardly be copyrightable. IANAL, though, and jurisdictions vary. Still, something that every CS undergrad has written at least five times in quite exactly the same manner might very well fall under this. –  Joey May 12 '12 at 8:30
3  
@nilu: Let us then say that the programmer wrote his own version of MIN and it ended up looking exactly like the one from math.c - would it then be okay? Yes, if it was an accident. "What difference would it then make if he had just copied it?" Bits have color. ansuz.sooke.bc.ca/entry/23 This is not something most computer scientists can understand - it is indeed a complex topic, but it is something that is recognized by law. If you are in doubt about whether something is legal or not, you had best to talk to a lawyer. –  Mark Byers May 13 '12 at 16:37
3  
@alexis: I understand your point, but I do not really agree - if the resulting code is identical, no one but the programmer can know if it was copied or written from scratch. Obviously, one should have little trouble implementing min and it would probably take less time than finding another implementation. However, if a programmer does indeed copy it and no else knows that he has not written the function himself, what damage has then been done? Unless the programmer is feeling some kind of 'guilt', the answer is none. The rest is simply semantics. –  nilu May 13 '12 at 17:43
4  
@MarkByers: When I say 'cannot prove', I mean that no physical evidence exist to show that a crime has even been committed - the crime which has been commited is completely confined to the mind of the programmer. Obviously, this cannot be the case if some sweets are stolen - this has a physical impact on the world (lollipop -= 1). If the 'crime' is confined to your own mind, whos to judge whether it is moral or immoral? –  nilu May 13 '12 at 21:31
show 16 more comments

It doesn't directly answer your question, but try this:

#define min(a,b) ((a) < (b) ? (a) : (b))

I hereby release this glorious, complex macro to the public domain. Though I may have to publish a paper on this technique.

If you're macro-phobic, try this inline version (really, unless you're coding on a SPARC, you'll save lots of CPU cycles by not wrapping a function as ubiquitous as min in enter/exit code):

inline int min(int a, int b)
{
    return a < b ? a : b;
}

The issue of code licensing for trivial snippets is a bit of a nuisance, really. Check with a lawyer, but I'm tempted to assume that they can't really be licensed and/or the licensing can't be enforced in practice. Same as you can't copyright or trademark the word ‘word’, then lean on people who use it. (oops)

share|improve this answer
1  
Oh, they can certainly copyright that code, but given the simplicity of the algorithm, they'd be hard pressed to prove in court that you copied it unless you did something stupid like left comments intact, etc. The rule is not that you can't use the same code, the rule is that you can only do so if you wrote it using your own brain, and did not copy the original in any fashion. –  Steven Burnap May 12 '12 at 4:57
2  
@StevenBurnap Of course the original code would also have the same problems: can they prove they have copyright (ie. that they didn't copy it from some earlier implementation)? –  Richard May 12 '12 at 8:26
16  
Problem with macro version of min comes when you try to do min(a++,b++). –  user1249 May 12 '12 at 8:45
4  
"release this...to the public domain" is very US-centric. It doesn't mean the same thing in many countries and indeed in some is not legally possible. –  user4051 May 12 '12 at 11:46
8  
Thanks, but on a compiler from the 2000s, a compiler can inline trivialities like min. –  DeadMG May 12 '12 at 14:34
show 9 more comments

Answer: Only with explicit permission of the author.

That's copyrighted code. You cannot copy it without permission of the author.

You will have to write your own version of min, or copy a variant of min that uses a different license.

Remember, with copyright, it's the code that is protected, not the algorithm. Copyright law comes from text. Even if "math.c" was nothing but comments and had no executable code at all, it'd still be protected under copyright.

share|improve this answer
1  
What means own version? MY OWN version of MIN, sounds hilarious! –  Heather May 12 '12 at 4:58
2  
Remember, you don't own min, you own a particular sequence of characters that happens to implement min. –  Steven Burnap May 12 '12 at 5:00
2  
You cannot change it at all without permission of the author except for personal use. You have to rewrite it from scratch. That's the whole point. Note that if you sat down at a blank editor and wrote something nearly identical to that, it'd be fine, because you didn't copy it. (Assuming you weren't typing from memory.) –  Steven Burnap May 12 '12 at 5:04
2  
No, I am not joking. And yes, you can probably get away with it in cases like this. It'd be up to the copyright holder to prove that you'd done it. Note that I am just telling you what the law is, not defending it. –  Steven Burnap May 12 '12 at 5:10
3  
Because if you don't recode these trivial things, you're using someone else's work in a way that they haven't given you permission to do. If it's so trivial that you don't need it, don't use it. –  alexis May 12 '12 at 11:47
show 10 more comments

I won't use this code anyway, because the comment doesn't match with what the code does. The comment talks about integer, while the code uses unsigned int. Who knows what errors are buried within the depths of this function?

Seriously, this is where "Don't reinvent the wheel" becomes ridiculous and you forgot to take it with a grain of salt. Just write your own version from scratch, match it with the naming convention and style guide of your code (e.g. math_min_uint(x, y)), make sure it works (unit test or whatever) and go on with life and without copyright issues.

With growing experience, these trivial snippets you need again and again are just in your toolbox and you will rewrite them without thinking too much, or simply get them from your own library.

share|improve this answer
add comment

I'm releasing the following code under the BSD license. As that license is a lot more permissive you shouldn't run into copyright issues if you use my implementation.

unsigned int min(unsigned int a, unsigned int b)
{
    return (a > b? b: a);
}
share|improve this answer
    
How can I be sure that you not sealed that GPL code and rewrite it ? :P (just joking) –  Heather May 12 '12 at 8:26
    
You're not. But if you used my code you can claim you used it in good faith. –  GordonM May 12 '12 at 8:27
2  
Rejoice, brethren, as the new age has dawned - we can now find min in closed-source software. Also, @Sholy, now it's GordonM's problem whether he has copied it, or not. –  K.Steff May 12 '12 at 14:43
4  
You are also releasing it under "Creative commons" See the bottom of this page about code pasted here. –  Loki Astari May 12 '12 at 17:15
1  
show 3 more comments

It's an absurd situation, I agree, but you have only yourself to blame. If you are writing proprietary code, you should not be reading GPL-licensed source files, period. Remove math.c from your filesystem, keep the documentation, and feel free to re-implement any part of the API that you need. If you don't read the code, you don't need to worry if your own code turns out similar.

Wasteful, you say? The GPL intentionally prevents you from benefiting from free code without giving back to the ecosystem. If you can't or won't meet its terms, and you don't want to implement yet another math library, you can always buy one or find a free one that comes with a license you can accept.

Microsoft's programmers are not allowed to read open source code, to protect the company from legal problems. For purely ethical reasons, you should do the same with any program whose license you don't intend to adhere to.

You don't have a right to use this math.c, but nobody is trying to corner the market on implementations of min. The goal of the GPL is to increase programmer "freedom" (and the end user's freedom), not to restrict it. The GNU license, the FSF, and the Free Software Movement started with Richard Stallman, and Stallman started with re-implementing from scratch each new version of Symbolics lisp as it came out. The FSF would not (and could not) go after anyone for re-implementing a GPL library.

share|improve this answer
    
So but, it's really strange example. It's example, that means I must to read it and follow it... but as far as I understand I'm free to not pick GPL license here. So I know who is Stallman ;) –  Heather May 12 '12 at 11:50
2  
Sholy, sorry but I've no idea what you're saying! What example? (Are you also asking me who Stallman is? Google it. Or just check wikipedia.) –  alexis May 12 '12 at 12:06
2  
"Microsoft's programmers are not allowed to read open source code" oh my, that's horrible - is there anywhere i can read about that? Google isn't helping. –  sparkleshy May 12 '12 at 12:52
    
Good question. I read about it in some Iron Python development forum, but unfortunately I cannot find it now. Summary: Jim Hugunin (hugunin.net/microsoft_farewell.html) mentioned that they aren't allowed to look at the python implementation and someone jokingly asked if they're allowed to look at their own code (Microsoft had made Iron Python open source). The answer is: They made a special exception for Iron Python. –  alexis May 12 '12 at 13:15
    
The people whose freedom the GPL is intended to increase are all the users of the program, not (specifically) programmers. –  James Youngman May 12 '12 at 22:29
add comment

Disclaimer: I am not an attorney. Use my advice at your own risk.

This implementation of min is not copyrightable, since there are only very few reasonable ways of writing it.

In the legal world, this is known as the merger doctrine. The (non-copyrightable) idea of the function is said to have "merged" with the (potentially copyrightable) expression. Therefore, the code is not subject to copyright -- you are free to use it.

To be on the safe side, don't copy it verbatim. Use different names for variables, apply your own formatting, don't copy comments word for word. The exact literal representation of this code may be covered by copyright.

Keep in mind that this doesn't guarantee someone won't sue you, but at least you will have a strong defense.

Also, bear in mind that even if a piece of code isn't protected by copyright, it might still be covered by a software patent (This is not the case for your min function).

share|improve this answer
2  
You try telling that to Oracle! –  GordonM May 12 '12 at 15:56
    
Maybe that 9-line function wasn't trivial... I'd like to see the code. –  Tony the Pony May 12 '12 at 16:00
1  
programmers.stackexchange.com/a/148362/28718 seems pretty trivial to me, assuming that that's the correct code. –  GordonM May 12 '12 at 16:43
add comment

This is what Google Supposedly ripped off. This seems like common sense for an array.

 908     /**
 909      * Checks that fromIndex and toIndex are in range, and throws an
 910      * appropriate exception if they aren't.
 911      *
 912      * @param arrayLen the length of the array
 913      * @param fromIndex the index of the first element of the range
 914      * @param toIndex the index after the last element of the range
 915      * @throws IllegalArgumentException if fromIndex > toIndex
 916      * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
 917      *         or toIndex > arrayLen
 918      */
 919     private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
 920         if (fromIndex > toIndex)
 921             throw new IllegalArgumentException("fromIndex(" + fromIndex +
 922                        ") > toIndex(" + toIndex+")");
 923         if (fromIndex < 0)
 924             throw new ArrayIndexOutOfBoundsException(fromIndex);
 925         if (toIndex > arrayLen)
 926             throw new ArrayIndexOutOfBoundsException(toIndex);
 927     }
share|improve this answer
    
If this is all, the verdict will be tossed on appeal. But according to Wired, the judge also found Google copied 8 other files. See wired.com/wiredenterprise/2012/05/google-oracle-decompile –  Tony the Pony May 12 '12 at 16:49
3  
Yes, but "common sense" isn't the bar for copyright. It's whether they copied and edited or wrote themselves from scratch. Whether something is "Common sense" is the test for patents, not copyrights. In other words, Oracle can copyright that text, but they can't patent that algorithm. –  Steven Burnap May 12 '12 at 16:49
    
These 9 lines are a classic example of the merger doctrine. How many other ways are there to perform a range check on an array? –  Tony the Pony May 12 '12 at 16:52
1  
@TonythePony, lots, if the function throws an exception containing a textual message. –  avakar May 12 '12 at 16:58
    
@avakar But there aren't many ways to phrase that message –  Tony the Pony May 12 '12 at 17:01
show 3 more comments

People seem to be forgetting that copyright law, both in spirit and letter, means that the act of actually copying is prohibited - not the act of expressing the same thought yourself, not even if the result turns out suspiciously similar.

If you take the GPL'd code, modify it, use it in a way that is against the license under which you received it, and claim it is not a derived work, then you are breaking the law. Even if you use your second version - you have changed the names of all variables, and altered the formatting, but it's still derived from the original.

If, however, you write your own version of min, and due to the triviality of the task, the fact that there are only so many ways you can sensibly write such a thing, and a bit of coincidence, your version ends up being exactly identical to the GPL'd code, then you haven't copied anything.

Whether or not either story would pass in court is a different matter though. If a larger or more complex code sample shows up somewhere else, complete with comments, formatting, and spelling errors, you'll have a hard time convincing anyone that you wrote it independently, more so if it can be shown that you knew the original source (which, given the fact that you have posted it here, should be fairly trivial).

share|improve this answer
add comment

I wouldn't copy that version. You really want a stable implementation, so here is a version of min that you can copy :)

unsigned int min(unsigned int l, unsigned int r) {
    if ( l <= r )
        return l;
    else
        return r;
}

In the case where l and r are equal this one will return l. Doesn't make any difference for unsigned int, but for more complex types then stability is always a good thing.

share|improve this answer
add comment

For the general case; More complex code that is definitely copyright-able, as opposed to min and max that may or may not be copyright-able / you may not get caught etc.

License your code as GPL!

share|improve this answer
add comment

If the code is really is trivial as the "min" example then there is a very high probability that someone else has already implemented the same functionality under a different open source license.

Depending on the license you may verywell be allowed to include the code in your closed source appliantion.

share|improve this answer
add comment

If you're using C++ and not C, sidestep this issue by using the standard library std::min or std::max in <algorithm>:

http://www.cplusplus.com/reference/algorithm/min/

That buys you generality, and it will work for any comparable type. Also: when whatever d-bag tries to claim they invented it and is covered by software patents, they'll be rumbling with the ISO committee...not you.

In the general case, take note of who published the code. Corporation, or sane individual? If it's a sane individual (as is the case with much open source), write a nice note to them and get permission. Save their response saying it's okay. Problem solved.

share|improve this answer
add comment

Just write it yourself in 30 seconds. You can use exactly the same algorithm. Just don't copy/paste.

Copyright only protects the expression of an idea, not the idea itself. You can use the idea directly providing you don't copy it.

FWIW, I doubt that copying such a trivial piece of code would get you into trouble anyway. Damages would be zero, and it would probably fall under fair use in most jurisdictions. But legal cases are such a pain that I wouldn't want to risk it.

share|improve this answer
add comment

As its GPL code the big question here is are you planning distrubute your code or sell your software.

If you are just using it internally the GPL gives you complete freedom to do as you wish.

If you are planning to distribute you package -- the easiest thing is to GPL your package on the same terms as any GPL source you used. There are other ways but it gets complicated.

If you are planning to sell your software, you must distribute the source code for any GPL code you used and/or amended under the same GPL license by which you obtained it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.