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
unsigned int hyrthrtRERG            ) {  if
< hyrthrtRERG            ) {  return JEIOfuihFHIYEFHyigHUEFGEGEJEIOFJOIGHE;  }
else {return hyrthrtRERG            ;    } }

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

  • 23
    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
    Commented May 12, 2012 at 5:11
  • 5
    Aww come on, seriously, min? Commented May 12, 2012 at 7:34
  • 39
    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
    Commented May 12, 2012 at 14:53
  • 5
    Copy it. A trivial function like min is not covered by copyright. See my answer for details. Commented May 12, 2012 at 15:59
  • 6
    @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
    Commented May 12, 2012 at 22:22

14 Answers 14


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!

  • 6
    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 >_<
    – cnd
    Commented May 12, 2012 at 8:24
  • 23
    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
    Commented May 12, 2012 at 8:30
  • 5
    @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
    Commented May 13, 2012 at 16:37
  • 6
    @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
    Commented May 13, 2012 at 21:31
  • 6
    Lawyers made us afraid of doing simplest most natural things.
    – Kamil Szot
    Commented May 18, 2012 at 0:39

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 (also known as the idea-expression divide). 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).

  • 2
    You try telling that to Oracle!
    – GordonM
    Commented May 12, 2012 at 15:56
  • 1
    Maybe that 9-line function wasn't trivial... I'd like to see the code. Commented May 12, 2012 at 16:00
  • 2
    programmers.stackexchange.com/a/148362/28718 seems pretty trivial to me, assuming that that's the correct code.
    – GordonM
    Commented May 12, 2012 at 16:43
  • Actually, the code is so trivial and there is so little semantics for the arguments, that ven using other variable names would be curious. Commented May 5 at 18:47

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)

  • 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.
    – user53141
    Commented May 12, 2012 at 4:57
  • 3
    @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
    Commented May 12, 2012 at 8:26
  • 19
    Problem with macro version of min comes when you try to do min(a++,b++).
    – user1249
    Commented May 12, 2012 at 8:45
  • 6
    "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
    Commented May 12, 2012 at 11:46
  • 9
    Thanks, but on a compiler from the 2000s, a compiler can inline trivialities like min.
    – DeadMG
    Commented May 12, 2012 at 14:34

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.

  • 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 ;)
    – cnd
    Commented May 12, 2012 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
    Commented May 12, 2012 at 12:06
  • 3
    "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.
    – amara
    Commented May 12, 2012 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
    Commented May 12, 2012 at 13:15
  • The people whose freedom the GPL is intended to increase are all the users of the program, not (specifically) programmers. Commented May 12, 2012 at 22:29

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);
  • 1
    How can I be sure that you not sealed that GPL code and rewrite it ? :P (just joking)
    – cnd
    Commented May 12, 2012 at 8:26
  • 1
    You're not. But if you used my code you can claim you used it in good faith.
    – GordonM
    Commented May 12, 2012 at 8:27
  • 4
    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
    Commented May 12, 2012 at 14:43
  • 7
    You are also releasing it under "Creative commons" See the bottom of this page about code pasted here. Commented May 12, 2012 at 17:15
  • 1
    See: meta.programmers.stackexchange.com/q/3585/12917 Commented May 12, 2012 at 21:13

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.

  • 1
    What means own version? MY OWN version of MIN, sounds hilarious!
    – cnd
    Commented May 12, 2012 at 4:58
  • 2
    Remember, you don't own min, you own a particular sequence of characters that happens to implement min.
    – user53141
    Commented May 12, 2012 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.)
    – user53141
    Commented May 12, 2012 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.
    – user53141
    Commented May 12, 2012 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
    Commented May 12, 2012 at 11:47

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     }
  • 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 Commented May 12, 2012 at 16:49
  • 5
    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.
    – user53141
    Commented May 12, 2012 at 16:49
  • 1
    @TonythePony, lots, if the function throws an exception containing a textual message.
    – avakar
    Commented May 12, 2012 at 16:58
  • 1
    The comment block is only protected if it reaches a minimum threshold of originality. A short, descriptive phrase is not protected. Commented May 13, 2012 at 8:31
  • 6
    Few people have mentioned that the person who copied this text at Google is also the person who wrote it at Sun. Programmers have to realize that Stallman--the crazy bearded extremist--was right. Money, that root of all evil, makes people forget why they participate in technology in the first place. :-/ Commented May 18, 2012 at 0:44

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.


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).


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.


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.


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.


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


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.


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).

Then License your code in compliance with the library i.e. GPL! Note however that there are LGPL libraries (and GPL with exceptions) that let you use their implementations while giving your code whatever licence you wish.

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